Skip to content

Commit f4f497d

Browse files
author
suiyueranzly
committed
Selection Sort on Java in Brute Force\Selection Sort
1 parent 28c8a23 commit f4f497d

File tree

1 file changed

+55
-0
lines changed

1 file changed

+55
-0
lines changed

Brute Force/Selection Sort/code.java

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import org.algorithm_visualizer.*;
2+
3+
import java.util.Arrays;
4+
5+
public class Main {
6+
7+
private static ChartTracer chartTracer = new ChartTracer();
8+
9+
private static LogTracer logTracer = new LogTracer("Console");
10+
11+
private static Integer [] array = (Integer[]) new Randomize.Array1D(15, new Randomize.Integer(1, 20)).create();
12+
13+
public static void main(String[] args) {
14+
int length = array.length;
15+
Layout.setRoot(new VerticalLayout(new Commander[]{chartTracer, logTracer}));
16+
logTracer.printf("original array = %s\n", Arrays.toString(array));
17+
chartTracer.set(array);
18+
Tracer.delay();
19+
int minIndex;
20+
21+
for (int i = 0; i < length; i++) {
22+
chartTracer.select(i);
23+
Tracer.delay();
24+
minIndex = i;
25+
for (int j = i + 1; j < length; j++){
26+
chartTracer.select(j);
27+
Tracer.delay();
28+
if(array[j] < array[minIndex]) {
29+
chartTracer.patch(j, array[j]);
30+
Tracer.delay();
31+
chartTracer.depatch(j);
32+
minIndex = j;
33+
}
34+
chartTracer.deselect(j);
35+
}
36+
swap(minIndex, i, array);
37+
chartTracer.deselect(i);
38+
}
39+
40+
logTracer.printf("sorted array = %s\n", Arrays.toString(array));
41+
}
42+
43+
44+
private static void swap(int x, int y, Integer[] array) {
45+
int temp = array[x];
46+
array[x] = array[y];
47+
array[y] = temp;
48+
chartTracer.patch(x, array[x]);
49+
chartTracer.patch(y, array[y]);
50+
Tracer.delay();
51+
chartTracer.depatch(x);
52+
chartTracer.depatch(y);
53+
}
54+
55+
}

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