Presentation is loading. Please wait.

Presentation is loading. Please wait.

Computer Science 1620 Sorting. cases exist where we would like our data to be in ascending (descending order) binary searching printing purposes selection.

Similar presentations


Presentation on theme: "Computer Science 1620 Sorting. cases exist where we would like our data to be in ascending (descending order) binary searching printing purposes selection."— Presentation transcript:

1 Computer Science 1620 Sorting

2 cases exist where we would like our data to be in ascending (descending order) binary searching printing purposes selection (e.g. select 5 students with highest gpas) etc the sorting problem given a list of non-sorted data, rearrange the data into ascending or descending order

3 Sorting 8524634517319650 Sort 8524634517319650

4 Sorting a well-studied area of Computer Science many clever algorithms exist Simple: bubble sort selection sort insertion sort Complex: merge sort quick sort (very popular!!) We will focus on the simple sorting algorithms, as the complex sorts involve recursion.

5 Sorting – the general idea the sort will be conducted using a series of swaps (discussed on next slide) we will not use any extra arrays inline the quality of a sorting algorithm is typically based on two metrics: # of comparisons # of swaps

6 Simple Sorting The swap procedure swap(a[i], a[j]) swaps the elements i and j in a, so that a[i] becomes a[j] and vice versa eg. swap(a[3], a[5]) provided by algorithm header file #include 8524634517319650 8524633117459650 Before After

7 Bubble Sort a very simple sorting algorithm the basic idea: when two adjacent items are found to be out of order, swap them adjacent in this case means two items that are side by side in an array (a[1], a[2]) or (b[i], b[i+1])

8 Bubble Sort – Algorithm Step 1: Repeat while items in list are out of order Step 1.1: Check each pair of adjacent elements (that is, (1,2), (2,3), (3,4), and so on) Step 1.1.1 If a pair of adjacent elements are found to be out of order, swap them

9 Bubble Sort 8524634517319650 Step 1: Repeat while items in list are out of order Step 1.1: Check each pair of adjacent elements Step 1.1.1 If a pair of adjacent elements are found to be out of order, swap them

10 Bubble Sort 8524634517319650 Step 1: Repeat while items in list are out of order Step 1.1: Check each pair of adjacent elements Step 1.1.1 If a pair of adjacent elements are found to be out of order, swap them Out of order, so we swap

11 Bubble Sort 8563244517319650 Step 1: Repeat while items in list are out of order Step 1.1: Check each pair of adjacent elements Step 1.1.1 If a pair of adjacent elements are found to be out of order, swap them Out of order, so we swap

12 Bubble Sort 8545246317319650 Step 1: Repeat while items in list are out of order Step 1.1: Check each pair of adjacent elements Step 1.1.1 If a pair of adjacent elements are found to be out of order, swap them Out of order, so we swap

13 Bubble Sort 8517246345319650 Step 1: Repeat while items in list are out of order Step 1.1: Check each pair of adjacent elements Step 1.1.1 If a pair of adjacent elements are found to be out of order, swap them Out of order, so we swap

14 Bubble Sort 8531246345179650 Step 1: Repeat while items in list are out of order Step 1.1: Check each pair of adjacent elements Step 1.1.1 If a pair of adjacent elements are found to be out of order, swap them Out of order, so we swap

15 Bubble Sort 8596246345173150 Step 1: Repeat while items in list are out of order Step 1.1: Check each pair of adjacent elements Step 1.1.1 If a pair of adjacent elements are found to be out of order, swap them In order, so we don't swap

16 Bubble Sort 9650246345173185 Step 1: Repeat while items in list are out of order Step 1.1: Check each pair of adjacent elements Step 1.1.1 If a pair of adjacent elements are found to be out of order, swap them Out of order, so we swap.

17 Bubble Sort 5096246345173185 Step 1: Repeat while items in list are out of order Step 1.1: Check each pair of adjacent elements Step 1.1.1 If a pair of adjacent elements are found to be out of order, swap them Are items still out of order? yes, so we repeat

18 Bubble Sort 2463451731855096 Step 1: Repeat while items in list are out of order Step 1.1: Check each pair of adjacent elements Step 1.1.1 If a pair of adjacent elements are found to be out of order, swap them In order, no swap

19 Bubble Sort 6345241731855096 Step 1: Repeat while items in list are out of order Step 1.1: Check each pair of adjacent elements Step 1.1.1 If a pair of adjacent elements are found to be out of order, swap them Out of order, so we swap

20 Bubble Sort 6317244531855096 Step 1: Repeat while items in list are out of order Step 1.1: Check each pair of adjacent elements Step 1.1.1 If a pair of adjacent elements are found to be out of order, swap them Out of order, so we swap

21 Bubble Sort 6331244517855096 Step 1: Repeat while items in list are out of order Step 1.1: Check each pair of adjacent elements Step 1.1.1 If a pair of adjacent elements are found to be out of order, swap them Out of order, so we swap

22 Bubble Sort 6385244517315096 Step 1: Repeat while items in list are out of order Step 1.1: Check each pair of adjacent elements Step 1.1.1 If a pair of adjacent elements are found to be out of order, swap them In order, no swap

