Skip to content

Instantly share code, notes, and snippets.

@rishi93
Created December 15, 2017 15:50
Show Gist options
  • Select an option

  • Save rishi93/28a4b29afa92911e4e02833c7daa4672 to your computer and use it in GitHub Desktop.

Select an option

Save rishi93/28a4b29afa92911e4e02833c7daa4672 to your computer and use it in GitHub Desktop.
Quick sort(Implementation in C)
#include <stdio.h>
void quicksort(int arr[], int start, int stop){
if(start < stop){
int left = start;
int right = stop;
int pivot = arr[start];
while(arr[left] < pivot){
left += 1;
}
while(arr[right] > pivot){
right -= 1;
}
if(left <= right){
int temp = arr[left];
arr[left] = arr[right];
arr[right] = temp;
left += 1;
right -= 1;
}
quicksort(arr, start, right);
quicksort(arr, left, stop);
}
}
int main(){
int arr[] = {5, 2, 3, 4, 1};
int n = sizeof(arr)/sizeof(arr[0]);
quicksort(arr, 0, n-1);
printf("After sorting\n");
for(int i = 0; i < n; i++){
printf("%d ", arr[i]);
}
printf("\n");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment