Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to Computing Dr. Nadeem A Khan. Lecture 10.

Similar presentations


Presentation on theme: "Introduction to Computing Dr. Nadeem A Khan. Lecture 10."— Presentation transcript:

1 Introduction to Computing Dr. Nadeem A Khan

2 Lecture 10

3 ► Ordered array vsUnordered array E.g:  Ascending order: [each element]<=[next element] Using Arrays: Ordered Arrays

4 ► Ordered array vsUnordered array => Advantage: Efficient Searching Using Arrays: Ordered Arrays

5 ► Example: Request a name and inform if in the ordered list Using Arrays: Ordered Arrays

6 Dim nom(1 To 5) As String ‘General Declaration Sub Form_Load Rem Place the names in the array in ascending order Let nom(1) =“AKBAR” Let nom(2) =“ASLAM” Let nom(3) =“BUSHRA” Let nom(4) =“TONY” Let nom(5) =“ZAID” End Sub Using Arrays: Ordered Arrays

7 Sub Command1_Click Dim n As Integer, name2Find As String Let name2Find = Ucase(Trim(Text1.Text)) Let n=0 Do Let n=n+1 Loop Until (nom(n) >= name2Find) Or (n=5) If nom(n) =name2Find Then Picture1.Print “Found” Else Picture1.Print “Not found” End If End Sub

8 => Average search was half the array dimension Using Arrays: Ordered Arrays

9 Sorting

10 ► Sorting: Ordering an unordered array like the following: PebblesBarneyWilmaFredDino Sorting

11 ► Compare adjacent items and swap if out of order ► In n-1 passes the array of n elements will get sorted =>How will you swap values stored in a pair of variables? Bubble Sorting Algorithm

12 ► First Pass Pebbles Barney Barney Braney Barney Barney Pebbles Pebbles Pebbles Pebbles Wilma Wilma Wilma Fred Fred Fred Fred Fred Wilma Dino Dino Dino Dino DinoWilma Bubble Sorting

13 ► Second Pass Barney Barney Barney Braney Pebbles Pebbles Fred Fred FredFred Pebbles Dino Dino Dino Dino Pebbles Wilma Wilma Wilma Wilma Bubble Sorting

14 ► Third Pass Barney Barney Barney FredFred Dino DinoDino Fred Pebbles Pebbles Pebbles Wilma Wilma Wilma Bubble Sorting

15 ► Fourth Pass Barney Barney DinoDino FredFred Pebbles Pebbles Wilma Wilma Bubble Sorting


Download ppt "Introduction to Computing Dr. Nadeem A Khan. Lecture 10."

Similar presentations


Ads by Google