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 quickSort2(int, int);
void analyzeWorst();
//void analyzeAverage();
void quickSortAsc(int, int);
int partition (int, int);
void swap(int*, int*);

int main()
{
int ch;
while(-1) {
printf("Enter your choice:n");
printf("0-Quitn");
printf("1-Generate n numbers and store an arrayn");
printf("2-Sort the array in ascending ordern");
printf("3-Sort the array in descending ordern");
printf("4-Analyse 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 Choice");
break;
}
}
return 0;
}
void generate()
{
int i;
printf("Enter the size of the array: ");
scanf("%d", &n);
a = (int*) malloc (n*sizeof(int));
for(i=0;i<n;i++)
*(a+i) = rand() % 80;
printf("Random Array Generatedn");
display();
}
void display()
{
int i;
printf("The Array Would be: {");
for(i=0;i<n;i++)
printf("%d ,", *(a+i));
printf("}nnn");
}
void sortInAsc()
{
printf("Sorting the array In Ascendingn");
quickSortAsc(0,n-1);
display();
}


void quickSortAsc(int low, int high)
{
if (low < high)
{
int pi = partition(low, high);
quickSort1(low, pi - 1);
quickSort1(pi + 1, high);
}
}
int partition (int low, int high)
{
int pivot = a[high]; // pivot
int i = (low - 1); // Index of smaller element

for (int j = low; j <= high- 1; j++)
{
// If current element is smaller than or
// equal to pivot
if (a[j] <= pivot)
{
i++; // increment index of smaller element
swap(&a[i], &a[j]);
}
}
swap(&a[i + 1], &a[high]);
return (i + 1);
}
void swap(int* a, int* b)
{
int t = *a;
*a = *b;
*b = t;
}

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 sortInDesc()
{
printf("Sorting the array In Descendingn");
quickSort2(n-1,0);
display();
}*/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.