0% found this document useful (0 votes)
18 views

Merge

The document contains code for implementing the merge sort algorithm. It includes functions for printing arrays, merging two sorted halves of an array, and performing the overall merge sort on an array by recursively splitting it in half and merging the sorted halves.

Uploaded by

edieali
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

Merge

The document contains code for implementing the merge sort algorithm. It includes functions for printing arrays, merging two sorted halves of an array, and performing the overall merge sort on an array by recursively splitting it in half and merging the sorted halves.

Uploaded by

edieali
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

#include <stdio.

h>

void printArray(int *A, int n)

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

printf("%d ", A[i]);

printf("\n");

void merge(int A[], int mid, int low, int high)

int i, j, k, B[100];

i = low;

j = mid + 1;

k = low;

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

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

B[k] = A[i];

i++;

k++;
}

else

B[k] = A[j];

j++;

k++;

while (i <= mid)

B[k] = A[i];

k++;

i++;

while (j <= high)

B[k] = A[j];

k++;

j++;

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

A[i] = B[i];

}
}

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, mid, low, high);

int main()

// int A[] = {9, 14, 4, 8, 7, 5, 6};

int A[] = {9, 1, 4, 14, 4, 15, 6};

int n = 7;

printArray(A, n);

mergeSort(A, 0, 6);

printArray(A, n);

return 0;

You might also like