Created
December 15, 2017 15:10
-
-
Save rishi93/0b21d97b0362f8bc30bec95c9d03eb8d to your computer and use it in GitHub Desktop.
Quick sort (Implementation in Java)
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
| class QuickSort{ | |
| public 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); | |
| } | |
| } | |
| } | |
| public class Test{ | |
| public static void main(String[] args){ | |
| QuickSort ob = new QuickSort(); | |
| int[] arr = {5, 2, 3, 4, 1}; | |
| int n = arr.length; | |
| ob.quicksort(arr, 0, n-1); | |
| System.out.println("After sorting"); | |
| for(int i = 0; i < n; i++){ | |
| System.out.print(arr[i] + " "); | |
| } | |
| System.out.println(); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment