Consider an array of n values to be sorted into ascending order. Sorting
Involves repeated passes(scans) through the data in the array. 9.1The Bubble Sort
This involves comparing successive pairs of items
For example, if the first is larger than the second, then they are interchanged, otherwise they’re left alone
This guarantees that after the first pass, the largest item is moved(“bubbled”) to the end of the array
After the second pass, the next largest item is placed in end position of the array but one, and so on.....
After a maximum of n passes all the elements will have been bubbled into their correct places and the array will be sorted. Example follows
i = 0; j = i + 1; 0 i 1 jij SWAP
i++; j = i + 1; 0 i 1 j ij
i++; j = i + 1; 1 i 2 j ij
SWAP 2 i 3 j ij
2 i 3 j ij i++; j = i + 1;
3 i 4 j ij SWAP
3 i 4 j ij i++; j = i + 1;
4 i 5 j ij SWAP
4 i 5 j ij i = 0; j = i + 1; 1 Pass completed
0 i 1 j ij i++ = 0; j = i + 1; Start of 2nd pass … and so on
When does the program know when to stop? “Test that no interchanges have occurred in a pass.”
Bubble sort is not regarded as being efficient.
Given n elements, n-1 comparisons are needed per pass [*general case]. * implementation dependent If n passes are required(worst case) then, n(n - 1) comparisons are needed in all.
... Cards are inserted into their correct positions as they are dealt to a player. 9.2 The Insertion Sort Same way in which a deck of cards might be sorted.
Insertion Sort
This works by finding the smallest in the array and putting it into position [0]. 9.3 The Selection Sort
The “remaining array” is then sorted in a similar way. More efficient way of sorting than bubble sort. Example follows
0 i 0 min 1 j i = 0; min = i; j = i + 1;
0 i 0 min 1 j if (a[j] < a[min]) min = j; iminj
j++; 0 i 1 min 1 j i j
0 i 1 2 j i j if (a[j] < a[min]) min = j;
0 i 2 min 2 j i j j++;
0 i 2 min 3 j i j if (i != min) swap(a[i],a[min]) SWAP
0 i 2 min 3 j i j i++; min = i; j = i + 1;
1 i 1 min 2 j i j if (a[j] < a[min]) min = j; Sort the “remaining array” in similar way
1 i 1 min 2 j i j j++;
1 i 1 min 3 j i j if (i != min) swap(a[i],a[min]) i.e.false
1 i 1 min 3 j i j i++; min = i; j = i + 1;
imin j TERMINATE 2 i 2 min 3 ji < (a.length-1) i.e false
This is known as a Partition Exchange Sort and was invented by C.A.Hoare in The Quick Sort Method In general faster than the bubble and selection sorts.
This involves partitioning a set of data into subsets with respect to: an ordering relation particular item - a “pivot”
A = {44,55,12,42,94,6,18,47} B = {18,6,12} C = {94,55,44,47} {42} {6}{18} {12}{94} {55,44,47} {47} {44}{55}... repeatedly partition until sets with a single member, reassembling gives sorted set
pivot = a[0]bottom = 0top = 7 bottomtop Refer to algorithm(steps 3 and 4) & carry out “dry runs” in tutorials
... ensure elements with indices above top are greater than pivot and elements with indices below bottom are less than pivot bottomtop At the end of the first partition the array is “more sorted” than before
bottomtopbottomtop Repeat the partition on each set....