-
Notifications
You must be signed in to change notification settings - Fork 1
/
merge sort.c
86 lines (69 loc) · 1.53 KB
/
merge sort.c
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
79
80
81
82
83
84
85
86
#include <stdio.h>
#include <conio.h>
#include <time.h>
#include <stdlib.h>
void mergeSort(int arr[],int low,int mid,int high);
void partition(int arr[],int low,int high);
int main(){
int merge[9]={1,2,3,4,5,6,7,8,9},i,n;
srand(time(NULL));
printf("dizi eleman sayisi?\n");
scanf("%d",&n);
/*for(i=0;i<n;i++){
merge[i]=rand()%100;
}*/
printf("dizi ilk hali = \n");
for(i=0; i<n; i++)
{
printf("%d\t",merge[i]);
}
printf("\n");
partition(merge,0,n-1);
printf("merge sort uygulanmis dizi=\n");
for(i=0;i<n;i++){
printf("%d\t",merge[i]);
}
getch();
return 0;
}
void partition(int arr[],int low,int high){
int mid;
if(low<high){
mid=(low+high)/2;
partition(arr,low,mid);
partition(arr,mid+1,high);
mergeSort(arr,low,mid,high);
}
}
void mergeSort(int arr[],int low,int mid,int high){
int i,m,k,l,temp[50];
l=low;
i=low;
m=mid+1;
while((l<=mid)&&(m<=high)){
if(arr[l]<=arr[m]){
temp[i]=arr[l];
l++;
}
else{
temp[i]=arr[m];
m++;
}
i++;
}
if(l>mid){
for(k=m;k<=high;k++){
temp[i]=arr[k];
i++;
}
}
else{
for(k=l;k<=mid;k++){
temp[i]=arr[k];
i++;
}
}
for(k=low;k<=high;k++){
arr[k]=temp[k];
}
}