Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- int *a, n;
- void swap(int *a ,int *b){
- int temp = *a;
- *a = *b;
- *b = temp;
- }
- void quickSort(int left,int right){
- if(left < right){
- int pivot = left, i = left, j = right;
- while(i < j){
- while(a[i] <= a[pivot] && i < right) i++;
- while(a[j] > a[pivot]) j--;
- if(i < j) swap(&a[i], &a[j]);
- }
- swap(&a[j], &a[pivot]);
- quickSort(left, j-1);
- quickSort(j+1, right);
- }
- }
- int main(){
- printf("Enter the number of elements: ");
- scanf("%d", &n);
- a = (int *) malloc(sizeof(int) * n);
- printf("\nNow enter %d elements:-\n", n);
- for(int i = 0; i < n; i++) scanf("%d", &a[i]);
- quickSort(0, n-1);
- for(int i = 0; i < n; i++) printf("%d ", a[i]);
- }
- // The array is:-
- // 19 27 5 9 86 45 (n = 6)
- // quickSort(0, n-1)(0, 5) will be called from main().
- // Start of quickSort(0, 5)
- // Since left < right, pivot and i = left = 0 and j = right = 5
- // At the end of a pass in the while loop, i = 1 and j = 3
- // Since i < j, a[i](9) and a[j](27) are swapped.
- // The array is:-
- // 19 9 5 27 86 45
- // At the end of a pass in the while loop, i = 3 and j = 2
- // Since i >= j, no swapping takes place.
- // Since i>=j, control has come out of the while loop.
- // a[j](5) and a[pivot](19) are swapped.
- // The array is:-
- // 5 9 19 27 86 45
- // quickSort(left, j-1)(0, 1) and quickSort(j+1, right)(3, 5) will be called.
- // Start of quickSort(0, 1)
- // Since left < right, pivot and i = left = 0 and j = right = 1
- // At the end of a pass in the while loop, i = 1 and j = 0
- // Since i >= j, no swapping takes place.
- // Since i>=j, control has come out of the while loop.
- // a[j](5) and a[pivot](5) are swapped.
- // The array is:-
- // 5 9 19 27 86 45
- // quickSort(left, j-1)(0, -1) and quickSort(j+1, right)(1, 1) will be called.
- // Start of quickSort(0, -1)
- // Since left >= right, end of quickSort(a, 0, -1)
- // Start of quickSort(1, 1)
- // Since left >= right, end of quickSort(a, 1, 1)
- // After completing quickSort(0, -1) and quickSort(1, 1), quickSort(0, 1) ends
- // Start of quickSort(3, 5)
- // Since left < right, pivot and i = left = 3 and j = right = 5
- // At the end of a pass in the while loop, i = 4 and j = 3
- // Since i >= j, no swapping takes place.
- // Since i>=j, control has come out of the while loop.
- // a[j](27) and a[pivot](27) are swapped.
- // The array is:-
- // 5 9 19 27 86 45
- // quickSort(left, j-1)(3, 2) and quickSort(j+1, right)(4, 5) will be called.
- // Start of quickSort(3, 2)
- // Since left >= right, end of quickSort(a, 3, 2)
- // Start of quickSort(4, 5)
- // Since left < right, pivot and i = left = 4 and j = right = 5
- // At the end of a pass in the while loop, i = 5 and j = 5
- // Since i >= j, no swapping takes place.
- // Since i>=j, control has come out of the while loop.
- // a[j](45) and a[pivot](86) are swapped.
- // The array is:-
- // 5 9 19 27 45 86
- // quickSort(left, j-1)(4, 4) and quickSort(j+1, right)(6, 5) will be called.
- // Start of quickSort(a, 4, 4)
- // Since left >= right, end of quickSort(a, 4, 4)
- // Start of quickSort(6, 5)
- // Since left >= right, end of quickSort(a, 6, 5)
- // After completing quickSort(4, 4) and quickSort(6, 5), quickSort(4, 5) ends
- // After completing quickSort(3, 2) and quickSort(4, 5), quickSort(3, 5) ends
- // After completing quickSort(0, 1) and quickSort(3, 5), quickSort(0, 5) ends
- // The sorted array is:-
- // 5 9 19 27 45 86
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement