Notes
![]() ![]() Notes - notes.io |
#include<stdlib.h>
#include<stdio.h>
#include<time.h>
// Merges two subarrays of arr[].
// First subarray is arr[l..m]
// Second subarray is arr[m+1..r]
void merge(int arr[], int l, int m, int r)
{
int i, j, k;
int n1 = m - l + 1;
int n2 = r - m;
/* create temp arrays */
int L[n1], R[n2];
/* Copy data to temp arrays L[] and R[] */
for (i = 0; i < n1; i++)
L[i] = arr[l + i];
for (j = 0; j < n2; j++)
R[j] = arr[m + 1+ j];
/* Merge the temp arrays back into arr[l..r]*/
i = 0; // Initial index of first subarray
j = 0; // Initial index of second subarray
k = l; // Initial index of merged subarray
while (i < n1 && j < n2)
{
if (L[i] <= R[j])
{
arr[k] = L[i];
i++;
}
else
{
arr[k] = R[j];
j++;
}
k++;
}
/* Copy the remaining elements of L[], if there
are any */
while (i < n1)
{
arr[k] = L[i];
i++;
k++;
}
/* Copy the remaining elements of R[], if there
are any */
while (j < n2)
{
arr[k] = R[j];
j++;
k++;
}
}
/* l is for left index and r is right index of the
sub-array of arr to be sorted */
void mergeSort(int arr[], int l, int r)
{
if (l < r)
{
// Same as (l+r)/2, but avoids overflow for
// large l and h
int m = l+(r-l)/2;
// Sort first and second halves
mergeSort(arr, l, m);
mergeSort(arr, m+1, r);
merge(arr, l, m, r);
}
}
void merge2(int arr[], int l, int m, int r)
{
int i, j, k;
int n1 = m - l + 1;
int n2 = r - m;
/* create temp arrays */
int L[n1], R[n2];
/* Copy data to temp arrays L[] and R[] */
for (i = 0; i < n1; i++)
L[i] = arr[l + i];
for (j = 0; j < n2; j++)
R[j] = arr[m + 1+ j];
/* Merge the temp arrays back into arr[l..r]*/
i = 0; // Initial index of first subarray
j = 0; // Initial index of second subarray
k = l; // Initial index of merged subarray
while (i < n1 && j < n2)
{
if (L[i] >= R[j])
{
arr[k] = L[i];
i++;
}
else
{
arr[k] = R[j];
j++;
}
k++;
}
/* Copy the remaining elements of L[], if there
are any */
while (i < n1)
{
arr[k] = L[i];
i++;
k++;
}
/* Copy the remaining elements of R[], if there
are any */
while (j < n2)
{
arr[k] = R[j];
j++;
k++;
}
}
void mergeSortdesc(int arr[], int l, int r)
{
if (l < r)
{
// Same as (l+r)/2, but avoids overflow for
// large l and h
int m = l+(r-l)/2;
// Sort first and second halves
mergeSortdesc(arr, l, m);
mergeSortdesc(arr, m+1, r);
merge2(arr, l, m, r);
}
}
/* UTILITY FUNCTIONS */
/* Function to print an array */
void printArray(int A[], int size)
{
int i;
for (i=0; i < size; i++)
printf("%d ", A[i]);
printf("n");
}
/* Driver program to test above functions */
int main()
{ clock_t start,end;
double CPU_time;
int d,i,arr[6],arr_size;
while(d!=6 && d>=1)
{
printf("ntArray will be generated");
for(i=0;i<5;i++){
arr[i]=rand();
}
arr_size = sizeof(arr)/sizeof(arr[0]);
printf("nGenerated array is n");
printArray(arr, arr_size);
printf("nOptionsnt1. Sort array in ascnt2. Sort array in descnt3. Best Casent4. Worst Casent5. Average Casent6. QuitnntChoice : ");
scanf("%d",&d);
switch(d){
case 1: start=clock();
mergeSort(arr, 0, arr_size-1);
end=clock();
CPU_time= ((double)(end-start))/CLOCKS_PER_SEC;
printf("nntSorted array is n");
printArray(arr, arr_size);
printf("ntTime taken:%fnn",CPU_time);
break;
case 2: start=clock();
mergeSortdesc(arr, 0, arr_size-1);
end=clock();
CPU_time= ((double)(end-start))/CLOCKS_PER_SEC;
printf("nntSorted array is n");
printArray(arr, arr_size);
printf("ntTime taken:%fnn",CPU_time);
break;
case 3: start=clock();
mergeSort(arr, 0, arr_size-1);
mergeSort(arr, 0, arr_size-1);
end=clock();
CPU_time= ((double)(end-start))/CLOCKS_PER_SEC;
printf("nntSorted array is n");
printArray(arr, arr_size);
printf("ntTime taken:%fnn",CPU_time);
break;
case 4: start=clock();
mergeSortdesc(arr, 0, arr_size-1);
mergeSort(arr, 0, arr_size-1);
end=clock();
CPU_time= ((double)(end-start))/CLOCKS_PER_SEC;
printf("nntSorted array is n");
printArray(arr, arr_size);
printf("ntTime taken:%fnn",CPU_time);
break;
case 5: start=clock();
mergeSort(arr, 0, arr_size-1);
end=clock();
CPU_time= ((double)(end-start))/CLOCKS_PER_SEC;
printf("nntSorted array is n");
printArray(arr, arr_size);
printf("ntTime taken:%fnn",CPU_time);
break;
case 6: break;
default: printf("wrong choixce");
}
}
return 0;
}
![]() |
Notes is a web-based application for online taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000+ notes created and continuing...
With notes.io;
- * You can take a note from anywhere and any device with internet connection.
- * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
- * You can quickly share your contents without website, blog and e-mail.
- * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
- * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.
Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.
Easy: Notes.io doesn’t require installation. Just write and share note!
Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )
Free: Notes.io works for 14 years and has been free since the day it was started.
You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;
Email: [email protected]
Twitter: http://twitter.com/notesio
Instagram: http://instagram.com/notes.io
Facebook: http://facebook.com/notesio
Regards;
Notes.io Team