Skip to content

Commit c90d627

Browse files
author
8h
committed
Quick Sort
1 parent 038544c commit c90d627

File tree

1 file changed

+35
-1
lines changed

1 file changed

+35
-1
lines changed

src/main/java/com/algorithms/sort/QuickSort.java

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ public class QuickSort {
55
public static void main(String[] args) {
66

77
int[] array = {10,8,100,3,5,2,1,10};
8-
QuickSort quickSort = new QuickSort();
98
quickSort(array, 0, array.length-1);
109

1110
for(int i=0; i< array.length; i++) {
@@ -54,5 +53,40 @@ public static void quickSort(int[] array, int low, int high) {
5453

5554
}
5655

56+
public static void quickSortRecursive(int[] array, int low, int high) {
57+
58+
if(low > high) {
59+
return;
60+
}
61+
62+
int partitionIndex = partition(array, low, high);
63+
quickSortRecursive(array, low, partitionIndex-1);
64+
quickSortRecursive(array, partitionIndex+1, high);
65+
66+
}
67+
68+
public static int partition(int[] array, int low, int high) {
69+
70+
int pivot = array[high];
71+
int index = low-1;
72+
73+
for(int i=low; i<high; i++) {
74+
75+
if(array[i] <= pivot) {
76+
index++;
77+
int temp = array[i];
78+
array[i] = array[index];
79+
array[index] = temp;
80+
}
81+
82+
}
83+
84+
int temp = array[index+1];
85+
array[index+1] = array[high];
86+
array[high] = temp;
87+
88+
return index+1;
89+
}
90+
5791

5892
}

0 commit comments

Comments
 (0)