Presentation is loading. Please wait.

Presentation is loading. Please wait.

Week 11 Sorting Algorithms. Sorting Sorting Algorithms A sorting algorithm is an algorithm that puts elements of a list in a certain order. We need sorting.

Similar presentations


Presentation on theme: "Week 11 Sorting Algorithms. Sorting Sorting Algorithms A sorting algorithm is an algorithm that puts elements of a list in a certain order. We need sorting."— Presentation transcript:

1 Week 11 Sorting Algorithms

2 Sorting

3 Sorting Algorithms A sorting algorithm is an algorithm that puts elements of a list in a certain order. We need sorting algorithm in lots of areas: Science Engineering Business ……

4 https://en.wikipedia.org/wiki/Sorting_algorithm

5 Quadratic Sorting Algorithms We are given n records to sort. There are a number of simple sorting algorithms whose worst and average case performance is quadratic O(n 2 ): –Selection sort –Insertion sort –Bubble sort

6 Sorting an Array of Integers Example: we are given an array of six integers that we want to sort from smallest to largest [0] [1] [2] [3] [4] [5]

7 Selection sort

8 The Selection Sort Algorithm Start by finding the smallest entry. [0] [1] [2] [3] [4] [5]

9 The Selection Sort Algorithm Swap the smallest entry with the first entry. [0] [1] [2] [3] [4] [5]

10 The Selection Sort Algorithm Swap the smallest entry with the first entry. [0] [1] [2] [3] [4] [5]

11 The Selection Sort Algorithm Part of the array is now sorted. Sorted side Unsorted side [0] [1] [2] [3] [4] [5]

12 The Selection Sort Algorithm Find the smallest element in the unsorted side. Sorted side Unsorted side [0] [1] [2] [3] [4] [5]

13 The Selection Sort Algorithm Swap with the front of the unsorted side. Sorted side Unsorted side [0] [1] [2] [3] [4] [5]

14 The Selection Sort Algorithm We have increased the size of the sorted side by one element. Sorted side Unsorted side [0] [1] [2] [3] [4] [5]

15 The Selection Sort Algorithm The process continues... Sorted side Unsorted side Smallest from unsorted Smallest from unsorted [0] [1] [2] [3] [4] [5]

16 The Selection Sort Algorithm The process continues... Sorted side Unsorted side [0] [1] [2] [3] [4] [5] Swap with front Swap with front

17 The Selection Sort Algorithm The process continues... Sorted side Unsorted side Sorted side is bigger Sorted side is bigger [0] [1] [2] [3] [4] [5]

18 The Selection Sort Algorithm The process keeps adding one more number to the sorted side. The sorted side has the smallest numbers, arranged from small to large. Sorted side Unsorted side [0] [1] [2] [3] [4] [5]

19 The Selection Sort Algorithm We can stop when the unsorted side has just one number, since that number must be the largest number. [0] [1] [2] [3] [4] [5] Sorted side Unsorted side

20 The Selection Sort Algorithm The array is now sorted. We repeatedly selected the smallest element, and moved this element to the front of the unsorted side. [0] [1] [2] [3] [4] [5]

21 Selection sort animation