23 Bubble Sort 8550244517316396 Step 1: Repeat while items in list are out of order Step 1.1: Check each pair of adjacent elements Step 1.1.1 If a pair of adjacent elements are found to be out of order, swap them Out of order, so we swap.

24 Bubble Sort 5085244517316396 Step 1: Repeat while items in list are out of order Step 1.1: Check each pair of adjacent elements Step 1.1.1 If a pair of adjacent elements are found to be out of order, swap them In order, no swap

25 Bubble Sort 5085244517316396 Step 1: Repeat while items in list are out of order Step 1.1: Check each pair of adjacent elements Step 1.1.1 If a pair of adjacent elements are found to be out of order, swap them Are items still out of order? yes, so we repeat

26 Bubble Sort 2445173163508596 Step 1: Repeat while items in list are out of order Step 1.1: Check each pair of adjacent elements Step 1.1.1 If a pair of adjacent elements are found to be out of order, swap them In order, no swap

27 Bubble Sort 243163508596 Step 1: Repeat while items in list are out of order Step 1.1: Check each pair of adjacent elements Step 1.1.1 If a pair of adjacent elements are found to be out of order, swap them Out of order, so we swap 4517

28 31 Bubble Sort 241763508596 Step 1: Repeat while items in list are out of order Step 1.1: Check each pair of adjacent elements Step 1.1.1 If a pair of adjacent elements are found to be out of order, swap them Out of order, so we swap 45

29 63 Bubble Sort 241731508596 Step 1: Repeat while items in list are out of order Step 1.1: Check each pair of adjacent elements Step 1.1.1 If a pair of adjacent elements are found to be out of order, swap them In order, no swap. 45

30 50 Bubble Sort 241731458596 Step 1: Repeat while items in list are out of order Step 1.1: Check each pair of adjacent elements Step 1.1.1 If a pair of adjacent elements are found to be out of order, swap them Out of order, so we swap 63

31 Bubble Sort 241731458596 Step 1: Repeat while items in list are out of order Step 1.1: Check each pair of adjacent elements Step 1.1.1 If a pair of adjacent elements are found to be out of order, swap them In order, no swap 50

32 63 Bubble Sort 241731458596 Step 1: Repeat while items in list are out of order Step 1.1: Check each pair of adjacent elements Step 1.1.1 If a pair of adjacent elements are found to be out of order, swap them In order, no swap 50

33 63 Bubble Sort 241731458596 Step 1: Repeat while items in list are out of order Step 1.1: Check each pair of adjacent elements Step 1.1.1 If a pair of adjacent elements are found to be out of order, swap them 50 Are items still out of order? yes, so we repeat

34 63 Bubble Sort 31458596 Step 1: Repeat while items in list are out of order Step 1.1: Check each pair of adjacent elements Step 1.1.1 If a pair of adjacent elements are found to be out of order, swap them 50 Out of order, so we swap 2417

35 63 Bubble Sort 31458596 Step 1: Repeat while items in list are out of order Step 1.1: Check each pair of adjacent elements Step 1.1.1 If a pair of adjacent elements are found to be out of order, swap them 50 In order, no swap 1724

36 63 Bubble Sort 31458596 Step 1: Repeat while items in list are out of order Step 1.1: Check each pair of adjacent elements Step 1.1.1 If a pair of adjacent elements are found to be out of order, swap them 50 In order, no swap 1724

37 63 Bubble Sort 31458596 Step 1: Repeat while items in list are out of order Step 1.1: Check each pair of adjacent elements Step 1.1.1 If a pair of adjacent elements are found to be out of order, swap them 50 In order, no swap 1724

38 63 Bubble Sort 31458596 Step 1: Repeat while items in list are out of order Step 1.1: Check each pair of adjacent elements Step 1.1.1 If a pair of adjacent elements are found to be out of order, swap them 50 In order, no swap 1724

39 63 Bubble Sort 31458596 Step 1: Repeat while items in list are out of order Step 1.1: Check each pair of adjacent elements Step 1.1.1 If a pair of adjacent elements are found to be out of order, swap them 50 In order, no swap 1724

40 63 Bubble Sort 31458596 Step 1: Repeat while items in list are out of order Step 1.1: Check each pair of adjacent elements Step 1.1.1 If a pair of adjacent elements are found to be out of order, swap them 50 In order, no swap 1724

41 63 Bubble Sort 31458596 Step 1: Repeat while items in list are out of order Step 1.1: Check each pair of adjacent elements Step 1.1.1 If a pair of adjacent elements are found to be out of order, swap them 501724 Are items still out of order? no, we are done

42 Bubble Sort guaranteed to sort our data requires up to n passes through the array

43 #include using namespace std; void bubble_sort(int a[], int sz) { Step 1: Repeat while items in list are out of order Step 1.1: Check each pair of adjacent elements Step 1.1.1 If a pair of adjacent elements are found to be out of order, swap them }

44 #include using namespace std; void bubble_sort(int a[], int sz) { Step 1: Repeat while items in list are out of order Step 1.1: Check each pair of adjacent elements Step 1.1.1 If a pair of adjacent elements are found to be out of order, swap them }

