| 1 | public class Main { |
| 2 | static void displayArr(int[] arr){ |
| 3 | for(int val : arr){ |
| 4 | System.out.print(val + " "); |
| 5 | } |
| 6 | } |
| 7 | static void swap(int[] arr, int x, int y){ |
| 8 | int temp = arr[x]; |
| 9 | arr[x] = arr[y]; |
| 10 | arr[y] = temp; |
| 11 | } |
| 12 | static int partition(int[] arr, int st, int end){ |
| 13 | int pivot = arr[st]; |
| 14 | int cnt = 0; |
| 15 | for(int i = st+1; i <= end; i++){ |
| 16 | if(arr[i] <= pivot) cnt++; |
| 17 | } |
| 18 | int pivotIdx = st + cnt; |
| 19 | swap(arr, st, pivotIdx); |
| 20 | int i = st, j = end; |
| 21 | while(i < pivotIdx && j > pivotIdx){ |
| 22 | while (arr[i] <= pivot) i++; |
| 23 | while (arr[j] > pivot) j--; |
| 24 | if(i < pivotIdx && j > pivotIdx){ |
| 25 | swap(arr, i, j); |
| 26 | i++; |
| 27 | j--; |
| 28 | } |
| 29 | } |
| 30 | return pivotIdx; |
| 31 | } |
| 32 | static void quickSort(int[] arr, int st, int end){ |
| 33 | if(st >= end) return; |
| 34 | int pi = partition(arr, st, end); |
| 35 | quickSort(arr, st, pi-1); |
| 36 | quickSort(arr, pi+1, end); |
| 37 | } |
| 38 | |
| 39 | public static void main(String[] args) { |
| 40 | int[] arr = {6, 6, 3, 1, 5, 5, 4}; |
| 41 | System.out.println("Array before sorting"); |
| 42 | displayArr(arr); |
| 43 | System.out.println(); |
| 44 | quickSort(arr, 0, arr.length-1); |
| 45 | System.out.println("Array after sorting"); // 1 3 4 5 6 |
| 46 | displayArr(arr); |
| 47 | } |
| 48 | } |
nothing calls this directly
no outgoing calls
no test coverage detected