22 template void selection_sort(Item data[ ], size_t n) { size_t i, j, smallest; Item temp; if(n < 2) return; // nothing to sort!! for(i = 0; i < n-1 ; ++i) { // find smallest in unsorted part of array smallest = i; for(j = i+1; j < n; ++j) if(data[smallest] > data[j]) smallest = j; // put it at front of unsorted part of array (swap) temp = data[i]; data[i] = data[smallest]; data[smallest] = temp; }

23 Selection Time Sort Analysis In O-notation, what is: –Worst case running time for n items? –Average case running time for n items? Steps of algorithm: for i = 1 to n-1 find smallest key in unsorted part of array swap smallest item to front of unsorted array decrease size of unsorted array by 1

24 Selection Time Sort Analysis In O-notation, what is: –Worst case running time for n items? –Average case running time for n items? Steps of algorithm: for i = 1 to n-1 O(n) find smallest key in unsorted part of array O(n) swap smallest item to front of unsorted array decrease size of unsorted array by 1 Selection sort analysis: O(n 2 )

25 template void selection_sort(Item data[ ], size_t n) { size_t i, j, smallest; Item temp; if(n < 2) return; // nothing to sort!! for(i = 0; i < n-1 ; ++i) { // find smallest in unsorted part of array smallest = i; for(j = i+1; j < n; ++j) if(data[smallest] > data[j]) smallest = j; // put it at front of unsorted part of array (swap) temp = data[i]; data[i] = data[smallest]; data[smallest] = temp; } Outer loop: O(n)

26 template void selection_sort(Item data[ ], size_t n) { size_t i, j, smallest; Item temp; if(n < 2) return; // nothing to sort!! for(i = 0; i < n-1 ; ++i) { // find smallest in unsorted part of array smallest = i; for(j = i+1; j < n; ++j) if(data[smallest] > data[j]) smallest = j; // put it at front of unsorted part of array (swap) temp = data[i]; data[i] = data[smallest]; data[smallest] = temp; } Outer loop: O(n) Inner loop: O(n)

27 Insertion sort

28 The Insertion Sort Algorithm The Insertion Sort algorithm also views the array as having a sorted side and an unsorted side. [0] [1] [2] [3] [4] [5]

29 The Insertion Sort Algorithm The sorted side starts with just the first element, which is not necessarily the smallest element. [0] [1] [2] [3] [4] [5] Sorted side Unsorted side

30 The Insertion Sort Algorithm The sorted side grows by taking the front element from the unsorted side... [0] [1] [2] [3] [4] [5] Sorted side Unsorted side

31 The Insertion Sort Algorithm...and inserting it in the place that keeps the sorted side arranged from small to large. [0] [1] [2] [3] [4] [5] Sorted side Unsorted side

32 The Insertion Sort Algorithm [0] [1] [2] [3] [4] [5] Sorted side Unsorted side

33 The Insertion Sort Algorithm Sometimes we are lucky and the new inserted item doesn't need to move at all. [0] [1] [2] [3] [4] [5] Sorted side Unsorted side

34 The Insertionsort Algorithm Sometimes we are lucky twice in a row. [0] [1] [2] [3] [4] [5] Sorted side Unsorted side

35 How to Insert One Element ¶Copy the new element to a separate location. [0] [1] [2] [3] [4] [5] Sorted side Unsorted side

36 How to Insert One Element ·Shift elements in the sorted side, creating an open space for the new element. [0] [1] [2] [3] [4] [5]

37 How to Insert One Element ·Shift elements in the sorted side, creating an open space for the new element. [0] [1] [2] [3] [4] [5]

38 How to Insert One Element ·Continue shifting elements... [0] [1] [2] [3] [4] [5]

39 How to Insert One Element ·Continue shifting elements... [0] [1] [2] [3] [4] [5]

40 How to Insert One Element ·...until you reach the location for the new element. [0] [1] [2] [3] [4] [5]

41 How to Insert One Element ¸Copy the new element back into the array, at the correct location. [0] [1] [2] [3] [4] [5] Sorted side Unsorted side

42 How to Insert One Element The last element must also be inserted. Start by copying it... [0] [1] [2] [3] [4] [5] Sorted side Unsorted side

43 Sorted Result [0] [1] [2] [3] [4] [5]

44 Insertion Sort animation

45 template void insertion_sort(Item data[ ], size_t n) { size_t i, j; Item temp; if(n < 2) return; // nothing to sort!! for(i = 1; i < n; ++i) { // take next item at front of unsorted part of array // and insert it in appropriate location in sorted part of array temp = data[i]; for(j = i; data[j-1] > temp and j > 0; --j) data[j] = data[j-1]; // shift element forward data[j] = temp; }

46 Insertion Sort Time Analysis In O-notation, what is: –Worst case running time for n items? –Average case running time for n items? Steps of algorithm: for i = 1 to n-1 take next key from unsorted part of array insert in appropriate location in sorted part of array: for j = i down to 0, shift sorted elements to the right if key > key[i] increase size of sorted array by 1

47 Insertion Sort Time Analysis In O-notation, what is: –Worst case running time for n items? –Average case running time for n items? Steps of algorithm: for i = 1 to n-1 take next key from unsorted part of array insert in appropriate location in sorted part of array: for j = i down to 0, shift sorted elements to the right if key > key[i] increase size of sorted array by 1 Outer loop: O(n)

48 Insertion Sort Time Analysis In O-notation, what is: –Worst case running time for n items? –Average case running time for n items? Steps of algorithm: for i = 1 to n-1 take next key from unsorted part of array insert in appropriate location in sorted part of array: for j = i down to 0, shift sorted elements to the right if key > key[i] increase size of sorted array by 1 Outer loop: O(n) Inner loop: O(n)

49 template void insertion_sort(Item data[ ], size_t n) { size_t i, j; Item temp; if(n < 2) return; // nothing to sort!! for(i = 1; i < n; ++i) { // take next item at front of unsorted part of array // and insert it in appropriate location in sorted part of array temp = data[i]; for(j = i; data[j-1] > temp and j > 0; --j) data[j] = data[j-1]; // shift element forward data[j] = temp; } O(n)

50 The Bubble Sort Algorithm The Bubble Sort algorithm looks at pairs of entries in the array, and swaps their order if needed. [0] [1] [2] [3] [4] [5]

51 Bubble sort

52 The Bubble Sort Algorithm The Bubble Sort algorithm looks at pairs of entries in the array, and swaps their order if needed. [0] [1] [2] [3] [4] [5] Swap?

53 The Bubble Sort Algorithm The Bubble Sort algorithm looks at pairs of entries in the array, and swaps their order if needed. [0] [1] [2] [3] [4] [5] Yes!

54 The Bubble Sort Algorithm The Bubble Sort algorithm looks at pairs of entries in the array, and swaps their order if needed. [0] [1] [2] [3] [4] [5] Swap?

55 The Bubble Sort Algorithm The Bubble Sort algorithm looks at pairs of entries in the array, and swaps their order if needed. [0] [1] [2] [3] [4] [5] No.

56 The Bubble Sort Algorithm The Bubble Sort algorithm looks at pairs of entries in the array, and swaps their order if needed. [0] [1] [2] [3] [4] [5] Swap?

57 The Bubble Sort Algorithm The Bubble Sort algorithm looks at pairs of entries in the array, and swaps their order if needed. [0] [1] [2] [3] [4] [5] No.

58 The Bubble Sort Algorithm The Bubble Sort algorithm looks at pairs of entries in the array, and swaps their order if needed. [0] [1] [2] [3] [4] [5] Swap?

59 The Bubble Sort Algorithm The Bubble Sort algorithm looks at pairs of entries in the array, and swaps their order if needed. [0] [1] [2] [3] [4] [5] Yes!

60 The Bubble Sort Algorithm The Bubble Sort algorithm looks at pairs of entries in the array, and swaps their order if needed. [0] [1] [2] [3] [4] [5] Swap?

61 The Bubble Sort Algorithm The Bubble Sort algorithm looks at pairs of entries in the array, and swaps their order if needed. [0] [1] [2] [3] [4] [5] Yes!

62 The Bubble Sort Algorithm Repeat. [0] [1] [2] [3] [4] [5] Swap? No.

63 The Bubble Sort Algorithm Repeat. [0] [1] [2] [3] [4] [5] Swap? No.

64 The Bubble Sort Algorithm Repeat. [0] [1] [2] [3] [4] [5] Swap? Yes.

65 The Bubble Sort Algorithm Repeat. [0] [1] [2] [3] [4] [5] Swap? Yes.

66 The Bubble Sort Algorithm Repeat. [0] [1] [2] [3] [4] [5] Swap? Yes.

67 The Bubble Sort Algorithm Repeat. [0] [1] [2] [3] [4] [5] Swap? Yes.

68 The Bubble Sort Algorithm Repeat. [0] [1] [2] [3] [4] [5] Swap? No.

69 The Bubble Sort Algorithm Loop over array n-1 times, swapping pairs of entries as needed. [0] [1] [2] [3] [4] [5] Swap? No.

70 The Bubble Sort Algorithm Loop over array n-1 times, swapping pairs of entries as needed. [0] [1] [2] [3] [4] [5] Swap? Yes.

71 The Bubble Sort Algorithm Loop over array n-1 times, swapping pairs of entries as needed. [0] [1] [2] [3] [4] [5] Swap? Yes.

72 The Bubble Sort Algorithm Loop over array n-1 times, swapping pairs of entries as needed. [0] [1] [2] [3] [4] [5] Swap? Yes.

73 The Bubble Sort Algorithm Loop over array n-1 times, swapping pairs of entries as needed. [0] [1] [2] [3] [4] [5] Swap? Yes.

74 The Bubble Sort Algorithm Loop over array n-1 times, swapping pairs of entries as needed. [0] [1] [2] [3] [4] [5] Swap? No.

75 The Bubble Sort Algorithm Loop over array n-1 times, swapping pairs of entries as needed. [0] [1] [2] [3] [4] [5] Swap? No.

76 The Bubble Sort Algorithm Continue looping, until done. [0] [1] [2] [3] [4] [5] Swap? Yes.

77 Bubble sort animation

78 template void bubble_sort(Item data[ ], size_t n) { size_t i, j; Item temp; if(n < 2) return; // nothing to sort!! for(i = 0; i < n-1; ++i) { for(j = 0; j < n-1;++j) if(data[j] > data[j+1]) // if out of order, swap! { temp = data[j]; data[j] = data[j+1]; data[j+1] = temp; }

79 template void bubble_sort(Item data[ ], size_t n) { size_t i, j; Item temp; bool swapped = true; if(n < 2) return; // nothing to sort!! for(i = 0; swapped and i < n-1; ++i) {// if no elements swapped in an iteration, // then elements are in order: done! for(swapped = false, j = 0; j < n-1;++j) if(data[j] > data[j+1]) // if out of order, swap! { temp = data[j]; data[j] = data[j+1]; data[j+1] = temp; swapped = true; }

80 Bubble Sort Time Analysis In O-notation, what is: –Worst case running time for n items? –Average case running time for n items? Steps of algorithm: for i = 0 to n-1 for j =0 to n-2 if key[j] > key[j+1] then swap if no elements swapped in this pass through array, done. otherwise, continue

81 Bubble Sort Time Analysis In O-notation, what is: –Worst case running time for n items? –Average case running time for n items? Steps of algorithm: for i = 0 to n-1 for j =0 to n-2 if key[j] > key[j+1] then swap if no elements swapped in this pass through array, done. otherwise, continue O(n)

82 Selection Sort, Insertion Sort, and Bubble Sort all have a worst-case time of O(n 2 ), making them impractical for large arrays. But they are easy to understand, easy to program, easy to debug. Insertion Sort also has good performance when the array is nearly sorted to begin with. Timing and Other Issues

83 Next class More sophisticated sorting algorithms are needed when good performance is needed in all cases for large arrays. Next class: Merge Sort, Heap Sort. –O(N log N) Review the “recursion algorithm” and “heap structure” we discussed last week.

84 Merge sort

85 1. Partitioning: Divide the unsorted list into n sublists, each containing 1 element (a list of 1 element is considered sorted). 2. Merging: Recursively merge sublists to produce new sorted sublists until there is only 1 sublist remaining. This will be the sorted list. Recursion

86 86 52471326 52471326 52471326

87 87 0 0 0

88 Main Index Contents 88 The Merge Algorithm Example The merge algorithm takes a sequence of elements in a vector v having index range [first, last). The sequence consists of two ordered sublists separated by an intermediate index, called mid.

89 Main Index Contents 89 Main Index Contents The Merge Algorithm… (Cont…)

90 Main Index Contents 90 Main Index Contents The Merge Algorithm… (Cont…)

91 Main Index Contents 91 The Merge Algorithm… (Cont…)

92 Merge sort animation

93 mergeSort()

94 Main Index Contents 94 Main Index Contents Partitioning and Merging of Sublists in mergeSort()

95 Complexity of merge sort

96 Worst, best, average case Complexity: O(n log n) Worst, best, average case Complexity: O(n log n)

97 Heap sort

98 Heaps For a maximum heap, the value of a parent is greater than or equal to the value of each of its children. For a minimum heap, the value of the parent is less than or equal to the value of each of its children. We assume that a heap is a maximum heap.

99 Heap sort 1. Build a heap of N elements – the maximum element is at the top of the heap 2. Perform N Delete operations – the elements are extracted in sorted order

100 Heap sort animation

101 Heap sort – Running Time Analysis 1. Build a binary heap of N elements  O(N), see slide 21 of lecture ‘heap’ 2. Perform N Delete operations – Each Delete operation takes O(log N), See slide 23 of lecture ‘heap’  Total: O(N log N) Total time complexity: O(N) + O(N log N)  O(N log N)

102 Reading Chapter 7


Download ppt "Week 11 Sorting Algorithms. Sorting Sorting Algorithms A sorting algorithm is an algorithm that puts elements of a list in a certain order. We need sorting."

Similar presentations


Ads by Google