answersLogoWhite

0

#include <stdio.h>

void merge_sort(int [], int, int);

void merge_array(int [], int, int, int);

main()

{

int a[50], n, i;

printf("\nEnter size of an array: ");

scanf("%d", &n);

printf("\nEnter elements of an array:\n");

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

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

merge_sort(a, 0, n-1);

printf("\n\nAfter sorting:\n");

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

printf("\n%d", a[i]);

getch();

}

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, mid+1, end);

merge_array(a, beg, mid, end);

}

}www.eazynotes.com Gursharan Singh Tatla Page No. 2

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

{

int i, left_end, num, temp, j, k, b[50];

for(i=beg; i<=end; i++)

b[i] = a[i];

i = beg;

j = mid+1;

k = beg;

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

{

if (b[i] <= b[j])

{

a[k] = b[i];

i++; k++;

}

else

{

a[k] = b[j];

j++; k++;

}

}

if (i <= mid)

{

while (i <= mid)

{

a[k] = b[i];

i++; k++;

}

}

else

{

while (j <= end)

{

a[k] = b[j];

j++; k++;

}

}

}

User Avatar

Wiki User

13y ago

What else can I help you with?

Related Questions

The easy logic of Merge sort in C?

Top down merge sort is the easy way of merge sort in C language . It is used to derived o(n log n) algorithm . This is in par with the other methods.


Different types of sorting techniques in c language?

types of sorting in c language are: insertion sort selection sort bubble sort merge sort two way merge sort heap sort quick sort


Is C plus plus preprocessor software?

Sometimes, it is. Some implementations compile C++ code into C code, and then compile the C code.


Types of sort in c plus plus?

There's only one type of sort in C++; std::sort. If you want other types you'll need to write your own.


What is the mean c plus plus in machine code?

It is used to distinguish between the C or C++


How do you Open a text file as C plus plus source code?

All C++ source code is is a text file with the .cpp extension. So if you save your code as *****.cpp then it is automatically C++ source code.


What is the Visual C plus plus 2008 Express Serial Code?

The Express edition of C++ does not require a serial code. It is free.


C plus plus code for payroll processing?

yihuy


What are the steps in c plus plus?

Code, compile, link, run.


What is the code to include a library in c plus plus?

#include &lt;libraryname&gt;


What is the differentiate of turbo c from turbo c plus plus?

Turbo C compiles c source. turbo c++ compiles c++ source code.


How do you write a c plus plus program to sort a vector of strings using MSD radix sort?

The standard library sort algorithm automatically uses MSD radix to sort strings: std::vector&lt;std::string&gt; vs = {"a", "b", "c" "d", "ab"}; std::sort(vs.begin(), vs.end()); After sorting, the order will be: {"a", "ab", "b", "c", "d"}