Sei sulla pagina 1di 11

Implementation of mergesort algorithm by using c++ :

#include <iostream>

using namespace std;

void Merge(int *a, int low, int high, int mid)

int i, j, k, temp[high-low+1];

i = low;

k = 0;

j = mid + 1;

while (i <= mid && j <= high)

if (a[i] < a[j])

temp[k] = a[i];

k++;

i++;

else

temp[k] = a[j];

k++;

j++;

while (i <= mid)

temp[k] = a[i];
k++;

i++;

while (j <= high)

temp[k] = a[j];

k++;

j++;

for (i = low; i <= high; i++)

a[i] = temp[i-low];

void MergeSort(int *a, int low, int high)

int mid;

if (low < high)

mid=(low+high)/2;

MergeSort(a, low, mid);

MergeSort(a, mid+1, high);

Merge(a, low, high, mid);

int main()

int n, i;
cout<<"\nEnter the number of data element to be sorted: ";

cin>>n;

int arr[n];

for(i = 0; i < n; i++)

cout<<"Enter element "<<i+1<<": ";

cin>>arr[i];

MergeSort(arr, 0, n-1);

cout<<"\nSorted Data ";

for (i = 0; i < n; i++)

cout<<"->"<<arr[i];

return 0;

Merge sort algorithm:


Merge sort is divide and conquer sorting technique where we divide the whole array in to parts then
apply merge technique on them and then combine them in order to produce the final sorted array.
Merge sort consist of two basic algorithms called MERGE and MERGE_SORT.

The basic algorithm MERGE is the procedure of combining two sorted arrays into a new sorted array.

It is like say there are two arrays,

Now we will apply MERGE technique on it.


Firstly we will create a new array of size m+n where m is the size of the Arr1 and n is the size of the
Arr2. Like in this case m=4 and n=4. Hence our new array Arr3 will of size
4+4=8.

Now we will have two pointers first pointing at the first position of Arr1 and the other on the 1st
position of the Arr2.

Then they go on comparing the values of the two pointers. The pointer with lesser value copy the
value to the Arr3 and move a place right

.
Now for the first comparison Arr1[0]=1 and Arr2[0]=2. So as 1<2 hence Arr3[0]=1 and the pointer at
Arr1[0] incements and now points towards Arr1[1].

Similarly now Arr1[1]=5 and Arr2[0]=2. We know that 2<5 and hence Arr3[1]=Arr2[0]=2. Pointer
pointing towards Arr2[0] increments and now pointing towards Arr2[1].
Similarly now Arr1[1]=5 and Arr2[1]=4. We know that 4<5 and hence Arr3[2]=Arr2[1]=4. Pointer
pointing towards Arr2[1] increments and now pointing towards Arr2[2].

Similarly after several steps we get,


Th flow of control in this recursive algorithm is,

Implementation of heap sort algorithm by using c++ :

#include <iostream>

using namespace std;

void heapify(int arr[], int n, int i)

int largest = i;

int l = 2 * i + 1; // left = 2*i + 1


int r = 2 * i + 2; // right = 2*i + 2

if (l < n && arr[l] > arr[largest])

largest = l;

if (r < n && arr[r] > arr[largest])

largest = r;

if (largest != i) {

swap(arr[i], arr[largest]);

heapify(arr, n, largest);

void buildHeap(int arr[], int n)

int startIdx = (n / 2) - 1;

for (int i = startIdx; i >= 0; i--) {

heapify(arr, n, i);

void printHeap(int arr[], int n)

cout << "Array representation of Heap is:\n";

for (int i = 0; i < n; ++i)

cout << arr[i] << " ";

cout << "\n";

int main()

{
int arr[] = { 1, 3, 5, 4, 6, 13, 10, 9, 8, 15, 17 };

int n = sizeof(arr) / sizeof(arr[0]);

buildHeap(arr, n);

printHeap(arr, n);

return 0;

Flow control of heapify :

Array = {1, 3, 5, 4, 6, 13, 10, 9, 8, 15, 17}

Corresponding Complete Binary Tree is:

/ \

3 5

/ \ / \

4 6 13 10

/\ /\

9 8 15 17

The task to build a Max-Heap from above array.

Total Nodes = 11.

Last Non-leaf node index = (11/2) - 1 = 4.

Therefore, last non-leaf node = 6.


To build the heap, heapify only the nodes:

[1, 3, 5, 4, 6] in reverse order.

Heapify 6: Swap 6 and 17.

/ \

3 5

/ \ / \

4 17 13 10

/\ / \

9 8 15 6

Heapify 4: Swap 4 and 9.

/ \

3 5

/ \ / \

9 17 13 10

/\ / \

4 8 15 6

Heapify 5: Swap 13 and 5.

/ \

3 13

/ \ / \

9 17 5 10

/\ / \
4 8 15 6

Heapify 3: First Swap 3 and 17, again swap 3 and 15.

/ \

17 13

/ \ / \

9 15 5 10

/\ / \

4 8 3 6

Heapify 1: First Swap 1 and 17, again swap 1 and 15,

finally swap 1 and 6.

17

/ \

15 13

/ \ / \

9 6 5 10

/\ / \

4 8 3 1

Potrebbero piacerti anche