-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMergeSort.cpp
More file actions
78 lines (61 loc) · 1.5 KB
/
MergeSort.cpp
File metadata and controls
78 lines (61 loc) · 1.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
//MergeSort
//divide array into two halves, sort each half, merge two halves
//time: O(nlgn) compares, O(nlgn) exchanges
//but use O(n) memory
#include <iostream>
#include <stdlib.h>
#include <time.h>
using namespace std;
#define SORT_NUM 50
void swap(int *a,int m,int n)
{
int tmp = a[m];
a[m] = a[n];
a[n] = tmp;
}
void display(int *a,int num)
{
for(int i=0;i<num;i++)
cout << "sorted array [" << i << "] = " << a[i] << endl;
}
void MergeArray(int *a,int lo,int mid,int hi)
{
int *tmp1 = new int[mid-lo+1];
int *tmp2 = new int[hi-mid];
for(int i=0;i<(mid-lo+1);i++)
tmp1[i] = a[lo+i];
for(int j=0;j<(hi-mid);j++)
tmp2[j] = a[mid+1+j];
for(int k=lo,i=0,j=0;k<=hi;k++)
{
if(i>(mid-lo)) a[k] = tmp2[j++];
else if(j>(hi-mid-1)) a[k] = tmp1[i++];
else if(tmp1[i] < tmp2[j]) a[k] = tmp1[i++];
else a[k] = tmp2[j++];
}
delete []tmp1;
delete []tmp2;
}
void MergeSort(int *a,int lo,int hi) //recursion call itself
{
if(hi<=lo) return;
int mid = lo+(hi-lo)/2;
MergeSort(a,lo,mid);
MergeSort(a,mid+1,hi);
MergeArray(a,lo,mid,hi);
}
int main()
{
int lo = 0;
int hi = SORT_NUM-1;
int num = SORT_NUM;
int a[num]= {0};
srand((int)time(0));
for(int i=0;i<num;i++)
a[i] = rand()%100;
//display(a,num);
//cout << "-----------------------------------------------"<<endl;
MergeSort(a,lo,hi);
display(a,num);
return 0;
}