Presentation is loading. Please wait.

Presentation is loading. Please wait.

Sorting 2 An array a is sorted (ascending order) if: for all i a[i]  a[j] Probably the most well-studied algorithmic problem in Computer Science There.

Similar presentations


Presentation on theme: "Sorting 2 An array a is sorted (ascending order) if: for all i a[i]  a[j] Probably the most well-studied algorithmic problem in Computer Science There."— Presentation transcript:

1

2 Sorting

3 2 An array a is sorted (ascending order) if: for all i a[i]  a[j] Probably the most well-studied algorithmic problem in Computer Science There are many algorithms –We will study 2 –Why not just one? Differ in ease of programming Differ in performance What’s “best” sometimes depends on the problem

4 3 What Affects Choice of Algorithm? …… How large is the set of things? Are you sorting it all at once, or are elements coming in one at a time (incremental)? Is the data likely to be (nearly) sorted when you start? …

5 B. Smith4 Selection Sort Selection sort idea: –find smallest element in the array and exchange it with the element in the first position. –find second smallest element and exchange it with the element in the second position. Do this (n-1) times. 1 5 8 7 9 2 4 2 1 4 4 9 5 7 7 8 8 9 find min 1 5 8 7 9 2 4 input

6 … int i, j, tmp; for (i = 0; i < n - 1; i++) { for (j = i + 1; j < n; j++) if (arr[i] > arr[j]) //for descending order use if(a[j]>a[i]) { tmp = arr[i]; arr[i] = arr[j]; arr[j] = tmp; } … 5 Selection Sort Code

7 6 For I = 0 –for j=1, j < N, find min –The inner j loop iterates (N-1) times For I = 1 –for j=2, j < N, find min –The inner j loop iterates (N-2) times For I = 2 –for j=3, j < N, find min –The inner j loop iterates (N-3) times e.g., N=5, Iterate thru a[1] to a[4], 4 loops e.g., Iterate thru a[2] to a[4], 3 loops e.g., Iterate thru a[3] to a[4], 2 loops For the last subarray, the number of iterations is just 1 In general, total number of iterations (& comparisons) is: (N - 1) + (N - 2) + (N - 3) + … + 2 + 1 = N(N - 1)/2 In general, total number of iterations (& comparisons) is: (N - 1) + (N - 2) + (N - 3) + … + 2 + 1 = N(N - 1)/2 N = number of elements in array Selection Sort Analysis

8 7 What is the worst input for selection sort? Analysis of the worst case: –Outer loop executes N-1 times. –Inner loop executes N-i-1 comparisons. (N-1) + (N-2) + … + 2 + 1 = N*(N-1)/2 Max number of exchanges is proportional to array size (one exchange per outer loop). Worst case: … Advantage of selection sort: –Easy to write Disadvantage: –The running time is quite high. Selection sort finds minimum by linear search Can we do better than this?

9 8 Exchange (“Bubble”) Sort (RS) In the bubble sort, as elements are sorted they gradually "bubble" (or rise) to their proper location in the array, like bubbles rising in a glass of soda.  Elements next to each other are swapped so that the list gets sorted 1.Start with comparison of first two elements 2.For ascending order, smaller value is placed before larger value –For descending order,smaller value is placed after larger value 3.Continue until last two items are compared and evaluated for exchange 4.When you can go through the list with no exchanges, the elements are sorted

10 9 Bubble Sort – pass 1 6903073215542669030732155426 30769032690155690426690

11 10 69030732155426690307321554263076903215542630732690155426307321556904263073215542669030732155426690 Bubble Sort pass 1

12 11 Bubble Sort - pass 2 30732155426690307321554266903230715542669032155307426690

13 12 Bubble Sort Hence, when the first pass through the array is complete, the bubble sort returns to elements one and two and starts the process all over again. Note that the smallest value always sank to the bottom of list The larger elements slowly rose or “bubbled” up from the bottom Each pass always places the smallest in the list at the bottom –Hence, no need to do “compare and exchange” for the final two elements –For each pass, one less compare/exchange operation is required

14 So, when does the process stop? The bubble sort knows that it is finished when it examines the entire array and no "swaps" are needed (thus the list is in proper order). The bubble sort keeps track of occurring swaps by the use of a flag: we can insert a variable called flag and we can keep checking whether swapping of elements is taking place or not: if no swapping is taking place, that means the array is sorted and we can jump out of the for loop B. Smith13

15 Bubble sort code: exemple … int a[6]= {5,1,6,2,4,3 }; int i, j, temp; for (i=0; i<6; i++) { for (j=0; j<6-i-1; j++) { int flag=0;//taking a flag variable if (a[j]>a[j+1]) { temp=a[j]; a[j]=a[j+1]; a[j+1]=temp; flag=1; //setting flag as 1 if swapping occurs } if (!flag)//breaking out of for loop if no swapping take place { break; } 14

16 In the above code, if in a complete single cycle of j iteration (inner for loop) no swapping takes place and flag remains 0, then we will break out of the for loops, because the array has already been sorted 15

17 Complexity analysis of bubble sorting In the worst case (…) the total number of comparisons will be: n-1 in first pass, n-2 in second pass, n-3 in third pass and so on. So the total number of comparison will be: But when the list is already sorted the number of comparisons will be N-1. (using the flag variable) So, the number of comparisons is between n-1 and n(n-1)/2 16

18 Bibliography http://mathbits.com/MathBits/CompSci/Arrays/Bubb le.htmhttp://mathbits.com/MathBits/CompSci/Arrays/Bubb le.htm https://www.google.it/url?sa=t&rct=j&q=&esrc=s&sour ce=web&cd=1&cad=rja&uact=8&ved=0CCEQFjAA&url= http%3A%2F%2Fwww2.swccd.edu%2F~bsmith%2Fm1 30%2Fsp06%2FL15-Sorting%2520- %2520OLD%2520Vers.ppt&ei=spBkVMXIGMHCywPrz 4DwBg&usg=AFQjCNF7QeyhLoWBwRe3VyF0_jIn78f tMw&sig2=hJE7CqEqQJ6p1vtXo2ACEg&bvm=bv.7918 9006,d.bGQ B. Smith17


Download ppt "Sorting 2 An array a is sorted (ascending order) if: for all i a[i]  a[j] Probably the most well-studied algorithmic problem in Computer Science There."

Similar presentations


Ads by Google