Skip to content

Commit 6906600

Browse files
authored
Merge pull request w0rthy#12 from Skeen/binaryQuickSort
Introduce binary QuickSort
2 parents 904fc84 + 3c77b53 commit 6906600

File tree

2 files changed

+97
-5
lines changed

2 files changed

+97
-5
lines changed

src/array/visualizer/ArrayVisualizer.java

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public class ArrayVisualizer {
6666
static ArrayList<Color> COLORSTRAT2cols = new ArrayList<Color>();
6767

6868
static String[] ComparativeSorts = "Selection!Bubble!Insertion!Double Selection!Cocktail Shaker!Quick!Merge!Merge OOP!Weave Merge!Max Heap!Shell".split("!");
69-
static String[] DistributiveSorts = "Radix LSD!Radix MSD!Radix LSD In-Place!Gravity!Shatter!Counting!Time!Bogo".split("!");
69+
static String[] DistributiveSorts = "Radix LSD!Radix MSD!Radix LSD In-Place!Binary Quicksort!Gravity!Shatter!Counting!Time!Bogo".split("!");
7070

7171
static int cx = 0;
7272
static int cy = 0;
@@ -657,6 +657,7 @@ public void run(){
657657
new GravitySort(),
658658
new RadixLSD(4),
659659
new RadixMSD(4),
660+
new BinaryQuickSort(),
660661
new RadixLSDInPlace(2),
661662
new RadixLSDInPlace(10)}
662663
)
@@ -766,14 +767,16 @@ public void run(){
766767
case 2:
767768
sort = new RadixLSDInPlace(base);break;
768769
case 3:
769-
sort = new GravitySort();break;
770+
sort = new BinaryQuickSort();break;
770771
case 4:
771-
sort = new ShatterSorts(base);break;
772+
sort = new GravitySort();break;
772773
case 5:
773-
sort = new CountingSort();break;
774+
sort = new ShatterSorts(base);break;
774775
case 6:
775-
sort = new TimeSort(base);break;
776+
sort = new CountingSort();break;
776777
case 7:
778+
sort = new TimeSort(base);break;
779+
case 8:
777780
sort = new BogoSort();break;
778781
default:
779782
sort = null; break;
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/*
2+
* To change this template, choose Tools | Templates
3+
* and open the template in the editor.
4+
*/
5+
package array.visualizer.sort;
6+
7+
import array.visualizer.ArrayController;
8+
9+
import static array.visualizer.ArrayVisualizer.*;
10+
import static array.visualizer.utils.Analysis.*;
11+
import static array.visualizer.utils.Swaps.*;
12+
13+
/**
14+
*
15+
* @author Skeen
16+
*/
17+
public class BinaryQuickSort implements Sort {
18+
19+
public static boolean getBit(int n, int k) {
20+
// Find boolean value of bit k in n
21+
return ((n >> k) & 1) == 1;
22+
}
23+
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)
37+
{
38+
if (p < r && bit >= 0)
39+
{
40+
int q=partition(ac, p, r, bit);
41+
sleep(1);
42+
binaryQuickSort(ac, p, q, bit-1);
43+
binaryQuickSort(ac, q+1, r, bit-1);
44+
}
45+
}
46+
47+
public static int partition(final ArrayController ac, int p, int r, int bit)
48+
{
49+
int i = p - 1;
50+
int j = r + 1;
51+
52+
while (true) {
53+
// Left is not set
54+
i++;
55+
while(i < r && !getBit(ac.array[i], bit)) {
56+
i++;
57+
ac.marked.set(1, i);
58+
sleep(0.45);
59+
ac.comps+=2;
60+
}
61+
// Right is set
62+
j--;
63+
while(j > p && getBit(ac.array[j], bit)) {
64+
j--;
65+
ac.marked.set(2, j);
66+
sleep(0.45);
67+
ac.comps+=2;
68+
}
69+
// If i is less than j, we swap, otherwise we are done
70+
if (i < j)
71+
swap(ac, i, j);
72+
else
73+
return j;
74+
}
75+
}
76+
77+
@Override
78+
public String name()
79+
{
80+
return "Binary Quicksort";
81+
}
82+
83+
@Override
84+
public void sort(ArrayController ac)
85+
{
86+
int msb = analyzeBit(ac);
87+
binaryQuickSort(ac, 0, ac.length-1, msb);
88+
}
89+
}

0 commit comments

Comments
 (0)