Skip to content

Commit a14f92b

Browse files
authored
Merge pull request thuva4#93 from srpurwaha201/master
add mergeSort algo for c++
2 parents c089416 + 846cc21 commit a14f92b

2 files changed

Lines changed: 159 additions & 0 deletions

File tree

HeapSort/C++/HeapSort.cpp

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
// C++ program for implementation of Heap Sort
2+
#include <iostream>
3+
using namespace std;
4+
5+
// To heapify a subtree rooted with node i which is
6+
// an index in arr[]. n is size of heap
7+
void heapify(int arr[], int n, int i)
8+
{
9+
int largest = i; // Initialize largest as root
10+
int l = 2*i + 1; // left = 2*i + 1
11+
int r = 2*i + 2; // right = 2*i + 2
12+
13+
// If left child is larger than root
14+
if (l < n && arr[l] > arr[largest])
15+
largest = l;
16+
17+
// If right child is larger than largest so far
18+
if (r < n && arr[r] > arr[largest])
19+
largest = r;
20+
21+
// If largest is not root
22+
if (largest != i)
23+
{
24+
swap(arr[i], arr[largest]);
25+
26+
// Recursively heapify the affected sub-tree
27+
heapify(arr, n, largest);
28+
}
29+
}
30+
31+
// main function to do heap sort
32+
void heapSort(int arr[], int n)
33+
{
34+
// Build heap (rearrange array)
35+
for (int i = n / 2 - 1; i >= 0; i--)
36+
heapify(arr, n, i);
37+
38+
// One by one extract an element from heap
39+
for (int i=n-1; i>=0; i--)
40+
{
41+
// Move current root to end
42+
swap(arr[0], arr[i]);
43+
44+
// call max heapify on the reduced heap
45+
heapify(arr, i, 0);
46+
}
47+
}
48+
49+
/* A utility function to print array of size n */
50+
void printArray(int arr[], int n)
51+
{
52+
for (int i=0; i<n; ++i)
53+
cout << arr[i] << " ";
54+
cout << "\n";
55+
}
56+
57+
// Driver program
58+
int main()
59+
{
60+
int arr[] = {12, 11, 13, 5, 6, 7};
61+
int n = sizeof(arr)/sizeof(arr[0]);
62+
63+
heapSort(arr, n);
64+
65+
cout << "Sorted array is \n";
66+
printArray(arr, n);
67+
}

MergeSort/C++/MergeSort.cpp

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
#include <iostream>
2+
3+
using namespace std;
4+
5+
// A function to merge the two half into a sorted data.
6+
void Merge(int *a, int low, int high, int mid)
7+
{
8+
// We have low to mid and mid+1 to high already sorted.
9+
int i, j, k, temp[high-low+1];
10+
i = low;
11+
k = 0;
12+
j = mid + 1;
13+
14+
// Merge the two parts into temp[].
15+
while (i <= mid && j <= high)
16+
{
17+
if (a[i] < a[j])
18+
{
19+
temp[k] = a[i];
20+
k++;
21+
i++;
22+
}
23+
else
24+
{
25+
temp[k] = a[j];
26+
k++;
27+
j++;
28+
}
29+
}
30+
31+
// Insert all the remaining values from i to mid into temp[].
32+
while (i <= mid)
33+
{
34+
temp[k] = a[i];
35+
k++;
36+
i++;
37+
}
38+
39+
// Insert all the remaining values from j to high into temp[].
40+
while (j <= high)
41+
{
42+
temp[k] = a[j];
43+
k++;
44+
j++;
45+
}
46+
47+
48+
// Assign sorted data stored in temp[] to a[].
49+
for (i = low; i <= high; i++)
50+
{
51+
a[i] = temp[i-low];
52+
}
53+
}
54+
55+
// A function to split array into two parts.
56+
void MergeSort(int *a, int low, int high)
57+
{
58+
int mid;
59+
if (low < high)
60+
{
61+
mid=(low+high)/2;
62+
// Split the data into two half.
63+
MergeSort(a, low, mid);
64+
MergeSort(a, mid+1, high);
65+
66+
// Merge them to get sorted output.
67+
Merge(a, low, high, mid);
68+
}
69+
}
70+
71+
int main()
72+
{
73+
int n, i;
74+
cout<<"\nEnter the number of data element to be sorted: ";
75+
cin>>n;
76+
77+
int arr[n];
78+
for(i = 0; i < n; i++)
79+
{
80+
cout<<"Enter element "<<i+1<<": ";
81+
cin>>arr[i];
82+
}
83+
84+
MergeSort(arr, 0, n-1);
85+
86+
// Printing the sorted data.
87+
cout<<"\nSorted Data ";
88+
for (i = 0; i < n; i++)
89+
cout<<"->"<<arr[i];
90+
91+
return 0;
92+
}

0 commit comments

Comments
 (0)