Objectives of this lecture
Problem of Insertion 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
Shell Sort
void
shell_sort(int list[])
{ int d=MAX_SIZE;
do
{ d = d/3+1;
ins_sort2(list,d);
} while (d>1);
}
void
ins_sort2(int list[], int d)
{
int i, j, temp;
for (i=d; i<MAX_SIZE; i++)
{ temp=list[i];
j=i;
while ((j-d>=0) &&
list[j-d]>temp)
{ list[j]=list[j-d];
j=j-d;
}
list[j]=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