Objectives of this lecture
Merge Sort
[28 81
36 13 17 47 55
65 23 18 67 38
3]
[28 81
36 13 17 47] [55 65 23 18
67 38 3]
[28 81 36] [13
17 47] [55 65 23]
[18 67 38 3]
[28]
[81 36] [13]
[17 47] [55]
[65 23] [18
67] [38 3]
[81] [36] [17]
[47] [65] [23]
[18] [67] [38] [ 3]
[36 81] [17 47] [23 65]
[18 67] [3
38]
[28
36 81] [13 17 47]
[23 55 65 ] [3 18
38 67]
[13 17 28 36
47 81] [3 18 23
38 55 65 67]
[3
13 17 18 23 28
36 38 47 55 65
67 81]
#include
<stdio.h>
#define
SIZE 13
void
MergeSort(int x[], int first, int last);
void
Merge(int x[], int init1, int final1, int init2, int final2);
int
main(void)
{ int i;
int list[SIZE] =
{28,81,36,47,17,13,55,65,23,18,67,38,3};
MergeSort(list, 0, SIZE-1);
for(i = 0; i < SIZE; i++)
printf("%d ", num[i]);
return 0;
}
void
Merge(int x[],int init1,int final1,int init2,int final2)
{
int n,j,k;
int temp[SIZE];
n = init1; j = init1; k = init2;
while(j <= final1 && k <=
final2)
{
if(x[j] < x[k])
temp[n++] = x[j++];
else
temp[n++] = x[k++];
}
while(j <= final1)
temp[n++] = x[j++];
while(k <= final2)
temp[n++] = x[k++];
for(n = init1; n <= final2; n++)
x[n] = temp[n];
return 0;
}
void
MergeSort(int x[], int first, int last)
{ int mid;
if(last > first)
{ mid = (first + last)/2;
MergeSort(x, first, mid);
MergeSort(x, mid + 1, last);
Merge(x, first, mid, mid + 1, last);
}
return;
}
Performance:
Quick Sort
28 |
81 |
36 |
13 |
17 |
47 |
55 |
65 |
23 |
18 |
67 |
38 |
3 |
left |
|
|
|
|
|
|
|
|
|
|
|
right |
28 |
81 |
36 |
13 |
17 |
47 |
55 |
65 |
23 |
18 |
67 |
38 |
3 |
|
left |
|
|
|
|
|
|
|
|
|
|
right |
28 |
3 |
36 |
13 |
17 |
47 |
55 |
65 |
23 |
18 |
67 |
38 |
81 |
|
left |
|
|
|
|
|
|
|
|
|
|
right |
28 |
3 |
36 |
13 |
17 |
47 |
55 |
65 |
23 |
18 |
67 |
38 |
81 |
|
|
|
|
|
left |
|
|
|
|
|
right |
|
28 |
3 |
36 |
13 |
17 |
38 |
55 |
65 |
23 |
18 |
67 |
47 |
81 |
|
|
|
|
|
left |
|
|
|
|
|
right |
|
28 |
3 |
36 |
13 |
17 |
38 |
55 |
65 |
23 |
18 |
67 |
47 |
81 |
|
|
|
|
|
|
left |
|
|
right |
|
|
|
28 |
3 |
36 |
13 |
17 |
38 |
18 |
65 |
23 |
55 |
67 |
47 |
81 |
|
|
|
|
|
|
left |
|
|
right |
|
|
|
28 |
3 |
36 |
13 |
17 |
38 |
18 |
23 |
65 |
55 |
67 |
47 |
81 |
|
|
|
|
|
|
|
left |
right |
|
|
|
|
28 |
3 |
36 |
13 |
17 |
38 |
18 |
23 |
65 |
55 |
67 |
47 |
81 |
|
|
|
|
|
|
|
right |
left |
|
|
|
|
28 |
3 |
36 |
13 |
17 |
38 |
18 |
23 |
65 |
55 |
67 |
47 |
81 |
|
|
|
/*
Quick sorts an array in increasing order */
#include
<stdio.h>
#define
SIZE 13
void
swap(int *a , int *b);
void
partition(int x[], int *left , int *right, int pivot);
void
QuickSort(int x[], int start, int end);
int
main(void)
{ int i;
int list[SIZE] =
{28,81,36,13,17,47,55,65,23,18,67,38,3};
QuickSort(list, 0, SIZE-1);
for(i = 0; i < SIZE; i++)
printf("%d ", list[i]);
return 0;
}
void
QuickSort(int x[], int start, int end)
{
int left = start, right = end, pivot = x[(start + end)/2];
partition(x, &left, &right,
pivot);
if(start < right)
QuickSort(x, start, right);
if(left < end)
QuickSort(x, left, end);
Return 0;
}
void
partition(int x[], int *left , int *right, int pivot)
{ do
{
while(x[*left] < pivot)
(*left)++;
while(x[*right] > pivot)
(*right)--;
if(*left < *right)
{ swap(&x[*left] ,
&x[*right]);
(*left)++;
(*right)--;
}
else if(*left == *right)
(*left)++;
}while(*left <= *right);
return
0;
}
void
swap(int *a , int *b)
{
int temp;
temp =
*a;
*a = *b;
*b = temp;
return;
}
Performance:
Choice of Pivot:
1 2 3 4 5 6 7
1
2 3 4 5 6 7