45 #include using namespace std; void bubble_sort(int a[], int sz) { while (items in list are out of order) { Step 1.1: Check each pair of adjacent elements Step 1.1.1 If a pair of adjacent elements are found to be out of order, swap them }

46 #include using namespace std; void bubble_sort(int a[], int sz) { while (items in list are out of order) { Step 1.1: Check each pair of adjacent elements Step 1.1.1 If a pair of adjacent elements are found to be out of order, swap them }

47 #include using namespace std; void bubble_sort(int a[], int sz) { while (items in list are out of order) { Step 1.1: for each pair of adjacent elements Step 1.1.1 If these items out of order, swap }

48 #include using namespace std; void bubble_sort(int a[], int sz) { while (items in list are out of order) { for (int j = 0; ; j++) { Step 1.1.1 If these items out of order, swap }

49 #include using namespace std; void bubble_sort(int a[], int sz) { while (items in list are out of order) { for (int j = 0; ; j++) { Step 1.1.1 If these items out of order, swap }

50 #include using namespace std; void bubble_sort(int a[], int sz) { while (items in list are out of order) { for (int j = 0; ; j++) { Step 1.1.1 If these items out of order Step 1.1.1.1 swap the two items }

51 #include using namespace std; void bubble_sort(int a[], int sz) { while (items in list are out of order) { for (int j = 0; ; j++) { Step 1.1.1 If these items out of order Step 1.1.1.1 swap the two items }

52 #include using namespace std; void bubble_sort(int a[], int sz) { while (items in list are out of order) { for (int j = 0; ; j++) { if (a[j] > a[j+1]) { Step 1.1.1.1 swap the two items }

53 #include using namespace std; void bubble_sort(int a[], int sz) { while (items in list are out of order) { for (int j = 0; ; j++) { if (a[j] > a[j+1]) { Step 1.1.1.1 swap the two items }

54 #include using namespace std; void bubble_sort(int a[], int sz) { while (items in list are out of order) { for (int j = 0; ; j++) { if (a[j] > a[j+1]) { swap(a[j], a[j+1]); }

55 #include using namespace std; void bubble_sort(int a[], int sz) { while (items in list are out of order) { for (int j = 0; ; j++) { if (a[j] > a[j+1]) { swap(a[j], a[j+1]); } What is condition here?

56 #include using namespace std; void bubble_sort(int a[], int sz) { while (items in list are out of order) { for (int j = 0; j < sz; j++) { if (a[j] > a[j+1]) { swap(a[j], a[j+1]); } Will this work?

57 #include using namespace std; void bubble_sort(int a[], int sz) { while (items in list are out of order) { for (int j = 0; j < sz - 1; j++) { if (a[j] > a[j+1]) { swap(a[j], a[j+1]); }

58 #include using namespace std; void bubble_sort(int a[], int sz) { while (items in list are out of order) { for (int j = 0; j < sz - 1; j++) { if (a[j] > a[j+1]) { swap(a[j], a[j+1]); }

59 How do we know if the list is out of order? Question: What happens in our "check" loop when all of the items are in order?

60 63 Bubble Sort 31458596 Step 1: Repeat while items in list are out of order Step 1.1: Check each pair of adjacent elements Step 1.1.1 If a pair of adjacent elements are found to be out of order, swap them 50 In order, no swap 1724

61 63 Bubble Sort 31458596 Step 1: Repeat while items in list are out of order Step 1.1: Check each pair of adjacent elements Step 1.1.1 If a pair of adjacent elements are found to be out of order, swap them 50 In order, no swap 1724

62 63 Bubble Sort 31458596 Step 1: Repeat while items in list are out of order Step 1.1: Check each pair of adjacent elements Step 1.1.1 If a pair of adjacent elements are found to be out of order, swap them 50 In order, no swap 1724

63 63 Bubble Sort 31458596 Step 1: Repeat while items in list are out of order Step 1.1: Check each pair of adjacent elements Step 1.1.1 If a pair of adjacent elements are found to be out of order, swap them 50 In order, no swap 1724

64 63 Bubble Sort 31458596 Step 1: Repeat while items in list are out of order Step 1.1: Check each pair of adjacent elements Step 1.1.1 If a pair of adjacent elements are found to be out of order, swap them 50 In order, no swap 1724

65 63 Bubble Sort 31458596 Step 1: Repeat while items in list are out of order Step 1.1: Check each pair of adjacent elements Step 1.1.1 If a pair of adjacent elements are found to be out of order, swap them 50 In order, no swap 1724

66 63 Bubble Sort 31458596 Step 1: Repeat while items in list are out of order Step 1.1: Check each pair of adjacent elements Step 1.1.1 If a pair of adjacent elements are found to be out of order, swap them 50 In order, no swap 1724

67 What did you notice? no swaps took place our list is sorted if during our check loop, no swaps took place during our check loop, if a swap occurs, then remember this use a boolean variable initially false true if a swap occurs

68 #include using namespace std; void bubble_sort(int a[], int sz) { while (items in list are out of order) { for (int j = 0; j < sz - 1; j++) { if (a[j] > a[j+1]) { swap(a[j], a[j+1]); }

