Asymptotic Complexity | ||
Something | Time Complexity | Space Complexity |
Worst Case Performance | O (n^2) | |
Best Case Performance | O (n log n) | |
Average Case Performance | O(n log n) | |
Worst Case space Complexity | O (log n) (for in-place) |
There are implementations of Quicksort algorithm using new new array creation and thereby does a bad job with regard to space complexity. Here is an inplace quicksort algorithm implementation in C. If you are interested in the implementation in python please click the following link: Python implementation of inplace quicksort
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<stdio.h> | |
void quicksort(int [10],int,int); | |
int main(){ | |
int x[20],size,i; | |
printf("Enter size of the array: "); | |
scanf("%d",&size); | |
printf("Enter %d elements: ",size); | |
for(i=0;i<size;i++) | |
scanf("%d",&x[i]); | |
quicksort(x,0,size-1); | |
printf("Sorted elements: "); | |
for(i=0;i<size;i++) | |
printf(" %d",x[i]); | |
return 0; | |
} | |
void quicksort(int x[10],int first,int last){ | |
int pivot,j,temp,i; | |
if(first<last){ | |
pivot=first; | |
i=first; | |
j=last; | |
while(i<j){ | |
while(x[i]<=x[pivot]&&i<last) | |
i++; | |
while(x[j]>x[pivot]) | |
j--; | |
if(i<j){ | |
temp=x[i]; | |
x[i]=x[j]; | |
x[j]=temp; | |
} | |
} | |
temp=x[pivot]; | |
x[pivot]=x[j]; | |
x[j]=temp; | |
quicksort(x,first,j-1); | |
quicksort(x,j+1,last); | |
} | |
} |