1. merge sort

advertisement
1. MERGE SORT
Example : Suppose we had two sorted lists, and we wanted to merge them into one
sorted list. Then, it would be a simple matter to combine the two lists into one sorted
list – we simply take whichever is the smallest of the two heads of lists and put that
as the first item in the list. Lets look at figures below :
Fig 1.1 Two sorted lists to merge
From the two heads
of lists, the upper list
has the smaller head,
D. So we take D
from the list for the
merged list
Fig 1.2(a) Merging two sorted lists
Now the lower list
has the smallest
head, so we take E
Fig 1.2(b) Merging two sorted lists
Then it is back to the
upper list with I
Fig 1.2(c) Merging two sorted lists
Then it is the upper
list with M
Fig 1.2(d) Merging two sorted lists
Then it is the lower
list with P
Fig 1.2(e) Merging two sorted lists
Then it is the lower
list with U
Fig 1.2(f) Merging two sorted lists
Finally, the upper
list with X
Fig 1.2(g) Merging two sorted lists - MERGED
Splitting up the list
Here is an example of how we can recursively split up the list. We start with
“MIXEDUP”. We split up the list in the middle – “MIX” and “EDUP”. Now each of
those two lists gets split up again : “M” “IX” “ED” and “UP”. Finally we split them up
again and end up with lists of one element each : “M” “I” “X” “E” “D” “U” “P”.
Then we successively merge every two lists together until we have the complete sorted
list. See Fig 1.3
M
M
split
M
I
MIX
IX
MIXEDUP
X
E
ED
IX
UP
U
P
IMX
DEIMPUX
DE
D
EDUP
merge
DEPU
UP
This sorting is visualized as in flowchart Fig 1.4
Split
unsorted list
MergeSort
MergeSort
MergeSort
First half
Second half
Merge
sorted list
Fig 1.4 Merge Sort Algorithm
An Example Based on MERGE SORT Coding
Input :
7 6 5 4 3 2 1
Splitting and First Half Recursive (MergeSort(Left, Center)
MergeSort(0,6) 7654 321
Center = 3
Call MergeSort(4,6)
…
76 54 3 21
MergeSort(0,3)
Center = 1
Call MergeSort(2,3)
Call Merge(2,3,3)
MergeSort(0,1) 7 6 …
Center = 0
MergeSort(0,0)
STOP! (left == right)
Call MergeSort(Center+1, Right)
MergeSort(1, 1)
STOP! (left == right)
Call Merge(0,1,1)
void Merge(int lpos, int rpos, int rend)
{
int i,
//counter
int lend,
//left end
int numelements, //number of element to compare
int tmppos,
//temporarily position
int TmpArray[MAX]; //new array to store the sorted list
lend=rpos - 1;
tmppos=lpos;
//lend is before the rpos begins
//initiate tmppos to hold lpos position
numelements=rend-lpos+1;
//if both condition are true (left and right)
while ((lpos<=lend)&&(rpos<=rend))
//comparing the value between two pair
//(left and right eg. 76)
if ( A[lpos] <= A[rpos])
//if the first value smaller then the other,
//copy the position to TmpArray
TmpArray[tmppos++] = A[lpos++];
//if the first value is greater,
//change the position of storing in TmpArray
//so that TmpArray first element store the smallest value
else TmpArray[tmppos++] = A[rpos++];
//executed if statement in "ELSE" is executed - lpos is not
//incremented
while (lpos <= lend)
TmpArray[tmppos++] = A[lpos++];
//executed if statement in "IF" is executed - rpos is not
//incremented
while (rpos <= rend)
TmpArray[tmppos++] = A[rpos++];
//copy back from TmpArray to initial array of A.
for (i = 0; i < numelements; i++, rend--)
A[rend] = TmpArray[rend];
}
Merge(0,1,1)
lpos = 0, rpos=1, rend = 1
lend = 1-1 = 0
tmppos = 0
numelements = 2
after Merge is executed the result is :TmpArray[0] = 6 , TmpArray[1] = 7
A[1] = TmpArray[1] = 7
A[0] = TmpArray[0] = 6
Note : This process repeat for the rest of the element. Try figure out for the rest of the
element.
Exercise :
Given, two sorted lists as below, write the resulting sorted list using a MERGE
SORT technique.
List 1 : 4 6 84 100
List 2 : 10 36 60 90
Answer : 4 6 10 36 60 84 90 100
2. QUICK SORT
An Example Based on MERGE SORT Coding
Input :
0 1 2 3 4
5 4 3 2 1
//to swap the value
void Swap(int x, int y,int a[])
{
int temp;
temp=a[x];
a[x]=a[y];
a[y]=temp;
}
//to break down into two table by referencing to the pivot
int Partition (int left, int right,int a[])
{
char pivot;
int i,lastsmall,pivotpos;
Swap(left,(left+right)/2,a);
pivot=a[left];
pivotpos=left;
//Comparison is done between each value in each element with pivot
//if the value is smaller compare to the pivot, swap the value else
position remains
for(i=left+1;i<=right;i++)
{
if (a[i]<pivot)
Swap(++pivotpos,i,a);
}
//move large entry to right and small to left
//finally if each element complete exchanging, swap the pivot value
with the pivotpos value
Swap(left,pivotpos,a);
return pivotpos;
}
void QuickSort (int left,int right,int a[])
{
int pivotpos;
if (left<right)
{
pivotpos=Partition(left,right,a);
//divide again
QuickSort(left,pivotpos-1,a);
QuickSort(pivotpos+1,right,a);
}
}
QuickSort(0,4,A)
pivotpos
0 1 2 3 4
3 4 5 2 1
pivot
Partition(0,4,A)
Swap(0,2,A)
In for loop,
Comparison is done between each value in each element with pivot
pivotpos
pivotpos
0 1 2 3 4
3 4 5 2 1
pivot
Swap(1,3,A)
0 1 2 3 4
3 2 5 4 1
pivot
2<3
swap again
pivotpos
0 1 2 3 4
3 2 5 4 1
pivot
pivotpos
Swap(2,4,A)
0 1 2 3 4
3 2 1 4 5
pivot
1<3
swap again
swap the pivot value with the pivotpos value
Swap(left,pivotpos,a);
Swap(0,2,A
)
pivotpos
0
1
1 2 3 4
2 3 4 5
> pivot
< pivot
pivot
Note : This process repeat for the rest of the element until it is fully sorted
Exercise :
Given the below list, sketch out the result after a quick sort is carried out.
44 78 22 7 98 56 34 2 38 35 45
Answer : 45 22 7 44 34 2 38 35 56 78 98
> pivot
< pivot
pivot
Download