Upload
sandra-landry
View
236
Download
0
Embed Size (px)
Citation preview
Consider an array of n values to be sorted into ascending order.
Sorting
Involves repeated passes(scans) through the data in the array.
9.1 The Bubble Sort
This involves comparing successive pairs of items
7 19 2 83
For example, if the first is larger than the second, then they are interchanged, otherwise they’re left alone.
7 19 2 83
This guarantees that after the first pass, the largest item is moved(“bubbled”) to the end of the array.
7 19 2 83
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
7 3 9 2 8 1[0] [1] [2] [3] [4] [5]
i = 0;j = i + 1;
0
i
1
j
i j
SWAP
i++;j = i + 1;
0
i
1
j
3 7 9 2 8 1[0] [1] [2] [3] [4] [5]
i j
i++;j = i + 1;
1
i
2
j
3 7 9 2 8 1[0] [1] [2] [3] [4] [5]
i j
SWAP 2
i
3
j
3 7 9 2 8 1[0] [1] [2] [3] [4] [5]
i j
2
i
3
j
3 7 2 9 8 1[0] [1] [2] [3] [4] [5]
i j
i++;j = i + 1;
3
i
4
j
3 7 2 9 8 1[0] [1] [2] [3] [4] [5]
i j
SWAP
3
i
4
j
3 7 2 8 9 1[0] [1] [2] [3] [4] [5]
i j
i++;j = i + 1;
4
i
5
j
3 7 2 8 9 1[0] [1] [2] [3] [4] [5]
i j
SWAP
4
i
5
j
3 7 2 8 1 9[0] [1] [2] [3] [4] [5]
i j
i = 0;j = i + 1;
1 Pass completed
0
i
1
j
3 7 2 8 1 9[0] [1] [2] [3] [4] [5]
i j
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
3 2 1[0] [1] [2]
0
i
0
min
1
ji = 0;min = i;j = i + 1;
0
i
0
min
1
jif (a[j] < a[min]) min = j;
3 2 1[0] [1] [2]
i min j
j++; 0
i
1
min
1
j
3 2 1[0] [1] [2]
i min j
0
i
1
min
2
j
3 2 1[0] [1] [2]
i min j
if (a[j] < a[min]) min = j;
0
i
2
min
2
j
3 2 1[0] [1] [2]
i min j
j++;
0
i
2
min
3
j
3 2 1[0] [1] [2]
i min j
if (i != min) swap(a[i],a[min])
SWAP
0
i
2
min
3
j
1 2 3[0] [1] [2]
i min j
i++;min = i;j = i + 1;
1
i
1
min
2
j
1 2 3[0] [1] [2]
i min j
if (a[j] < a[min]) min = j;
Sort the “remaining array” in similar way
1
i
1
min
2
j
1 2 3[0] [1] [2]
i min j
j++;
1
i
1
min
3
j
1 2 3[0] [1] [2]
i min j
if (i != min) swap(a[i],a[min])i.e.false
1
i
1
min
3
j
1 2 3[0] [1] [2]
i min j
i++;min = i;j = i + 1;
1 2 3[0] [1] [2]
i min 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 1962
9.4 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
37 25 57 48 12 92 86 33[0] [1] [2] [3] [4] [5] [6] [7]
pivot = a[0] bottom = 0 top = 7
bottom top
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
33 25 12 37 48 92 86 57[0] [1] [2] [3] [4] [5] [6] [7]
bottom top
At the end of the first partition the array is “more sorted” than before
33 25 12 37 48 92 86 57
bottom top bottom top
Repeat the partition on each set....