Skip to content

Commit f842883

Browse files
author
suiyueranzly
committed
quicksort on java in Divide and Conquer\Quicksort
1 parent 8d2507d commit f842883

File tree

1 file changed

+68
-0
lines changed

1 file changed

+68
-0
lines changed
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import org.algorithm_visualizer.*;
2+
3+
import java.util.Arrays;
4+
5+
class Main {
6+
7+
private static ChartTracer chartTracer = new ChartTracer();
8+
9+
private static LogTracer logTracer = new LogTracer("Console");
10+
11+
private static Array1DTracer tracer = new Array1DTracer();
12+
13+
private static Integer[] array = (Integer[]) new Randomize.Array1D(15, new Randomize.Integer(1, 20)).create();
14+
15+
public static void main(String[] args) {
16+
tracer.set(array);
17+
tracer.chart(chartTracer);
18+
Layout.setRoot(new VerticalLayout(new Commander[]{chartTracer, tracer, logTracer}));
19+
logTracer.printf("original array = %s\n", Arrays.toString(array));
20+
21+
Tracer.delay();
22+
quickSort(array, 0, array.length - 1);
23+
logTracer.printf("sorted array = %s\n", Arrays.toString(array));
24+
}
25+
26+
public static void quickSort(Integer[] arr, int left, int right) {
27+
int l, r, s;
28+
while (right > left) {
29+
l = left;
30+
r = right;
31+
s = arr[left];
32+
while (l < r) {
33+
tracer.select(left);
34+
tracer.select(right);
35+
Tracer.delay();
36+
while (arr[r] > s) {
37+
tracer.select(r);
38+
Tracer.delay();
39+
tracer.deselect(r);
40+
r--;
41+
}
42+
arr[l] = arr[r];
43+
tracer.patch(l, arr[r]);
44+
Tracer.delay();
45+
tracer.depatch(l);
46+
while (s >= arr[l] && l < r) {
47+
tracer.select(l);
48+
Tracer.delay();
49+
tracer.deselect(l);
50+
l++;
51+
}
52+
arr[r] = arr[l];
53+
tracer.patch(r, arr[l]);
54+
Tracer.delay();
55+
tracer.depatch(r);
56+
tracer.deselect(left);
57+
tracer.deselect(right);
58+
}
59+
arr[l] = s;
60+
tracer.patch(l, s);
61+
Tracer.delay();
62+
tracer.depatch(l);
63+
quickSort(arr, left, l - 1);
64+
left = l + 1;
65+
}
66+
}
67+
68+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy