Chapter 05_ Sorting and Searching Algorithms
Chapter 05_ Sorting and Searching Algorithms
Oct, 2024
Contents
•Algorithm analysis
•Running time
•Comparision in insertion and
merging
•Mergesort and its comparison
•Sorting
•Searching: Sequential search and
binary search
2
Algorithm analysis
• Worst case
– element k requires (k-1) comparisons
– total number of comparisons:
0+1+2+ … + (n-1) = ½ (n)(n-1)
= ½ (n2-n)
• Best case
– elements 2 through n each require one comparison
– total number of comparisons:
1+1+1+ … + 1 = n-1
(n-1) times
Running time of insertion sort
27 10 12 20
divide
27 10 12 20
divide divide
27 10 12 20
merge merge
10 27 12 20
merge
10 12 20 27
Merging two sorted lists
10 27 12 20 10
10 27 12 20 10 12
10 27 12 20 10 12 20
10 27 12 20 10 12 20 27
Comparisons in merging
• Merging two sorted lists of size m requires at least m and at most 2m-1
comparisons
– m comparisons if all elements in one list are smaller than all elements in
the second list
– 2m-1 comparisons if the smallest element alternates between lists
Logarithm
• Best, worst and average-case running time of mergesort is O(n log n).
– This consistency is due to the algorithm always dividing the array into two
halves and merging them, regardless of the initial order of elements.
• Compare to average case behavior of insertion sort:
• If the input is already sorted, we can search more efficiently than linear time
• Example: “Higher-Lower”
– think of a number between 1 and 1000
– have someone try to guess the number
– if they are wrong, you tell them if the number is higher than their guess or
lower
• Strategy?
• How many guesses should we expect to make?
Best Strategy
• Binary search
– search n sorted inputs in logarithmic time
Binary search
1 3 4 5 5 7 9 10 11 13 14 18 20 22 23 30
1 3 4 5 5 7 9 10
5 7 9 10
9 10
9
Sequential vs. binary search
n sequential binary
search search
2 1 1
16 8 4
256 128 8
4096 2048 12
65536 32768 16
Searching
list
E. O(NlogN)
Stability:
• Selection Sort is not stable, so equal elements may not stay in
the same order after sorting.
Next Session!
Array sorted
50
ShellSort Code
public static void shellsort(Comparable[] list)
{ Comparable temp; boolean swap;
for(int gap = list.length / 2; gap > 0; gap /= 2)
for(int i = gap; i < list.length; i++)
{ Comparable tmp = list[i];
int j = i;
for( ; j >= gap &&
tmp.compareTo( list[j - gap] ) < 0;
j -= gap )
list[ j ] = list[ j - gap ];
list[ j ] = tmp;
}
}
times in milliseconds 52
Quicksort
8 Invented by C.A.R. (Tony) Hoare
8 A divide and conquer approach
that uses recursion
1. If the list has 0 or 1 elements it is sorted
2. otherwise, pick any element p in the list. This is called
the pivot value
3. Partition the list minus the pivot into two sub lists
according to values less than or greater than the pivot.
(equal values go to either)
4. return the quicksort of the first list followed by the
quicksort of the second list
Sorting and Searching 53
Quicksort in Action
39 23 17 90 33 72 46 79 11 52 64 5 71
Pick middle element as pivot: 46
Partition list
23 17 5 33 39 11 46 79 72 52 64 90 71
quick sort the less than list
Pick middle element as pivot: 33
23 17 5 11 33 39
quicksort the less than list, pivot now 5
{} 5 23 17 11
quicksort the less than list, base case
quicksort the greater than list
Pick middle element as pivot: 17
and so on….
Why?
Next Session:
Chapter 06:
61