-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMergeSort.cpp
More file actions
64 lines (55 loc) · 791 Bytes
/
MergeSort.cpp
File metadata and controls
64 lines (55 loc) · 791 Bytes
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
/*
¹é²¢ÅÅÐò
*/
#include<iostream>
using namespace std;
int array[]={7,9,8,5,4,6,1,2,3};
int s[10];
void swap(int p,int q){
int temp;
temp=array[p];
array[p]=array[q];
array[q]=temp;
}
void Merge(int low,int mid,int high){
int i=low;
int j=mid+1;
int count=0;
while(i<mid+1 && j<high+1){
if(array[i]<array[j]){
s[count]=array[i];
i++;
}
else{
s[count]=array[j];
j++;
}
count++;
}
while(i<mid+1){
s[count++]=array[i];
i++;
}
while(j<high+1){
s[count++]=array[j];
j++;
}
for(int k=0;k<count;k++){
array[low]=s[k];
low++;
}
}
void sort(int low,int high){
if(low<high){
int m=(low+high)/2;
sort(low,m);
sort(m+1,high);
Merge(low,m,high);
}
}
int main(){
sort(0,8);
for(int i=0;i<9;i++)
cout<<array[i]<<endl;
return 0;
}