-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path3.MergeSort.cpp
76 lines (61 loc) · 1.48 KB
/
3.MergeSort.cpp
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
/*
* This program takes an array from the user
* sorts it using merge sort
* Prints the sorted array
* Time Complexity of the algorithm is: O(nlogn)
*
* For more information about Merge Sort Algorithm: https://en.wikipedia.org/wiki/Merge_sort
*
* Coded by: Abdurrezak EFE
*
* */
#include <iostream>
#include <algorithm>
using namespace std;
void merge(int arr[], int l, int m, int r) //merge function O(n)
{
int lsize = m-l+1;
int rsize = r-m;
int left[lsize];
int right[rsize];
for(int i=0;i<lsize;i++)
left[i] = arr[i+l];
for(int i=0;i<rsize;i++)
right[i] = arr[i+m+1];
int li=0,ri=0;
int index = l;
while(li<lsize && ri<rsize)
{
if(left[li] <= right[ri])
arr[index]=left[li],li++;
else
arr[index] = right[ri],ri++;
index++;
}
while(li<lsize)
arr[index] = left[li],li++,index++;
while(ri<rsize)
arr[index] = right[ri],ri++,index++;
}
void merge_sort(int arr[],int l, int r) //merge sort function O(nlogn)
{
if(l<r)
{
int m = (l+r)/2;
merge_sort(arr,l,m);
merge_sort(arr,m+1,r);
merge(arr,l,m,r);
}
}
int main()
{
int k;
cout << "Enter the number of the integers you want to construct the array from: ";
cin >> k;
int arr[k];
for(int i=0;i<k;i++)
cin >> arr[i];
merge_sort(arr,0,k-1); //sorting
for(int i=0;i<k;i++) //printing the array
cout << arr[i] << " ";
}