Question: Program in "C" to implement merge sort.
0

Merge sort is a sorting algorithm that uses the divides conquer and combine algorithm paradigm.

a] Divide: It means partitioning the n-element array to be sorted into two sub arrays of n/2 elements in each sub-array.

b] conquer: It means sorting the two sub arrays recursively using merge sort.

c] combine: It means merging two sorted sub group of size n/2 each to produce the sorted array of n elements.

Program:

include < stdio . h>

include < conio.h >

void merge (int a [ ], int, int, int);

void merge sort (int a [ ], int, int, int) ;

void mains ( )

<

int a [10] , i, n , j , k ;

clrscr ( ) ;

print t ("in enter no of elements in array = ");

scan f (" % d ", &n);

print f (" in enter elements of array ");

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

<

print f ("in a [% d] = " ! i);

scan f (" % d , & a [i] ) ;

3

merge sort (a, o , n-1);

print f ("in sorted array = in ");

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

<

print f (" \ + % d " , a [i]) ;

<

print f ("+%d",a[i]);

3

fetch ( ) ;

3

void merge (int a [ ] , int beg, int mid, int end)

<

int i = beg, j = mid + 1 , index = beg, temp [10] , k ; while ( ( i < 0 mid) & & ( j < 0 end) )

<

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

<

temp [index] = a [i];

itt,

3

else

tem y [index] = a [j];

jtt;

3

index tt;

<

if (i > mid)

<

while (j < end)

<

temp [index] = a [j];

jtt;

index tt ;

3

else

<

while (i < = mid)

<

temp [index] = a [i];

itt;

index tt;

3

3

for (k = beg; k < index ; ktt)

<

a[k] = temp [k];

3

void merge sort (int a [ ], int beg, int end)

<

int mid;

if (beg < end)

<

mid = (beg + end) / 2;

merge-sort (a, beg , mid);

merge-sort (a, midt/ , end);

merge (a, beg , mid, end );

3

renu • 16 views
ADD COMMENTlink
written 4 weeks ago by gravatar for RB RB100
Please log in to add an answer.