Download presentation
Presentation is loading. Please wait.
1
Sorting
2
Selection sort Algorithm selection_sort(A,n)
input: An array A of n integers---- A[0] to A[n-1] output: An updated array A with elements sorted in ascending order for i = 0 to n-2 small_pos i for j = i+1 to n-1 if A[j] < A[small_pos] small_pos j temp A[i] A[i] A[small_pos] A[small_pos] temp
3
Running time of selection_sort
Worst case O(n2) why? Best case O(n2)
4
Insertion sort Algorithm insertion_sort(A,n)
input: An array A of n integers output: An updated array A with elements sorted in ascending order for j = 1 to n-1 key A[j] i j-1 while i>=0 and A[i] > key A[i+1] A[i] i i-1 A[i+1] key
5
Running time of insertion_sort
Worst case O(n2) when and why? Best case O(n)
6
Bubble sort Algorithm bubble_sort(A,n)
input: An array A of n integers---- A[0] to A[n-1] output: An updated array A with elements sorted in ascending order for i = 0 to n-2 for j = n-1 down to i+1 if A[j] < A[j-1] swap A[j] with A[j-1]
7
Running time of bubble_sort
Worst case O(n2) why? Best case O(n2)
8
Modified Bubble sort Algorithm bubble_sort2(A,n)
input: An array A of n integers---- A[0] to A[n-1] output: An updated array A with elements sorted in ascending order repeat_flag true i 0 while (repeat_flag = true and i<n-1) repeat_flag false for j = n-1 down to i+1 if A[j] < A[j-1] swap A[j] with A[j-1] i i + 1
9
Running time of bubble_sort2
Worst case O(n2) when and why? Best case O(n)
10
Heapsort Binary heap Max heap Min heap Nearly complete binary tree
Completely filled on all levels except possibly the lowest Max heap Every node in the tree has a key value higher than its children Min heap Every node in the tree has a key value lower than its children
11
heap
12
heap – parent/child
13
heapsort functions
14
Build heap
15
Building heap – Example (a)
16
Building heap – Example (b)
17
Building heap – Example (c)
18
Building heap – Example (d)
19
Building heap – Example (e)
20
Building heap – Example (f)
21
Heapsort Algorithm
22
Heapsort Example (a)
23
Heapsort Example (b)
24
Heapsort Example (c)
25
Heapsort Example (d)
26
Heapsort Example (e)
27
Heapsort Example (f)
28
Heapsort Example (g)
29
Heapsort Example (h)
30
Heapsort Example (h)
31
Heapsort Example (i)
32
Heapsort Example (j)
33
Merge sort
34
Merge
35
Divide and conquer Divide the problem into a number of subproblems that are smaller instances of the same problem. Conquer the subproblems by solving them recursively. If the subproblem sizes are small enough, however, just solve the subproblems in a straightforward manner. Combine the solutions to the subproblems into the solution for the original problem.
36
Quick sort Divide: Partition (rearrange) the array A[p..r] into two (possibly empty) subarrays A[p..q-1] and A[q+1..r] such that each element of A[p..q-1] is less than or equal to A[q], which is, in turn, less than or equal to each element of A[q+1..r] . Compute the index q as part of this partitioning procedure.
37
Quick sort Conquer: Sort the two subarrays A[p..q-1] and A[q+1..r] by recursive calls to quicksort. Combine: Because the subarrays are already sorted, no work is needed to combine them: the entire array A[p..r]is now sorted.
38
Quicksort
39
Partition
40
Quicksort example
41
Quicksort example
42
Quicksort example
43
Performance analysis of quicksort
Worst case O(n2) when and why? Best case O(n lg n) Average case O(n lg n) why?
44
Balanced Partition
45
Randomized quicksort
46
Quicksort - Median of three
47
Counting sort
48
Radix sort
49
Radix sort example
50
Radix sort - Exercise 8.3-1 (CLRS)
Using the previous exercise as a model, illustrate the operation of RADIX-SORT on the following list of English words: COW, DOG, SEA, RUG, ROW, MOB, BOX, TAB, BAR, EAR, TAR, DIG, BIG, TEA, NOW, FOX.
51
Bucket sort Bucket sort assumes that the input is drawn from a uniform distribution Bucket sort divides the interval [0,1) into n equal-sized subintevals, or buckets An auxiliary array B[0..n-1] of linked lists (buckets) is used.
52
Bucket sort example
53
Bucket sort
54
Comparison of sorting algorithms
Sorting technique Best Case Worst case In-place? stable? insertion O(n) O(n2) yes bubble selection no heap O(n lg n) merge quick counting O(n+k) radix O(d(n+k) bucket
55
External sorting Multi-way merge sort
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.