69 #include using namespace std; void bubble_sort(int a[], int sz) { bool swapped = true; while (swapped) { swapped = false; for (int j = 0; j < sz - 1; j++) { if (a[j] > a[j+1]) { swap(a[j], a[j+1]); swapped = true; }

70 Why do I initially set swapped to true to guarantee that the while loop executes at least once what is another way to guarantee this? make it a do-while loop

71 #include using namespace std; void bubble_sort(int a[], int sz) { bool swapped; do { swapped = false; for (int j = 0; j < sz - 1; j++) { if (a[j] > a[j+1]) { swap(a[j], a[j+1]); swapped = true; } } while (swapped); }

72 An Improvement to Bubble Sort this initial version of bubble sort works fine how many comparisons does it make? each time through the "check" loop: n-1 how many times does it go through the check loop: n (in the worst case) therefore, it can make up to n(n-1) comparisons

73 Better Bubble Sort what do you notice at the end of each "check" loop

74 Bubble Sort 5096246345173185 After first time through loop

75 Bubble Sort 5085244517316396 After second time through loop

76 63 Bubble Sort 241731458596 After third time through loop 50

77 Each time through the loop, the ith largest value gets put in its final position first time through the loop, largest item placed in last spot second time through the loop, second largest item placed in second-last spot ith time through the loop, ith largest item placed in ith largest spot

78 What does this mean? on the second pass through the loop, do I need to compare the last item? no – it's the largest item, so belongs in the last spot this means that each time through the loop, the number of items to sort gets smaller

79 Bubble Sort 5096246345173185 After first time through loop Only these elements need to be sorted

80 Bubble Sort 5085244517316396 After second time through loop Only these elements need to be sorted

81 63 Bubble Sort 241731458596 After third time through loop 50 Only these elements need to be sorted

82 #include using namespace std; void bubble_sort(int a[], int sz) { bool swapped; do { swapped = false; for (int j = 0; j < sz - 1; j++) { if (a[j] > a[j+1]) { swap(a[j], a[j+1]); swapped = true; } } while (swapped); }

83 #include using namespace std; void bubble_sort(int a[], int sz) { bool swapped; do { swapped = false; for (int j = 0; j < sz - 1; j++) { if (a[j] > a[j+1]) { swap(a[j], a[j+1]); swapped = true; } sz--; } while (swapped); }

84 How good is this simple fix? how many comparisons: first time through the loop: n – 1 second time through the loop: n-2 ith time through the loop: n-i Number of comparisons in the worst case:

85 Hence: this very small extension to the bubble sort algorithm improves the # of comparisons by 50%

86 Bubble Sort – Problems suppose you are sorting an array of strings each time you call swap, the following must occur* copy first string to temporary storage copy second string value to first string copy temporary storage value to second string if strings are big, this can be costly for time ie. swap can be expensive

87 Bubble Sort what makes bubble sort so "expensive" in terms of computation is the number of swaps that it does number of comparisons: n(n-1)/2 in worst case number of swaps: n(n-1)/2 in worst case

88 Selection Sort suppose my items are arranged like this what will the first "check" pass of bubble sort look like? 9624634517318550

89 Bubble Sort 9624634517318550 Step 1: Repeat while items in list are out of order Step 1.1: Check each pair of adjacent elements Step 1.1.1 If a pair of adjacent elements are found to be out of order, swap them

90 Bubble Sort 9624634517318550 Step 1: Repeat while items in list are out of order Step 1.1: Check each pair of adjacent elements Step 1.1.1 If a pair of adjacent elements are found to be out of order, swap them Out of order, so we swap

91 Bubble Sort 9663244517318550 Step 1: Repeat while items in list are out of order Step 1.1: Check each pair of adjacent elements Step 1.1.1 If a pair of adjacent elements are found to be out of order, swap them Out of order, so we swap

92 Bubble Sort 9645246317318550 Step 1: Repeat while items in list are out of order Step 1.1: Check each pair of adjacent elements Step 1.1.1 If a pair of adjacent elements are found to be out of order, swap them Out of order, so we swap

93 Bubble Sort 9617246345318550 Step 1: Repeat while items in list are out of order Step 1.1: Check each pair of adjacent elements Step 1.1.1 If a pair of adjacent elements are found to be out of order, swap them Out of order, so we swap

94 Bubble Sort 9631246345178550 Step 1: Repeat while items in list are out of order Step 1.1: Check each pair of adjacent elements Step 1.1.1 If a pair of adjacent elements are found to be out of order, swap them Out of order, so we swap

95 Bubble Sort 9685246345173150 Step 1: Repeat while items in list are out of order Step 1.1: Check each pair of adjacent elements Step 1.1.1 If a pair of adjacent elements are found to be out of order, swap them In order, so we don't swap

96 Bubble Sort 9650246345173185 Step 1: Repeat while items in list are out of order Step 1.1: Check each pair of adjacent elements Step 1.1.1 If a pair of adjacent elements are found to be out of order, swap them Out of order, so we swap.

