quick sort program in c with last element as pivot

Solutions on MaxInterview for quick sort program in c with last element as pivot by the best coders in the world

showing results for - "quick sort program in c with last element as pivot"
Greta
19 Aug 2017
1#include<stdio.h>
2int partition(int arr[], int low, int high) {
3  int temp;
4  int pivot = arr[high];
5  int i = (low - 1); 
6  for (int j = low; j <= high - 1; j++) {
7    if (arr[j] <= pivot) { 
8      i++; 
9      temp = arr[i];
10      arr[i] = arr[j];
11      arr[j] = temp;
12    } 
13  } 
14  temp = arr[i + 1];
15  arr[i + 1] = arr[high];
16  arr[high] = temp;
17  return (i + 1); 
18} 
19void quick_sort(int arr[], int low, int high) { 
20  if (low < high) {
21    int pi = partition(arr, low, high); 
22    quick_sort(arr, low, pi - 1); 
23    quick_sort(arr, pi + 1, high); 
24  } 
25} 
26int print(int arr[], int n) {
27  for(int i = 0; i < n; i++) {
28    printf("%d ", arr[i]);
29  }
30}
31
32int main()
33{
34int n, i;
35scanf("%d", &n);
36int arr[n];
37for(i = 0; i < n; i++)
38{
39scanf("%d", &arr[i]);
40}
41quick_sort(arr, 0, n - 1);
42print(arr, n);
43}
44