Objectives of this lecture
Introduction:
Sort(list)
{
if the list has length greater than 1 then
{
Partition the list into lowlist,
highlist;
Sort(lowlist);
Sort(highlist);
Combine(lowlist, highlist);
}
}
Merge Sort
/* MergeSort: sort contiguous list by the merge sort
method.
Pre: The list has
been created. Each entry of list contains a key.
Post: The list have been sorted in into non-decreasing order.
Uses: Merge.
*/
void MergeSort(List *list, int first, int last)
{ int mid;
if(last >
first)
{ mid = (first
+ last)/2;
MergeSort(list, first, mid);
MergeSort(list, mid + 1, last);
Merge(list,
first, mid, mid + 1, last);
}
return;
}
/* Merge: merge two lists producing a third list.
Pre: first and
second are sorted lists and have been created.
Post: out is a sorted list containing all entries that
were in
first and
second.
*/
void Merge(List *list, int init1,int final1,int init2,int
final2)
{ int i,j,k;
ListEntry
temp[SIZE]; /* SIZE is a constant */
k = init1; i =
init1; j = init2;
while(i <=
final1 && j <= final2)
{ if (LT(list->entry[i].key,
list->entry[j].key)
temp[k++] = list->entry[i++];
else
temp[k++] = list->entry[j++];
}
while(i <=
final1)
temp[k++] =
list->entry[i++];
while(j <=
final2)
temp[k++] =
list->entry[j++];
for(k = init1; k
<= final2; k++)
list->entry[k] = temp[k];
return 0;
}
Analysis:
|
|
|
|
|
|
|
|
|
|
|
n
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2 x n/2
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
4 x n/4
|
n = 2k è k = log n.
Thus,
number of comparisons = O(n log n) .
And
number of data movements = 2n log n = O(n log n).
Quick Sort
28 |
81 |
36 |
13 |
17 |
55 |
47 |
65 |
23 |
18 |
67 |
38 |
3 |
left |
|
|
|
|
|
|
|
|
|
|
|
right |
28 |
81 |
36 |
13 |
17 |
55 |
47 |
65 |
23 |
18 |
67 |
38 |
3 |
|
left |
|
|
|
|
|
|
|
|
|
|
right |
28 |
3 |
36 |
13 |
17 |
55 |
47 |
65 |
23 |
18 |
67 |
38 |
81 |
|
left |
|
|
|
|
|
|
|
|
|
|
right |
28 |
3 |
36 |
13 |
17 |
55 |
47 |
65 |
23 |
18 |
67 |
38 |
81 |
|
|
|
|
|
left |
|
|
|
|
|
right |
|
28 |
3 |
36 |
13 |
17 |
38 |
47 |
65 |
23 |
18 |
67 |
55 |
81 |
|
|
|
|
|
left |
|
|
|
|
|
right |
|
28 |
3 |
36 |
13 |
17 |
38 |
47 |
65 |
23 |
18 |
67 |
55 |
81 |
|
|
|
|
|
|
left |
|
|
right |
|
|
|
28 |
3 |
36 |
13 |
17 |
38 |
18 |
65 |
23 |
47 |
67 |
55 |
81 |
|
|
|
|
|
|
left |
|
|
right |
|
|
|
28 |
3 |
36 |
13 |
17 |
38 |
18 |
23 |
65 |
47 |
67 |
55 |
81 |
|
|
|
|
|
|
|
left |
right |
|
|
|
|
28 |
3 |
36 |
13 |
17 |
38 |
18 |
23 |
65 |
47 |
67 |
55 |
81 |
|
|
|
|
|
|
|
right |
left |
|
|
|
|
28 |
3 |
36 |
13 |
17 |
38 |
18 |
23 |
65 |
47 |
67 |
55 |
81 |
|
|
|
void QuickSort(List *list, int start, int end)
{ int left = start, right = end;
ListEntry
pivot = list->entry[(start + end)/2];
partition(list, &left, &right, pivot);
if(start <
right)
QuickSort(list, start, right);
if(left <
end)
QuickSort(list, left, end);
}
void partition(List *list, int *left , int *right,
ListEntry pivot)
{ do
{ while(LT(list->entry[*left].key ,
pivot.key))
(*left)++;
while(GT(list->entry[*right].key,
pivot.key))
(*right)--;
if(*left < *right) /* if left did not cross right */
{
swap(&list->entry[*left] , &list->entry[*right]);
(*left)++;
(*right)--;
}
else if(*left == *right)
(*left)++;
}while(*left
<= *right);
}
void swap(ListEntry *a , ListEntry *b)
{
ListEntry temp;
temp = *a;
*a = *b;
*b = temp;
}
Analysis: