Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- void swap(int *a,int *b){
- int temp = *a;
- *a = *b;
- *b = temp;
- }
- int Partition (int arr[], int low, int high)
- {
- int pivot = arr[high]; // pivot
- int i = (low - 1); // Index of smaller element and indicates the right position of pivot found so far
- for (int j = low; j <= high - 1; j++)
- {
- // If current element is smaller than the 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 Quicksort(int arr[], int low, int high)
- {
- if (low < high)
- {
- int pi = Partition(arr, low, high);
- Quicksort(arr, low, pi-1);
- Quicksort(arr, pi + 1, high);
- }
- }
- int main()
- {
- int n;
- scanf("%d", &n);
- int arr[n];
- for (int i = 0; i < n; i++) scanf("%d", &arr[i]);
- Quicksort(arr, 0, n - 1);
- for(int i=0 ; i<n ; i++) printf("%d ",arr[i]);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement