Last active
August 6, 2017 02:08
-
-
Save barrysteyn/5177637 to your computer and use it in GitHub Desktop.
Merge sort example in C
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<iostream> | |
#include "mergesort.c" | |
using namespace std; | |
int main(int argc, char** argv) { | |
int num; | |
cout << "How many numbers do you want to sort: "; | |
cin >> num; | |
int a[num]; | |
for (int i = 0; i < num; i++) { | |
cout << (i + 1) << ": "; | |
cin >> a[i]; | |
} | |
// Start merge sort | |
mergeSort(a, num); | |
// Print the sorted array | |
cout << endl; | |
for (int i = 0; i < num; i++) | |
cout << a[i] << " "; | |
cout << endl; | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void merge(int *arr, int size1, int size2, int *inversions) { | |
int temp[size1+size2]; | |
int ptr1=0, ptr2=0; | |
while (ptr1+ptr2 < size1+size2) { | |
if (ptr1 < size1 && arr[ptr1] <= arr[size1+ptr2] || ptr1 < size1 && ptr2 >= size2) | |
temp[ptr1+ptr2] = arr[ptr1++]; | |
if (ptr2 < size2 && arr[size1+ptr2] < arr[ptr1] || ptr2 < size2 && ptr1 >= size1) { | |
temp[ptr1+ptr2] = arr[size1+ptr2++]; | |
*inversions += size1-ptr1; | |
} | |
} | |
for (int i=0; i < size1+size2; i++) | |
arr[i] = temp[i]; | |
} | |
void mergeSort(int *arr, int size, int* inversions) { | |
if (size == 1) | |
return; | |
int size1 = size/2, size2 = size-size1; | |
mergeSort(arr, size1, inversions); | |
mergeSort(arr+size1, size2, inversions); | |
merge(arr, size1, size2, inversions); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void merge(int *arr, int size1, int size2) { | |
int temp[size1+size2]; | |
int ptr1=0, ptr2=0; | |
while (ptr1+ptr2 < size1+size2) { | |
if (ptr1 < size1 && arr[ptr1] <= arr[size1+ptr2] || ptr1 < size1 && ptr2 >= size2) | |
temp[ptr1+ptr2] = arr[ptr1++]; | |
if (ptr2 < size2 && arr[size1+ptr2] < arr[ptr1] || ptr2 < size2 && ptr1 >= size1) | |
temp[ptr1+ptr2] = arr[size1+ptr2++]; | |
} | |
for (int i=0; i < size1+size2; i++) | |
arr[i] = temp[i]; | |
} | |
void mergeSort(int *arr, int size) { | |
if (size == 1) | |
return; | |
int size1 = size/2, size2 = size-size1; | |
mergeSort(arr, size1); | |
mergeSort(arr+size1, size2); | |
merge(arr, size1, size2); | |
} |
Really nice code but it is missing detailed explanations, if anyone want to read step by step explanations then it could be found here http://www.hellgeeks.com/merge-sort/
This is wrong:
temp[ptr1+ptr2] = arr[ptr1++];
An equals sign is not a sequence point, so you have no way of telling if ptr1
is incremented before or after accessing temp[ptr1+ptr2]
.
An equal sign need not provide a sequence point. in arr[ptr1++]
the value for the index is ptr1
before the side effect of the postfix operator is applied to the pointer. C11 Standard (draft n1570) §6.5.2.4 Postfix increment and decrement operators (2)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
clear, thank you