-
Notifications
You must be signed in to change notification settings - Fork 0
/
mergeSort.cpp
35 lines (29 loc) · 1 KB
/
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
@author : Jinka Sai Sagar
#include <iostream>
using namespace std;
void merge(int arr[], int first, int middle, int last){
//Storing left and right parts in temporaty arrays
int left = middle - first;
int right = last - (middle + 1);
int leftArr[left], rightArr[right];
int mainArrayIndex;
for(int i = 0; i < left ; i++)
leftArr[i] = arr[i];
for(int i = 0; i < right; i++)
rightArr[i] = arr[i];
int i=0, j=0, k=first;
while(i <= middle && j <= last)
arr[k++] = (leftArr[i] < rightArr[j]) ? leftArr[i++] : rightArr[j++];
}
void mergeSort(int arr[], int first, int last){
int middle = (first + (last-1))/2;
mergeSort(arr,first, middle);
mergeSort(arr, middle+1, last);
merge(arr, first, middle, last);
}
int main(){
int arr[] = {64,67,23,56,98,12,42,9};
mergeSort(arr, 0, 7);
for(int i = 0; i < 8; i++)
cout<< "*(arr+i)" <<endl;
}