Skip to content

Commit 3c77b53

Browse files
committed
More efficient analyzing
1 parent 0fe442d commit 3c77b53

1 file changed

Lines changed: 23 additions & 10 deletions

File tree

src/array/visualizer/sort/BinaryQuickSort.java

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,37 +17,50 @@
1717
public class BinaryQuickSort implements Sort {
1818

1919
public static boolean getBit(int n, int k) {
20+
// Find boolean value of bit k in n
2021
return ((n >> k) & 1) == 1;
2122
}
2223

23-
public static void binaryQuickSort(final ArrayController ac, int p, int r, int pow)
24+
public static int analyzeBit(final ArrayController ac) {
25+
// Find highest bit of highest value
26+
int max = 0;
27+
for(int i = 0; i < ac.length; i++){
28+
ac.marked.set(1, i);
29+
ac.aa++;
30+
sleep(1);
31+
max = Math.max(max, ac.array[i]);
32+
}
33+
return 31 - Integer.numberOfLeadingZeros(max);
34+
}
35+
36+
public static void binaryQuickSort(final ArrayController ac, int p, int r, int bit)
2437
{
25-
if (p < r && pow >= 0)
38+
if (p < r && bit >= 0)
2639
{
27-
int q=partition(ac, p, r, pow);
40+
int q=partition(ac, p, r, bit);
2841
sleep(1);
29-
binaryQuickSort(ac, p, q, pow-1);
30-
binaryQuickSort(ac, q+1, r, pow-1);
42+
binaryQuickSort(ac, p, q, bit-1);
43+
binaryQuickSort(ac, q+1, r, bit-1);
3144
}
3245
}
3346

34-
public static int partition(final ArrayController ac, int p, int r, int pow)
47+
public static int partition(final ArrayController ac, int p, int r, int bit)
3548
{
3649
int i = p - 1;
3750
int j = r + 1;
3851

3952
while (true) {
4053
// Left is not set
4154
i++;
42-
while(i < r && !getBit(ac.array[i], pow)) {
55+
while(i < r && !getBit(ac.array[i], bit)) {
4356
i++;
4457
ac.marked.set(1, i);
4558
sleep(0.45);
4659
ac.comps+=2;
4760
}
4861
// Right is set
4962
j--;
50-
while(j > p && getBit(ac.array[j], pow)) {
63+
while(j > p && getBit(ac.array[j], bit)) {
5164
j--;
5265
ac.marked.set(2, j);
5366
sleep(0.45);
@@ -70,7 +83,7 @@ public String name()
7083
@Override
7184
public void sort(ArrayController ac)
7285
{
73-
int highestpower = analyze(ac, 2);
74-
binaryQuickSort(ac, 0, ac.length-1, highestpower);
86+
int msb = analyzeBit(ac);
87+
binaryQuickSort(ac, 0, ac.length-1, msb);
7588
}
7689
}

0 commit comments

Comments
 (0)