Skip to content

Commit ccf39e4

Browse files
author
codelogicws
committed
QuickSort
is working
1 parent 98e1903 commit ccf39e4

File tree

4 files changed

+58
-2
lines changed

4 files changed

+58
-2
lines changed
Binary file not shown.
Binary file not shown.

src/ws/codelogic/algorithms/sort/QuickSort.java

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,70 @@
22

33
public class QuickSort extends Sort{
44

5+
private Comparable temp;
6+
57
public QuickSort(Comparable[] array){
68
this.array = array;
79
}
810

911
@Override
1012
public void sort() {
13+
recursiveSort(0, array.length-1);
14+
}
15+
16+
private void recursiveSort(int low, int high){
17+
int mid = partitioning(low, high);
18+
if(low < mid-1){
19+
recursiveSort(low, mid-1);
20+
}
21+
if(mid+1 < high){
22+
recursiveSort(mid + 1, high);
23+
}
24+
System.out.println("Test");
25+
debugPrintArray();
26+
}
27+
28+
public int partitioning(int low, int high){
29+
int pivot = low;
30+
high++;
31+
32+
while(low<=high) {
33+
while (less(array[++low], array[pivot])) ;
34+
while (less(array[pivot], array[--high])) ;
35+
debug(low, high);
36+
swap(low, high);
37+
System.out.println("Swtich");
38+
debug(low, high);
39+
}
40+
swap(pivot, high);
41+
debugPrintArray();
42+
return high;
43+
}
1144

45+
private void debugPrintArray() {
46+
for(Comparable c : array){
47+
printWithComma(c);
48+
}
1249
}
1350

51+
private void debug(int low, int high){
52+
System.out.println("Debug-QuickSort low is: " + array[low] + " high is: " + array[high]);
53+
}
54+
55+
private void printWithComma(Comparable c){
56+
System.out.print(c + ", ");
57+
}
58+
59+
private void swap(int low, int high) {
60+
if(low<high){
61+
temp = array[low];
62+
array[low] = array[high];
63+
array[high] = temp;
64+
}
65+
}
66+
67+
private boolean less(Comparable less, Comparable more){
68+
return less.compareTo(more) == -1;
69+
}
1470

1571
}

src/ws/codelogic/algorithms/sort/QuickSortTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
public class QuickSortTest {
88

99
private Sort quickSort;
10-
private Integer[] unsorted = {9,8,7,4,3,2,6,5,1};
11-
private Integer[] sorted = {1,2,3,4,5,6,7,8,9};
10+
private Integer[] unsorted = {5,4,3,6,7,2,1,5,9};
11+
private Integer[] sorted = {1,2,3,4,5,5,6,7,9};
1212

1313
@Before
1414
public void setup() {

0 commit comments

Comments
 (0)