NotesWhat is notes.io?

Notes brand slogan

Notes - notes.io

#include<stdio.h>
#include<stdlib.h>
#include<time.h>

int n,*a;
void display();
void generate();
void SortInAsc();
void SortInDesc();
void AnalyzeBest();
void AnalyzeWorst();
void AnalyzeAverage();
void mergeSortAsc(int, int);
void mergeAsc(int, int, int);
void mergeSortDesc(int, int);
void mergeDesc(int, int, int);

int main()
{
int ch;
while(-1)
{
printf("Enter your Choice:n");
printf("0 - Quitn");
printf("1 - Generate n number of values & store them in arrayn");
printf("2 - Sort in Ascending ordern");
printf("3 - Sort in Descending ordern");
printf("4 - Analyze Best Casen");
printf("5 - Analyze Worst Casen");
printf("6 - Analyze Average Casen");
scanf("%d",&ch);
switch(ch)
{
case 0: exit(-1);
break;
case 1: generate();
break;
case 2: SortInAsc();
break;
case 3: SortInDesc();
break;
case 4: AnalyzeBest();
break;
case 5: AnalyzeWorst();
break;
case 6: AnalyzeAverage();
break;
default: printf("Invalid Choicen");
break;
}
}
return 0;
}
void generate()
{
int i;
printf("Enter the value for n: ");
scanf("%d", &n);
a = (int *) malloc (n*sizeof(int));
printf("Entering Random values.n");
for(i=0;i<n;i++)
*(a+i) = rand() % 80;
display();
}
void display()
{
int i;
printf("Displaying Values.n The array is {");
for(i=0;i<n;i++)
printf("%d , ",*(a+i));
printf("} n n");
}
void SortInAsc()
{
printf("Sorting In Ascending Order using Merge Sortn");
mergeSortAsc(0,n-1);
display();
}
void mergeSortAsc(int l, int r)
{
if (l < r)
{
int m = l+(r-l)/2;
mergeSortAsc(l, m);
mergeSortAsc(m+1, r);
mergeAsc(l, m, r);
}
}
void mergeAsc(int l, int m, int r)
{
int i, j, k;
int n1 = m - l + 1;
int n2 = r - m;
int L[n1], R[n2];
for (i = 0; i < n1; i++)
L[i] = *(a+(l + i));
for (j = 0; j < n2; j++)
R[j] = *(a+(m + 1+ j));
i = 0;
j = 0;
k = l;
while (i < n1 && j < n2)
{
if (L[i] <= R[j])
{
*(a+k) = L[i];
i++;
}
else
{
*(a+k) = R[j];
j++;
}
k++;
}
while (i < n1)
{
*(a+k) = L[i];
i++;
k++;
}
while (j < n2)
{
*(a+k) = R[j];
j++;
k++;
}
}
void SortInDesc()
{
printf("Sorting In Descending Order using Merge Sortn");
mergeSortDesc(0,n-1);
display();
}
void mergeSortDesc(int l, int r)
{
if (l < r)
{
int m = l+(r-l)/2;
mergeSortDesc(l, m);
mergeSortDesc(m+1, r);
mergeDesc(l, m, r);
}
}
void mergeDesc(int l, int m, int r)
{
int i, j, k;
int n1 = m - l + 1;
int n2 = r - m;
int L[n1], R[n2];
for (i = 0; i < n1; i++)
L[i] = *(a+(l + i));
for (j = 0; j < n2; j++)
R[j] = *(a+(m + 1+ j));
i = 0;
j = 0;
k = l;
while (i < n1 && j < n2)
{
if (L[i] >= R[j])
{
*(a+k) = L[i];
i++;
}
else
{
*(a+k) = R[j];
j++;
}
k++;
}
while (i < n1)
{
*(a+k) = L[i];
i++;
k++;
}
while (j < n2)
{
*(a+k) = R[j];
j++;
k++;
}
}
void AnalyzeBest()
{
clock_t start, end;
double timeUsed;
start = clock();
SortInAsc();
end = clock();
timeUsed = ((double) (end - start)) / CLOCKS_PER_SEC;
printf("For BEST CASE, time analysis is %fn", timeUsed);
}
void AnalyzeWorst()
{
clock_t start, end;
double timeUsed;
start = clock();
SortInDesc();
end = clock();
timeUsed = ((double) (end - start)) / CLOCKS_PER_SEC;
printf("For WORST CASE, time analysis is %fn", timeUsed);
}
void AnalyzeAverage();
{
}
Write a note in this area. It's really easy to share with others. Click here ...
     
 
what is notes.io
 

Notes.io is a web-based application for 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 12 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

     
 
Shortened Note Link
 
 
Looding Image
 
     
 
Long File
 
 

For written notes was greater than 18KB Unable to shorten.

To be smaller than 18KB, please organize your notes, or sign in.