Title: Introduction to Computing
1Introduction to Computing
2Lecture 10
3Using Arrays Ordered Arrays
- Ordered array vs Unordered array
- E.g
- Ascending order
- each element
4Using Arrays Ordered Arrays
- Ordered array vs Unordered array
-
- Advantage Efficient Searching
5Using Arrays Ordered Arrays
- Example
- Request a name and inform if in the ordered list
6Using Arrays Ordered Arrays
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
7Sub Command1_Click Dim n As Integer, name2Find
As String Let name2Find Ucase(Trim(Text1.Text))
Let n0 Do Let nn1 Loop Until (nom(n)
name2Find) Or (n5) If nom(n) name2Find
Then Picture1.Print Found Else Picture1.Pri
nt Not found End If End Sub
8Using Arrays Ordered Arrays
Average search was half the array dimension
9Sorting
10Sorting
- Sorting Ordering an unordered array like the
following - Pebbles
- Barney
- Wilma
- Fred
- Dino
-
11Bubble Sorting Algorithm
- 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?
12Bubble Sorting
- 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 Dino Wilma
-
13Bubble Sorting
- Second Pass
- Barney Barney Barney Braney
- Pebbles Pebbles Fred Fred
- Fred Fred Pebbles Dino
- Dino Dino Dino Pebbles
- Wilma Wilma Wilma Wilma
-
14Bubble Sorting
- Third Pass
- Barney Barney Barney
- Fred Fred Dino
- Dino Dino Fred
- Pebbles Pebbles Pebbles
- Wilma Wilma Wilma
-
15Bubble Sorting
- Fourth Pass
- Barney Barney
- Dino Dino
- Fred Fred
- Pebbles Pebbles
- Wilma Wilma
-