Quick Sort
Quicksort is an in-place sorting algorithm which means it doesn't take an additional array to sort the data. It uses the same array to sort the elements.
Advantages;
- The quick sort is regarded as the best sorting algorithm.
- It is able to deal well with a huge list of items.
- Because it sorts in place, no additional storage is required as well
- The slight disadvantage of quick sort is that its worst-case performance is similar to average performances of the bubble, insertion or selections sorts.
- If the list is already sorted than bubble sort is much more efficient than quick sort
- If the sorting element is integers than radix sort is more efficient than quick sort.
Complexity of the Quick Sort Algorithm;
- Data structure: Array
- Worst-case time complexity: O(n2)
- Average-case time complexity Θ(nlogn)
- Best-case time complexity: Ω(nlogn)
- Worst-case space complexity: O(logn)
CODE:
- #include <stdio.h>
- void quicksort(int a[20], int low, int high);
- int partition(int a[], int low, int high);
- void main()
- {
- int a[20],n,i;
- printf("How many elements you want to enter \n");
- scanf("%d", &n);
- printf("Enter %d elements \t",n);
- for(i=0; i<n; i++)
- {
- scanf("%d", &a[i]);
- }
- quicksort(a,0,n-1);
- printf("Sorted elements are \t");
- for(i=0; i<n; i++)
- {
- printf("%d\t",a[i]);
- }
- }
- void quicksort(int a[20], int low, int high)
- {
- int j;
- if(low<high)
- {
- j=partition(a,low,high);
- quicksort(a,low,j-1);
- quicksort(a, j+1, high);
- }
- }
- int partition(int a[], int low, int high)
- {
- int end,start,temp,pivot;
- end=high;
- start=low+1;
- pivot=a[low];
- do
- {
- while((a[start] <= pivot) && (start < end))
- start++;
- while((a[end] > pivot) && (end > low))
- end--;
- if(start < end)
- {
- temp=a[start];
- a[start]=a[end];
- a[end]=temp;
- }
- }while(start<end);
- a[low]=a[end];
- a[end]=pivot;
- return end;
- }
//Execute
//Output
/*
How many elements you want to enter
5
Enter 5 elements 78
41
54
67
84
Sorted elements are 41 54 67 78 84
*/
EXPLANATION:
- The code is a function that takes an array of integers and sorts them.
- The code starts by declaring the variables it will use, then declares the quicksort function which is defined in line 3.
- Line 4 defines what happens if low < high, which is to call partition(a,low,high) with low as the first parameter and high as the second parameter.
- Then lines 5-7 define what happens if high > low, which is to call partition(a,low+1,high).
- Lines 8-9 define what happens when start <= pivot (which means start is less than or equal to pivot), so line 10 sets up a while loop that goes through each element from start until end (line 11).
- Line 12 sets up another while loop that goes through each element from end until start (line 13).
- Line 14 sets up a do/while loop where line 15 checks whether both loops are true before continuing on with whatever else needs to happen in this part of the program.
- The main() function begins by asking for how many elements you want enter using printf("How many elements you want to enter \n"); followed by scanning those values into n using scanf("%d", &n); followed then printing out "Enter.
//ThE ProFessoR
Comments
Post a Comment