97 Bubble Sort 5096246345173185 Step 1: Repeat while items in list are out of order Step 1.1: Check each pair of adjacent elements Step 1.1.1 If a pair of adjacent elements are found to be out of order, swap them

98 What did you notice? the biggest number (96) got placed in the last spot (discussed last day) however, the number gets "dragged" along by a number of swaps what if, instead of dragging it to its final location, I swapped it directly there?

99 Selection Sort Algorithm: Step 1: for each i between 1 and n Step 1.1 find the i-th smallest number in the array Step 1.2 swap that number in the i-th spot in the array in other words find the smallest element, swap it into the first spot find the 2 nd smallest element, swap it into the 2 nd spot find the 3 rd smallest element, swap it into the 3 rd lspot

100 Selection Sort 962463451731 Step 1: for each i between 1 and n Step 1.1 find the i-th smallest number in the array Step 1.2 swap that number in the i-th spot in the array 8550

101 Selection Sort 962463451731 Step 1: for each i between 1 and n Step 1.1 find the i-th smallest number in the array Step 1.2 swap that number in the i-th spot in the array 8550 smallest

102 Selection Sort 962463458531 Step 1: for each i between 1 and n Step 1.1 find the i-th smallest number in the array Step 1.2 swap that number in the i-th spot in the array 1750 2 nd smallest

103 Selection Sort 962463458531 Step 1: for each i between 1 and n Step 1.1 find the i-th smallest number in the array Step 1.2 swap that number in the i-th spot in the array 1750 3 rd smallest

104 Selection Sort 962431458563 Step 1: for each i between 1 and n Step 1.1 find the i-th smallest number in the array Step 1.2 swap that number in the i-th spot in the array 1750 4 th smallest

105 Selection Sort 962431458563 Step 1: for each i between 1 and n Step 1.1 find the i-th smallest number in the array Step 1.2 swap that number in the i-th spot in the array 1750 5 th smallest

106 Selection Sort 962431455063 Step 1: for each i between 1 and n Step 1.1 find the i-th smallest number in the array Step 1.2 swap that number in the i-th spot in the array 1785 6 th smallest

107 Selection Sort 962431455063 Step 1: for each i between 1 and n Step 1.1 find the i-th smallest number in the array Step 1.2 swap that number in the i-th spot in the array 1785 7 th smallest

108 Selection Sort 852431455063 Step 1: for each i between 1 and n Step 1.1 find the i-th smallest number in the array Step 1.2 swap that number in the i-th spot in the array 1796 8 th smallest

109 #include using namespace std; void selection_sort(int a[], int n) { Step 1: for each i between 1 and n Step 1.1 find the i-th smallest number in the array Step 1.2 swap that number in the i-th spot in the array }

110 #include using namespace std; void selection_sort(int a[], int n) { Step 1: for each i between 1 and n Step 1.1 find the i-th smallest number in the array Step 1.2 swap that number in the i-th spot in the array }

111 #include using namespace std; void selection_sort(int a[], int n) { for (int i = 1; i <= n; i++) { Step 1.1 find the i-th smallest number in the array Step 1.2 swap that number in the i-th spot in the array } Will this work?

112 #include using namespace std; void selection_sort(int a[], int n) { for (int i = 0; i < n; i++) { Step 1.1 find the i-th smallest number in the array Step 1.2 swap that number in the i-th spot in the array } Remember: arrays are ordered 0 to n-1, not 1 to n

113 #include using namespace std; void selection_sort(int a[], int n) { for (int i = 0; i < n; i++) { Step 1.1 find the i-th smallest number in the array Step 1.2 swap that number in the i-th spot in the array }

114 #include using namespace std; void selection_sort(int a[], int n) { for (int i = 0; i < n; i++) { int ith = (i-th smallest number in the array); Step 1.2 swap that number in the i-th spot in the array }

115 #include using namespace std; void selection_sort(int a[], int n) { for (int i = 0; i < n; i++) { int ith = (i-th smallest number in the array); Step 1.2 swap that number in the i-th spot in the array }

116 #include using namespace std; void selection_sort(int a[], int n) { for (int i = 0; i < n; i++) { int ith = (i-th smallest number in the array); swap(a[ith], a[i]); }

117 #include using namespace std; void selection_sort(int a[], int n) { for (int i = 0; i < n; i++) { int ith = (i-th smallest number in the array); swap(a[ith], a[i]); } How do we find the ith smallest?

118 Let's simplify this: how do we set ith to point to the smallest number? One solution: assume smallest is at location 0 loop through the other elements if you find a smaller number at another location, remember that location

119 #include using namespace std; void selection_sort(int a[], int n) { for (int i = 0; i < n; i++) { int ith = (i-th smallest number in the array); swap(a[ith], a[i]); } assume smallest is at location 0 loop through the other elements if you find a smaller number at another location, remember that location

120 #include using namespace std; void selection_sort(int a[], int n) { for (int i = 0; i < n; i++) { int ith = 0; swap(a[ith], a[i]); } assume smallest is at location 0 loop through the other elements if you find a smaller number at another location, remember that location

