answersLogoWhite

0

Simple c program for mergesort

Updated: 12/23/2022
User Avatar

Wiki User

9y ago

Best Answer

#include

#include

#define MAX_ARY 10

void merge_sort(int x[], int end, int start);

int main(void) {

int ary[MAX_ARY];

int j = 0;

printf("\n\nEnter the elements to be sorted: \n");

for(j=0;j scanf("%d",&ary[j]);

/* array before mergesort */

printf("Before :");

for(j = 0; j < MAX_ARY; j++)

printf(" %d", ary[j]);

printf("\n");

merge_sort(ary, 0, MAX_ARY - 1);

/* array after mergesort */

printf("After Merge Sort :");

for(j = 0; j < MAX_ARY; j++)

printf(" %d", ary[j]);

printf("\n");

getch();

}

/* Method to implement Merge Sort*/

void merge_sort(int x[], int end, int start) {

int j = 0;

const int size = start - end + 1;

int mid = 0;

int mrg1 = 0;

int mrg2 = 0;

int executing[MAX_ARY];

if(end == start)

return;

mid = (end + start) / 2;

merge_sort(x, end, mid);

merge_sort(x, mid + 1, start);

for(j = 0; j < size; j++)

executing[j] = x[end + j];

mrg1 = 0;

mrg2 = mid - end + 1;

for(j = 0; j < size; j++) {

if(mrg2 <= start - end)

if(mrg1 <= mid - end)

if(executing[mrg1] > executing[mrg2])

x[j + end] = executing[mrg2++];

else

x[j + end] = executing[mrg1++];

else

x[j + end] = executing[mrg2++];

else

x[j + end] = executing[mrg1++];

}

}

;j++)>

User Avatar

Wiki User

9y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Simple c program for mergesort
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

What is algorithms in c or c plus plus?

it is a step by step program written in simple English for our understanding


Why you use count in simple interest program in c?

You mean 'count' as variable-name? It is optional.


How do you write a C program to find the Simple Interest?

Reference:cprogramming-bd.com/c_page1.aspx#simpleinterest


You want to write a simple without using pointer or array c program which will print greatest number when you give 20 number?

i want to write a simple without using pointer or array c program which will print greatest number when i give 20 number .........How far have you gotten so far?


Assembly language programming on mergesort?

try making yourself..hope it will be useful ofr you!

Related questions

Simple c language program?

Well, this is a very open question, please be specific, I will try try to answer this as well, a simple c language program can be any thing from printing your name on the screen to printing some patterns using symbols, or making a small game, or a simple calculator program. you can a clearer picture of what can be a simple c language program here:


What is algorithms in c or c plus plus?

it is a step by step program written in simple English for our understanding


C program algorithm for simple interest using while loop?

Not used


Write a program in c to sort an unsorted array using merge sort method?

#include &lt;stdio.h&gt; #include &lt;stdlib.h&gt; #define MAXARRAY 10 void mergesort(int a[], int low, int high); int main(void) { int array[MAXARRAY]; int i = 0; /* load some random values into the array */ for(i = 0; i &lt; MAXARRAY; i++) array[i] = rand() % 100; /* array before mergesort */ printf("Before :"); for(i = 0; i &lt; MAXARRAY; i++) printf(" %d", array[i]); printf("\n"); mergesort(array, 0, MAXARRAY - 1); /* array after mergesort */ printf("Mergesort :"); for(i = 0; i &lt; MAXARRAY; i++) printf(" %d", array[i]); printf("\n"); return 0; } void mergesort(int a[], int low, int high) { int i = 0; int length = high - low + 1; int pivot = 0; int merge1 = 0; int merge2 = 0; int working[length]; if(low == high) return; pivot = (low + high) / 2; mergesort(a, low, pivot); mergesort(a, pivot + 1, high); for(i = 0; i &lt; length; i++) working[i] = a[low + i]; merge1 = 0; merge2 = pivot - low + 1; for(i = 0; i &lt; length; i++) { if(merge2 &lt;= high - low) if(merge1 &lt;= pivot - low) if(working[merge1] &gt; working[merge2]) a[i + low] = working[merge2++]; else a[i + low] = working[merge1++]; else a[i + low] = working[merge2++]; else a[i + low] = working[merge1++]; } }


How do you write a C program to find the Simple Interest?

Reference:cprogramming-bd.com/c_page1.aspx#simpleinterest


Why you use count in simple interest program in c?

You mean 'count' as variable-name? It is optional.


Write a c program for false position method?

Write a simple program in finding roots x^3-6x^2+11x-6.1=0


You want to write a simple without using pointer or array c program which will print greatest number when you give 20 number?

i want to write a simple without using pointer or array c program which will print greatest number when i give 20 number .........How far have you gotten so far?


Show the simple c plus plus program?

int main (void) { puts ("Hello, world"); return 0; }


Assembly language programming on mergesort?

try making yourself..hope it will be useful ofr you!


Features of c program?

the features of a C program


C program was introduced in the year?

c program was introduced in the year 1972 by Dennis RitchieNo, it was the C language, not the C program.