Created
December 15, 2017 15:50
-
-
Save rishi93/28a4b29afa92911e4e02833c7daa4672 to your computer and use it in GitHub Desktop.
Quick sort(Implementation in C)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #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