121 #include using namespace std; void selection_sort(int a[], int n) { for (int i = 0; i < n; i++) { int ith = 0; for (int j = 1; j < n; j++) { } swap(a[ith], a[i]); } assume smallest is at location 0 loop through the other elements if you find a smaller number at another location, remember that location

122 #include using namespace std; void selection_sort(int a[], int n) { for (int i = 0; i < n; i++) { int ith = 0; for (int j = 1; j < n; j++) { if (a[j] < a[ith]) { } swap(a[ith], a[i]); } assume smallest is at location 0 loop through the other elements if you find a bigger number at another location, remember that location

123 #include using namespace std; void selection_sort(int a[], int n) { for (int i = 0; i < n; i++) { int ith = 0; for (int j = 1; j < n; j++) { if (a[j] < a[ith]) { ith = j; } swap(a[ith], a[i]); } assume smallest is at location 0 loop through the other elements if you find a bigger number at another location, remember that location

124 This works fine for finding the smallest but what about finding the 2 nd smallest, or 3 rd smallest, etc? let's consider what the array looks like after the ith smallest element is found

125 Bubble Sort 962463451731 Our array, initially 8550

126 Bubble Sort 962463458531 After smallest is found 1750

127 Bubble Sort 962463458531 After second smallest is found 1750

128 Bubble Sort 962431458563 After third smallest is found 1750

129 Bubble Sort 962431458563 After third smallest is found 1750

130 What do you notice? after the smallest is found, it is placed in location 0 this means that the 2 nd smallest item is the smallest item between location 1 and location n-1 after the 2 nd smallest is found, it is placed in location 1 this means that the 3 rd smallest item is the smallest item between location 2 and location n-1

131 Bubble Sort 962463451731 Our array, initially 8550 Find smallest in this set

132 Bubble Sort 962463458531 After smallest is found 1750 Find smallest in this set

133 Bubble Sort 962463458531 After second smallest is found 1750 Find smallest in this set

134 Bubble Sort 962431458563 After third smallest is found 1750 Find smallest in this set

135 In other words we only need to search the elements beginning at i

136 #include using namespace std; void selection_sort(int a[], int n) { for (int i = 0; i < n; i++) { int ith = 0; for (int j = 1; j < n; j++) { if (a[j] < a[ith]) { ith = j; } swap(a[ith], a[i]); }

137 #include using namespace std; void selection_sort(int a[], int n) { for (int i = 0; i < n; i++) { int ith = i; for (int j = i+1; j < n; j++) { if (a[j] < a[ith]) { ith = j; } swap(a[ith], a[i]); }

138 A Couple of Minor Improvements 1) We do not need to do the last pass through the outer loop 2) We do not need to swap if i == ith

139 #include using namespace std; void selection_sort(int a[], int n) { for (int i = 0; i < n-1; i++) { int ith = i; for (int j = i+1; j < n; j++) { if (a[j] < a[ith]) { ith = j; } if (i != ith) swap(a[ith], a[i]); }

140 Selection Sort how many comparisons does selection sort make? first pass through loop: n-1 second pass through loop: n-2 third pass through loop: n-3 …. n-1 pass through loop: 1

141 Selection Sort just as many compares as bubble sort how many swaps (worst case)? N -1 this is a big improvement over bubble sort, which required n(n-1)/2 swaps

142 Insertion Sort nested loop, just like selection sort outer loop moves one at a time through each element of the array at each element "picks up" element out of the array places the element in its corresponding spot behind this index result when outer loop is at index i, everything behind index i is sorted

143 Insertion Sort Example: 8524634517319650

144 Insertion Sort – Outer Loop loop through all elements one at a time starting at 2 nd element 8524634517319650 current

145 Insertion Sort – Outer Loop Step 1: take current element out of the array 85 24 634517319650 current

146 Insertion Sort – Outer Loop Step 2: shift elements behind this element to the right stop when element to be shifted is less than current element or nothing left to shift 85 24 634517319650 current

147 Insertion Sort – Outer Loop Step 2: shift elements behind this element to the right stop when element to be shifted is less than current element 85 > 24, so it's shifted 85 24 634517319650 current

148 Insertion Sort – Outer Loop Step 2: shift elements behind this element to the right stop when element to be shifted is less than current element nothing left to shift, so stop 24 634517319650 current 85

149 Insertion Sort – Outer Loop Step 3: place the current element in the empty spot created by shifting 24 634517319650 current 85

150 Insertion Sort – Outer Loop Step 3: place the current element in the empty spot created by shifting 24634517319650 current 85

151 Insertion Sort – Outer Loop Step 4: move to next element in list, and being process again 24634517319650 current 85

152 Insertion Sort – Outer Loop Step 1: take current element out of the array 24 63 4517319650 current 85

153 Insertion Sort – Outer Loop Step 2: shift elements behind this element to the right stop when element to be shifted is less than current element 85 > 63, so shift 24 63 4517319650 current 85

154 Insertion Sort – Outer Loop Step 2: shift elements behind this element to the right stop when element to be shifted is less than current element 24 < 63, so stop! 24 63 4517319650 current 85

155 Insertion Sort – Outer Loop Step 3: place the current element in the empty spot created by shifting 244517319650 current 8563

