Notes
Notes - notes.io |
#include<stdio.h>
#include<time.h>
void swap(int* a, int* b)
{
int t = *a;
*a = *b;
*b = t;
}
int partition2 (int arr[], int low, int high)
{
int pivot = arr[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 (arr[j] >= pivot)
{
i++; // increment index of smaller element
swap(&arr[i], &arr[j]);
}
}
swap(&arr[i + 1], &arr[high]);
return (i + 1);
}
void quickSortdesc(int arr[], int low, int high)
{
if (low < high)
{
/* pi is partitioning index, arr[p] is now
at right place */
int pi = partition2(arr, low, high);
// Separately sort elements before
// partition and after partition
quickSortdesc(arr, low, pi - 1);
quickSortdesc(arr, pi + 1, high);
}
}
int partition (int arr[], int low, int high)
{
int pivot = arr[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 (arr[j] <= pivot)
{
i++; // increment index of smaller element
swap(&arr[i], &arr[j]);
}
}
swap(&arr[i + 1], &arr[high]);
return (i + 1);
}
/* The main function that implements QuickSort
arr[] --> Array to be sorted,
low --> Starting index,
high --> Ending index */
void quickSort(int arr[], int low, int high)
{
if (low < high)
{
/* pi is partitioning index, arr[p] is now
at right place */
int pi = partition(arr, low, high);
// Separately sort elements before
// partition and after partition
quickSort(arr, low, pi - 1);
quickSort(arr, pi + 1, high);
}
}
/* Function to print an array */
void printArray(int arr[], int size)
{
int i;
for (i=0; i < size; i++)
printf("%d ", arr[i]);
printf("n");
}
// Driver program to test above functions
int main(void)
{
clock_t start,end;
double CPU_time;
start = clock();
int arr[] = {10, 7, 8, 9, 1, 5};
int n = sizeof(arr)/sizeof(arr[0]);
{ int d;
printf("ntArray generatednt1. Sort array in ascnt2. Sort array in descnt3. Best Casent4. Worst Casent5. Average Casent6. Quitnt ");
scanf("%d",&d);
while(d!=6 || d>=1)
{
switch(d){
case 1: quickSort(arr, 0, n-1);
break;
case 2: quickSortdesc(arr, 0, n-1);
break;
case 3: quickSort(arr, 0, n-1);
quickSort(arr, 0, n-1);
break;
case 4: quickSortdesc(arr, 0, n-1);
quickSort(arr, 0, n-1);
break;
case 5: quickSort(arr, 0, n-1);
break;
case 6: break;
default: printf("wrong choixce");
}
}
}
printf("ntSorted array: ");
printArray(arr, n);
end=clock();
CPU_time=((double)(end-start))/CLOCKS_PER_SEC;
printf("n quick sort took %f seconds to execute n",CPU_time);
return 0;
}
|
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