0% found this document useful (0 votes)
36 views

7 Algorithm (Searchinmg & Sorting UPDATED) (MT

Uploaded by

ahsan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
36 views

7 Algorithm (Searchinmg & Sorting UPDATED) (MT

Uploaded by

ahsan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 5
Syllabus Content: 7 Algorithm design and problem-solving © Show understanding of linear searching method Linear Search: Linear search is a method of searching a list in which each element of an array is checked in order, from the lower bound to the upper bound, until the item is found, or the upper bound is reached. The pseudocode linear search algorithm and identifier table to find if an item is in the 1D array(myList) is given below. DECLARE count, num As Integer DECLARE found As Boolean = False //Creating array to search intem (Free notes @ www.majidtahir.com) DECLARE Mylist() As Integer = {4, 2, 8, 17, 9, 3, 7, 12, 34, 21) OUTPUT (“Please enter any integer to be checked in List") INPUT num For count = @ To 9 If item = Mylist(count) Then found = True End If Next If found = True Then OUTPUT ("Item Found = ", num) Else ‘OUTPUT ("Item Found is unsuccessful") End If Sample VB program of linear search: Dim count, num As Integer Website: www.majidtahir.com || Contact: 03004003666 | [email protected] aDim found As Boolean = False Dim Mylist() As Integer = {4, 2, 8, 17, 9, 3, 7, 12, 34, 21) ‘Create array to search iten Console.WriteLine("please enter an integer to be found") num = Console.ReadLine() For index = @ To 9 ‘If item = Mylist(count) Then found = True End If Next If found = True Then Console-WriteLine("Iten Found = * & item) Else Console.WriteLine("Item Found is Unsucessful") End If Bubble Sort [Compare] [Compare] [Compare] [Compare] [Compare] [Compare] ‘Sorted * baie] [SMB] a Bae par| — | "fa ist ‘by | es 25 25 25 25 25 eee wo | 34 4 34. 4 34 98 mm 7 im = 7 z 7 7 a 41 al a a 41 swap | 99 19 BC) 19 19 19 oo 5 5 5 5 5 Figure 11.12 Swapping values working down the array When we have completed the first pass through the entire array, the largest value is in the correct position at the end of the array. The other values may or may not be in the correct order. We need to work through the array again and again. After each pass through the array the next largest value will be in its correct position, as shown in Figure below. @ Website: www.majidtahir.com || Contact: 03004003666 | [email protected] a@ original | [ after | { after | [ after | [after | [ after | [ after list passa | | pass2| | pass3 | | passa | | passs | | passe 25 25 25 7 7 7 5 34 u 7 25 19 5 we || 7 3 19 5 7 41 19 5 a 19 5 19 5 : | Favre 11.12 Ctatac af the arrav aftaraach nace In effect we perform a loop within a loop, a nested loop. This method is known as a bubblesort. The name comes from the fact that smaller values slowly rise to the top, like bubbles in a liquid. | Bubble sort: a sort method where adjacent pairs of values are compared and swapped Bubble Sort Algorithm: BEGIN DECLARE MyList [10] : INTEGER = {4, 2,8,17,9,3,7,12,34,21} DECLARE count, temp, top : INTEGER DECLARE swap : BOOLEAN = False top = LENGTH (MyList()) WHILE top 0. FOR count = 1 to (top-1) IF Mylist(count) > Mylist(count + 1) THEN temp = Mylist(count) Mylist(count) = Mylist(count + 1) Mylist(count + 1) = temp swap = True End if top = top- NEXT END WHILE END Website: www.majidtahir.com || Contact: 03004003666 | [email protected] a@ Or another sample Program DECLARE myList : ARRAYS[0:8] OF INTEGER = {4, 2,8,17,9,3,7,12,34,21} DECLARE upperBound, lowerbound, count, temp, top : INTEGER DECLARE swap + BOOLEAN Uuppersound €— 10 loverBound MyList [count + 1] THEN Temp =m HyList[count] WyList[eount] m= MyList{count + 1] MyList[count +1] myList(index + 1) Then ‘temp = myList (index) mylist(index) = myList(index + 1) mylist(index + 1) = temp swap = True end If Next top = top - 2 Loop Until (Not Swap) Or (top = 0) “Outpout The Sorted Array For index = @ To mylist.Length - 1 Console.WriteLine(myList(index) & " “ Next Console. Readkey() End Sub End Module Website: www.majidtahir.com || Contact: 03004003666 | [email protected] aReferences: Computer Science AS & A Level Coursebook by Sylvia Langfield & Dave Duddell Computer Science Teacher's Resource Computer Science AS & A level by HODDER EDUCATION https://www. youtube.com/watch 2v=I-kosUrt ft https:/Awww.dotnetperis.com/dictionary-vonet http://www. woridbestlearningcenter,com/index.files/vb.net-example-insertion-sort.htm wz Website: www.majidtahir.com & ‘[email protected] 2

You might also like