156 Insertion Sort – Outer Loop Step 4: move to next element in list, and being process again 244517319650 current 8563

157 Insertion Sort – Outer Loop Step 1: take current element out of the array 24 45 17319650 current 8563

158 Insertion Sort – Outer Loop Step 2: shift elements behind this element to the right stop when element to be shifted is less than current element 85 > 45, so shift 24 45 17319650 current 8563

159 Insertion Sort – Outer Loop Step 2: shift elements behind this element to the right stop when element to be shifted is less than current element 63 > 45, so shift 24 45 17319650 current 8563

160 Insertion Sort – Outer Loop Step 2: shift elements behind this element to the right stop when element to be shifted is less than current element 24 < 45, so stop! 24 45 17319650 current 8563

161 Insertion Sort – Outer Loop Step 3: place the current element in the empty spot created by shifting 244517319650 current 8563

162 Insertion Sort – Outer Loop Step 4: move to next element in list, and being process again 244517319650 current 8563

163 Insertion Sort – Outer Loop Step 1: take current element out of the array 2445 17 319650 current 8563

164 Insertion Sort – Outer Loop Step 2: shift elements behind this element to the right stop when element to be shifted is less than current element 85 > 17, so shift 2445 17 319650 current 8563

165 Insertion Sort – Outer Loop Step 2: shift elements behind this element to the right stop when element to be shifted is less than current element 63 > 17, so shift 2445 17 319650 current 8563

166 Insertion Sort – Outer Loop Step 2: shift elements behind this element to the right stop when element to be shifted is less than current element 45 > 17, so shift 2445 17 319650 current 8563

167 Insertion Sort – Outer Loop Step 2: shift elements behind this element to the right stop when element to be shifted is less than current element 24 > 17, so shift 2445 17 319650 current 8563

168 Insertion Sort – Outer Loop Step 2: shift elements behind this element to the right stop when element to be shifted is less than current element nothing left to shift, so stop! 2445 17 319650 current 8563

169 Insertion Sort – Outer Loop Step 3: place the current element in the empty spot created by shifting 244517319650 current 8563

170 Insertion Sort – Outer Loop Step 4: move to next element in list, and being process again 244517319650 current 8563

171 Insertion Sort – Outer Loop And so on notice that the list to the left of current is always sorted 244517 319650 current 8563

172 How do we code this? tricky Algorithm: for each element (starting at 2 nd ) take element out of array Shift elements behind this element to the right, stopping when element to be shifted is less than current element, or nothing left to shift place the current element in the empty spot created by shifting

173 #include using namespace std; void insertion_sort(int a[], int n) { Step 1: for each element (starting at 2nd) Step 1.1 Take element out of array Step 1.2 Shift elements behind this element to the right, stopping when element to be shifted is less than current element, or nothing left to shift Step 1.3 place the current element in the empty spot created by shifting }

174 #include using namespace std; void insertion_sort(int a[], int n) { Step 1: for each element (starting at 2nd) Step 1.1 Take element out of array Step 1.2 Shift elements behind this element to the right, stopping when element to be shifted is less than current element, or nothing left to shift Step 1.3 place the current element in the empty spot created by shifting }

175 #include using namespace std; void insertion_sort(int a[], int n) { for (int i = 1; i < n; i++) { Step 1.1 Take element out of array Step 1.2 Shift elements behind this element to the right, stopping when element to be shifted is less than current element, or nothing left to shift Step 1.3 place the current element in the empty spot created by shifting }

176 #include using namespace std; void insertion_sort(int a[], int n) { for (int i = 1; i < n; i++) { Step 1.1 Take element out of array Step 1.2 Shift elements behind this element to the right, stopping when element to be shifted is less than current element, or nothing left to shift Step 1.3 place the current element in the empty spot created by shifting }

177 #include using namespace std; void insertion_sort(int a[], int n) { for (int i = 1; i < n; i++) { int element = a[i]; Step 1.2 Shift elements behind this element to the right, stopping when element to be shifted is less than current element, or nothing left to shift Step 1.3 place the current element in the empty spot created by shifting } NOTE: This doesn't take the actual number out of the array – just a copy of it.

178 #include using namespace std; void insertion_sort(int a[], int n) { for (int i = 1; i < n; i++) { int element = a[i]; Step 1.2 Shift elements behind this element to the right, stopping when element to be shifted is less than current element, or nothing left to shift Step 1.3 place the current element in the empty spot created by shifting }

179 #include using namespace std; void insertion_sort(int a[], int n) { for (int i = 1; i < n; i++) { int element = a[i]; int j = i; while ( ) { a[j] = a[j-1]; j--; } Step 1.3 place the current element in the empty spot created by shifting }

180 #include using namespace std; void insertion_sort(int a[], int n) { for (int i = 1; i < n; i++) { int element = a[i]; int j = i; while ( ) { a[j] = a[j-1]; j--; } Step 1.3 place the current element in the empty spot created by shifting }

181 #include using namespace std; void insertion_sort(int a[], int n) { for (int i = 1; i < n; i++) { int element = a[i]; int j = i; while ( ) { a[j] = a[j-1]; j--; } a[j] = element; }

