0% found this document useful (0 votes)
1 views3 pages

Merge Sort

ADA MERGE SORE

Uploaded by

Dushyanth ms
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views3 pages

Merge Sort

ADA MERGE SORE

Uploaded by

Dushyanth ms
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

#include <stdio.

h>

#include <stdlib.h>

#include <time.h>

#define MAX_SIZE 25000

void merge(int a[], int low, int mid, int high) {

int h = low, i = low, j = mid + 1, k;

int b[MAX_SIZE];

while (h <= mid && j <= high) {

if (a[h] <= a[j]) {

b[i] = a[h];

h++;

} else {

b[i] = a[j];

j++;

i++;

while (h <= mid) {

b[i] = a[h];

h++;

i++;

}
while (j <= high) {

b[i] = a[j];

j++;

i++;

for (k = low; k <= high; k++) {

a[k] = b[k];

void mergesort(int a[], int low, int high) {

if (low < high) {

int mid = (low + high) / 2;

mergesort(a, low, mid);

mergesort(a, mid + 1, high);

merge(a, low, mid, high);

int main() {

int a[MAX_SIZE], n, k;

clock_t st, et;

double tt;

printf("Enter number of elements (max %d): ", MAX_SIZE);

scanf("%d", &n);
if (n > MAX_SIZE) {

printf("Number of elements exceeds the maximum allowed size of %d.\n", MAX_SIZE);

return 1;

printf("Elements to sort:\n");

srand(time(NULL));

for (k = 0; k < n; k++) {

a[k] = rand() % 100 + 1;

printf("%d\t", a[k]);

printf("\n");

st = clock();

mergesort(a, 0, n - 1);

et = clock();

tt = (double)(et - st) / CLOCKS_PER_SEC;

printf("Sorted array elements are:\n");

for (k = 0; k < n; k++) {

printf("%d\t", a[k]);

printf("\n");

printf("Time taken = %f seconds\n", tt);

return 0;

You might also like