Sorting 2 An array a is sorted (ascending order) if: for all i a[i] a[j] Probably the most...
-
Upload
rosalind-morgan -
Category
Documents
-
view
213 -
download
0
Transcript of Sorting 2 An array a is sorted (ascending order) if: for all i a[i] a[j] Probably the most...
Sorting
2
Sorting
• An array a is sorted (ascending order) if:
for all i < j => 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
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?• …
B. Smith 4
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
…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
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
loopsFor 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 arraySelection Sort Analysis
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 elements2. 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
690 307 32 155 426
690 307 32 155 426307 690 32 155 426307 32 690 155 426307 32 155 690 426307 32 155 426 690
307 32 155 426 690
Bubble Sort pass 1
11
Bubble Sort - pass 2
307 32 155 426 690
307 32 155 426 69032 307 155 426 69032 155 307 426 690
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
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. Smith 13
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
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
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
Bibliography
• http://mathbits.com/MathBits/CompSci/Arrays/Bubble.htm
• https://www.google.it/url?sa=t&rct=j&q=&esrc=s&source=web&cd=1&cad=rja&uact=8&ved=0CCEQFjAA&url=http%3A%2F%2Fwww2.swccd.edu%2F~bsmith%2Fm130%2Fsp06%2FL15-Sorting%2520-%2520OLD%2520Vers.ppt&ei=spBkVMXIGMHCywPrz4DwBg&usg=AFQjCNF7QeyhLoWBwRe3VyF0_jIn78ftMw&sig2=hJE7CqEqQJ6p1vtXo2ACEg&bvm=bv.79189006,d.bGQ
B. Smith 17