Objectives of
this lecture
Introduction
Selection Sort:
/*
SelectionSort: sort a contiguous list using selection sort.
Pre: The contiguous list has been created. Each
entry has a key.
Post: The list has been sorted into nondecreasing
order.
Uses:
MinKey, Swap.
*/
void
SelectionSort(List *list)
{ int current; /* position of place being correctly filled */
int min; /* position of smallest remaining key */
for (current = 0; current < list->count-1; current++) {
min = MinKey(current, list);
Swap(min, current, list);
}
}
/* MinKey: find the position of the smallest
key in the sublist.
Pre: The contiguous list has been created.
Start is a valid
positions
in list.
Post: The position of the entry with the smallest
key is returned.
*/
int
MinKey(int start, List *list)
{ int current, min=start;
for (current=start+1;
current<list->count; current++)
if (LT(list->entry[current].key,
list->entry[min].key))
min = current;
return min;
}
/*
Swap: swap two entries in the contiguous list.
Pre: The contiguous list has been created. pos1
and pos2
are valid positions in list.
Post: The entry at pos1 is swapped with the entry
at pos2.
*/
void
Swap(int pos1, int pos2, List *list)
{
ListEntry temp = list->entry[pos1];
list->entry[pos1] =
list->entry[pos2];
list->entry[pos2] = temp;
}
Index |
Original
|
Round1 |
Round3 |
Round
3 |
0 |
7 |
1 |
1 |
1 |
1 |
2 |
2 |
2 |
2 |
2 |
1 |
7 |
7 |
4 |
3 |
4 |
4 |
4 |
7 |
3(n-1) » 3n + O(1)
(n-1) + (n-2) + … + 1 =1/2n(n-1) » 1/2n2 + O(n)
Insertion Sort:
/*
InsertionSort: sort contiguous list using insertion sort method.
Pre: The contiguous list has been created.
Post: The list has been sorted into nondecreasing
order.
*/
void
InsertionSort(List *list)
{
int start; /* position of first unsorted entry */
int place; /* searches sorted part of list */
ListEntry temp; /* entry temporarily removed from list */
for (start = 1; start < list->count;
start++)
{
temp = list->entry[start];
place = start;
while ((place-1 >= 0) &&
LT(temp.key, list->entry[place-1].key))
{
list->entry[place] = list->entry[place-1];
place--;
}
list->entry[place] = temp;
}
}
Index |
Original
|
Round1 |
Round3 |
Round
3 |
0 |
7 |
2 |
1 |
1 |
1 |
2 |
7 |
2 |
2 |
2 |
1 |
1 |
7 |
4 |
3 |
4 |
4 |
4 |
7 |
Shell Sort
Original: |
28 |
81 |
36 |
47 |
17 |
13 |
55 |
65 |
23 |
18 |
67 |
38 |
3 |
Moves |
step1: |
28 |
81 |
36 |
47 |
17 |
13 |
55 |
65 |
23 |
18 |
67 |
38 |
3 |
0 |
step2: |
28 |
36 |
81 |
47 |
17 |
13 |
55 |
65 |
23 |
18 |
67 |
38 |
3 |
1 |
step3: |
28 |
36 |
47 |
81 |
17 |
13 |
55 |
65 |
23 |
18 |
67 |
38 |
3 |
1 |
step4: |
17 |
28 |
36 |
47 |
81 |
13 |
55 |
65 |
23 |
18 |
67 |
38 |
3 |
4 |
step5: |
13 |
17 |
28 |
36 |
47 |
81 |
55 |
65 |
23 |
18 |
67 |
38 |
3 |
5 |
step6: |
13 |
17 |
28 |
36 |
47 |
55 |
81 |
65 |
23 |
18 |
67 |
38 |
3 |
1 |
step7: |
13 |
17 |
28 |
36 |
47 |
55 |
65 |
81 |
23 |
18 |
67 |
38 |
3 |
1 |
step8: |
13 |
17 |
23 |
28 |
36 |
47 |
55 |
65 |
81 |
18 |
67 |
38 |
3 |
6 |
step9: |
13 |
17 |
18 |
23 |
28 |
36 |
47 |
55 |
65 |
81 |
67 |
38 |
3 |
7 |
step10: |
13 |
17 |
18 |
23 |
28 |
36 |
47 |
55 |
65 |
67 |
81 |
38 |
3 |
1 |
step11: |
13 |
17 |
18 |
23 |
28 |
36 |
38 |
47 |
55 |
65 |
67 |
81 |
3 |
5 |
step12: |
3 |
13 |
17 |
18 |
23 |
28 |
36 |
38 |
47 |
55 |
65 |
67 |
81 |
12 |
Total
number of moves = 44
/*
ShelSort: sort a contiguous list using ShellSort sort.
Pre: The contiguous list has been created. Each
entry has a key.
Post: The list has been sorted into nondecreasing
order.
Uses: InsersionSort2.
*/
void ShellSort(List *list)
{ int d=list->count;
do
{ d = d/3+1;
InsertionSort2(list,d);
} while (d>1);
}
/*
InsertionSort2: a modified version of insertion sort.
Pre: The contiguous list has been created.
Post: The relative ordering of the entries has
been improved, for
d=1, the list
will be sorted.
*/
void
InsertionSort2(List *list, int d)
{
int start; /* position of first unsorted entry */
int place; /* searches sorted part of list */
ListEntry temp; /* entry temporarily removed from list */
for (start = d; start < list->count;
start++)
{
temp = list->entry[start];
place = start;
while ((place-d >= 0) &&
LT(temp.key, list->entry[place-d].key))
{
list->entry[place] =
list->entry[place-d];
place-=d;
}
list->entry[place] = temp;
}
}
Original: |
28 |
81 |
36 |
47 |
17 |
13 |
55 |
65 |
23 |
18 |
67 |
38 |
3 |
Moves |
For d=5 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
step1: |
13 |
81 |
36 |
47 |
17 |
28 |
55 |
65 |
23 |
18 |
67 |
38 |
3 |
1 |
step2: |
13 |
55 |
36 |
47 |
17 |
28 |
81 |
65 |
23 |
18 |
67 |
38 |
3 |
1 |
step3: |
13 |
55 |
36 |
47 |
17 |
28 |
81 |
65 |
23 |
18 |
67 |
38 |
3 |
0 |
step4: |
13 |
55 |
36 |
23 |
17 |
28 |
81 |
65 |
47 |
18 |
67 |
38 |
3 |
1 |
step5: |
13 |
55 |
36 |
23 |
17 |
28 |
81 |
65 |
47 |
18 |
67 |
38 |
3 |
0 |
step6: |
13 |
55 |
36 |
23 |
17 |
28 |
81 |
65 |
47 |
18 |
67 |
38 |
3 |
0 |
step7: |
13 |
38 |
36 |
23 |
17 |
28 |
55 |
65 |
47 |
18 |
67 |
81 |
3 |
2 |
step8: |
13 |
38 |
3 |
23 |
17 |
28 |
55 |
36 |
47 |
18 |
67 |
81 |
65 |
2 |
For d=2 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
step1: |
3 |
38 |
13 |
23 |
17 |
28 |
55 |
36 |
47 |
18 |
67 |
81 |
65 |
1 |
step2: |
3 |
23 |
13 |
38 |
17 |
28 |
55 |
36 |
47 |
18 |
67 |
81 |
65 |
1 |
step3: |
3 |
23 |
13 |
38 |
17 |
28 |
55 |
36 |
47 |
18 |
67 |
81 |
65 |
0 |
step4: |
3 |
23 |
13 |
28 |
17 |
38 |
55 |
36 |
47 |
18 |
67 |
81 |
65 |
1 |
step5: |
3 |
23 |
13 |
28 |
17 |
38 |
55 |
36 |
47 |
18 |
67 |
81 |
65 |
0 |
step6: |
3 |
23 |
13 |
28 |
17 |
36 |
55 |
38 |
47 |
18 |
67 |
81 |
65 |
1 |
step7: |
3 |
23 |
13 |
28 |
17 |
36 |
47 |
38 |
55 |
18 |
67 |
81 |
65 |
1 |
step8: |
3 |
18 |
13 |
23 |
17 |
28 |
47 |
36 |
55 |
38 |
67 |
81 |
65 |
4 |
step9: |
3 |
18 |
13 |
23 |
17 |
28 |
47 |
36 |
55 |
38 |
67 |
81 |
65 |
0 |
step10: |
3 |
18 |
13 |
23 |
17 |
28 |
47 |
36 |
55 |
38 |
67 |
81 |
65 |
0 |
step11: |
3 |
18 |
13 |
23 |
17 |
28 |
47 |
36 |
55 |
38 |
65 |
81 |
67 |
1 |
For d= 1 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
step1: |
3 |
18 |
13 |
23 |
17 |
28 |
47 |
36 |
55 |
38 |
65 |
81 |
67 |
0 |
step2: |
3 |
13 |
18 |
23 |
17 |
28 |
47 |
36 |
55 |
38 |
65 |
81 |
67 |
1 |
step3: |
3 |
13 |
18 |
23 |
17 |
28 |
47 |
36 |
55 |
38 |
65 |
81 |
67 |
0 |
step4: |
3 |
13 |
17 |
18 |
23 |
28 |
47 |
36 |
55 |
38 |
65 |
81 |
67 |
2 |
step5: |
3 |
13 |
17 |
18 |
23 |
28 |
47 |
36 |
55 |
38 |
65 |
81 |
67 |
0 |
step6: |
3 |
13 |
17 |
18 |
23 |
28 |
47 |
36 |
55 |
38 |
65 |
81 |
67 |
0 |
step7: |
3 |
13 |
17 |
18 |
23 |
28 |
36 |
47 |
55 |
38 |
65 |
81 |
67 |
1 |
step8: |
3 |
13 |
17 |
18 |
23 |
28 |
36 |
47 |
55 |
38 |
65 |
81 |
67 |
0 |
step9: |
3 |
13 |
17 |
18 |
23 |
28 |
36 |
38 |
47 |
55 |
65 |
81 |
67 |
2 |
step10: |
3 |
13 |
17 |
18 |
23 |
28 |
36 |
38 |
47 |
55 |
65 |
81 |
67 |
0 |
step11: |
3 |
13 |
17 |
18 |
23 |
28 |
36 |
38 |
47 |
55 |
65 |
81 |
67 |
0 |
step12: |
3 |
13 |
17 |
18 |
23 |
28 |
36 |
38 |
47 |
55 |
65 |
67 |
81 |
1 |
Total
number of moves = 24