answersLogoWhite

0

#include <stdio.h>

#include <stdlib.h>

#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 < MAXARRAY; i++)

array[i] = rand() % 100;

/* array before mergesort */

printf("Before :");

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

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

printf("\n");

mergesort(array, 0, MAXARRAY - 1);

/* array after mergesort */

printf("Mergesort :");

for(i = 0; i < 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 < length; i++)

working[i] = a[low + i];

merge1 = 0;

merge2 = pivot - low + 1;

for(i = 0; i < length; i++) {

if(merge2 <= high - low)

if(merge1 <= pivot - low)

if(working[merge1] > 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++];

}

}

User Avatar

Wiki User

13y ago

What else can I help you with?

Related Questions

Can you use a sequential search on an unsorted array?

Sequential search is the only way to search an unsorted array unless you resort to a multi-threaded parallel search where all threads concurrently search a portion of the array sequentially.


What is the most efficient way to find the median of an unsorted array of numbers?

One efficient way to find the median of an unsorted array of numbers is to first sort the array in either ascending or descending order, then determine the middle value as the median.


Program to insert an element in array at beginning?

Answer: Use the unshift() Method You can use the unshift() method to easily add new elements or values at the beginning of an array in JavaScript. This method is a counterpart of the push() method, which adds the elements at the end of an array. However, both method returns the new length of the array To learn more about data science please visit- Learnbay.co


What is the best search algorithm to use for an unsorted array?

The best search algorithm to use for an unsorted array is linear search. It involves checking each element in the array one by one until the desired element is found. This algorithm has a time complexity of O(n), where n is the number of elements in the array.


What is the median of an unsorted array of numbers?

The median of an unsorted array of numbers is the middle value when the numbers are arranged in numerical order. It divides the array into two equal parts, with half of the numbers being greater than the median and half being less than the median.


Could you Write a program for 8086 microprocessor that displays on the monitor the average of 2 numbers from an array?

How to write a program for mouse in microprocessor?


How do you write a C Program to fill up an Integer Array?

Reference:cprogramming-bd.com/c_page1.aspx# array programming


How do you write a program to read set of numbers using by an array and display the ascending order of the given input numbers?

To write a C++ program to display the student details using class and array of object.


What is the process for finding the kth smallest number in an unsorted array?

To find the kth smallest number in an unsorted array, you can use a sorting algorithm like quicksort or heapsort to arrange the array in ascending order. Then, you can simply access the kth element in the sorted array to find the kth smallest number. This process ensures that the kth smallest number is easily identified and retrieved from the array.


How do you write an assembly language program to find the sum of n numbers using array?

write an assembly language program to find sum of N numbers


How do you write a program in C to find and display the sum of each row and column of a 2 dimensional array of type float?

array type


Write a c program to find the maximum value of 25 element in an array?

int findMax(int *array) { int max = array[0]; for(int i = 1; i &lt; array.length(); i++) { if(array[i] &gt; max) max = array[i] } return max; }