Searching and Sorting. Gary Wong. Prerequisite. Time complexity Pseudocode (Recursion). Searching Linear (Sequential) Search Binary Search. Sorting Bubble Sort Merge Sort Quick Sort Counting Sort. Agenda. Linear Search. One by one. Linear Search.
Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.
Searching and Sorting
Gary Wong
Searching
Linear (Sequential) Search
Binary Search
Sorting
Bubble Sort
Merge Sort
Quick Sort
Counting Sort
Linear Search
One by one...
Not found!
Found!
Binary Search
Chop by half...
L
R
Smaller!
Found!
Larger!
1) Initialize boundaries L and R
2) While L is still on the left of R
L
R
Bubble Sort
Smaller? Float! Larger? Sink!
18
9
20
11
77
45
Incorrect order, swap!
Correct order, pass!
for i -> 1 to n-1
for j -> 1 to n-i
if a[j]>a[j+1], swap them
Merge Sort & Quick Sort
Many a little makes a mickle...
List 1:
8
14
22
List 2:
10
13
29
65
Temporary list:
8
8
10
10
13
13
14
14
22
22
29
29
65
65
Combined list:
Merge
10
13
29
65
8
14
22
L
(L+R)/2
(L+R)/2+1
R
mergesort(L,R){
If L is equal to R, done;
Otherwise,
m=(L+R)/2;
mergesort(L,M);
mergesort(M+1,R);
Merge the lists [L,M] and [M+1,R];
}
mergesort(0,6)
mergesort(0,1)
mergesort(2,3)
mergesort(4,5)
mergesort(6,6)
65
10
29
13
14
8
22
mergesort(0,0)
mergesort(1,1)
mergesort(2,2)
mergesort(3,3)
mergesort(4,4)
mergesort(5,5)
mergesort(0,3)
mergesort(4,6)
8
10
10
65
13
10
13
29
13
14
29
65
22
8
8
14
29
14
65
22
10
13
29
65
8
14
22
10
13
8
14
22
65
29
x
y
a[y] < pivot! shift x, swap!
10
13
29
65
8
14
22
10
13
8
14
22
65
29
quicksort(L,R){
If L is equal to R, done;
Otherwise,
Choose a number as a pivot, say a[p];
Perform pivoting action;
quicksort(L,p-1);
quicksort(p+1,R);
}
Counting Sort
No more comparison...
More...
If we have time...
Time for lunch
Yummy!