182 #include using namespace std; void insertion_sort(int a[], int n) { for (int i = 1; i < n; i++) { int element = a[i]; int j = i; while ( ) { a[j] = a[j-1]; j--; } a[j] = element; } We want to loop until there is nothing left to shift the item to be shifted (a[j-1]) is less than element

183 #include using namespace std; void insertion_sort(int a[], int n) { for (int i = 1; i < n; i++) { int element = a[i]; int j = i; while ( ) { a[j] = a[j-1]; j--; } a[j] = element; } We want to loop until there is nothing left to shift the item to be shifted (a[j-1]) is less than element

184 #include using namespace std; void insertion_sort(int a[], int n) { for (int i = 1; i < n; i++) { int element = a[i]; int j = i; while ( (j > 0) ) { a[j] = a[j-1]; j--; } a[j] = element; } We want to loop until there is nothing left to shift the item to be shifted (a[j-1]) is less than element

185 #include using namespace std; void insertion_sort(int a[], int n) { for (int i = 1; i < n; i++) { int element = a[i]; int j = i; while ( (j > 0) && (a[j-1] > element) ) { a[j] = a[j-1]; j--; } a[j] = element; } We want to loop until there is nothing left to shift the item to be shifted (a[j-1]) is less than element

186 And so on What is the complexity of insertion sort? How many comparisons: First pass: 1 comparisons Second pass: 2 comparisons … ith passi comparisons n-1 passn-1 comparison

187 Swaps how many swaps? no swaps – just shifts shifting takes 1/3 as much work as swap how many shifts? possibly one for each comparison hence:

188 Which is the best algorithm? one source reports: selection sort roughly 60% faster than bubble sort (without optimization) why? less swaps insertion sort is roughly 40% faster than selection sort why? selection sort guaranteed to do n(n-1) comparisons insertion sort only does this many in the worst case in the best case, insertion sort requires linear comparisons

189 Insertion Sort Example: 1724314550638596

190 Insertion Sort – Outer Loop loop through all elements one at a time starting at 2 nd element current 1724314550638596

191 Insertion Sort – Outer Loop Step 1: take current element out of the array 17 24 current 314550638596

192 Insertion Sort – Outer Loop Step 2: shift elements behind this element to the right stop when element to be shifted is less than current element or nothing left to shift 17 24 current 314550638596

193 Insertion Sort – Outer Loop Step 2: shift elements behind this element to the right stop when element to be shifted is less than current element nothing left to shift, so stop 17 24 current 314550638596

194 Insertion Sort – Outer Loop Step 3: place the current element in the empty spot created by shifting 1724 current 314550638596

195 Insertion Sort – Outer Loop Step 4: move to next element in list, and being process again current 1724314550638596

196 Insertion Sort – Outer Loop Step 1: take current element out of the array 17 31 current 244550638596

197 Insertion Sort – Outer Loop Step 2: shift elements behind this element to the right stop when element to be shifted is less than current element or nothing left to shift 17 31 current 244550638596

198 Insertion Sort – Outer Loop Step 2: shift elements behind this element to the right stop when element to be shifted is less than current element nothing left to shift, so stop 17 31 current 244550638596

199 Insertion Sort – Outer Loop Step 3: place the current element in the empty spot created by shifting 1731 current 244550638596

200 Insertion Sort – Outer Loop Step 4: move to next element in list, and being process again 1731 current 244550638596

201 Insertion Sort – Outer Loop Step 1: take current element out of the array 17 45 current 243150638596

202 Insertion Sort – Outer Loop Step 2: shift elements behind this element to the right stop when element to be shifted is less than current element or nothing left to shift 17 45 current 243150638596

203 Insertion Sort – Outer Loop Step 2: shift elements behind this element to the right stop when element to be shifted is less than current element nothing left to shift, so stop 17 45 current 243150638596

204 Insertion Sort – Outer Loop Step 3: place the current element in the empty spot created by shifting 1745 current 243150638596

205 Insertion Sort – Outer Loop Step 4: move to next element in list, and being process again 1745 current 243150638596

206 And so on as you can see, insertion sort only does 1 comparison per item when the list is already sorted

207

208 Insertion Sort insertion sort is often daunting to new programmers much less intuitive than bubble sort or selection sort remember: after 1 st pass of inner loop, first two items are sorted after 2 nd pass of inner loop, first three items are sorted after 3 rd pass of inner loop, first four items are sorted etc …

209 Insertion Sort Example: 8524634517319650

210 Insertion Sort – Outer Loop After first pass notice that first two items are sorted 2463451731965085

211 Insertion Sort – Outer Loop After second pass notice that first three items are sorted 2445173196508563

212 Insertion Sort – Outer Loop After third pass notice that first four items are sorted 24451731965085176324

213 Insertion Sort – Outer Loop After 4 th pass notice that first 5 th items are sorted 2445173196508563


Download ppt "Computer Science 1620 Sorting. cases exist where we would like our data to be in ascending (descending order) binary searching printing purposes selection."

Similar presentations


Ads by Google