Skip to content

Commit 96be31a

Browse files
committed
Added Insertionsort
1 parent 2a7c18a commit 96be31a

4 files changed

Lines changed: 27 additions & 12 deletions

File tree

Binary file not shown.

src/com/marcomsl/algorithmvisualization/AlgorithmVisualizer.java

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import java.util.ArrayList;
44
import java.util.Random;
5+
import java.util.Timer;
56

67
public class AlgorithmVisualizer {
78

@@ -10,32 +11,32 @@ public class AlgorithmVisualizer {
1011
private int[] array;
1112
private ArrayList<Integer> arrayList = new ArrayList<>();
1213

13-
public AlgorithmVisualizer(){
14+
public AlgorithmVisualizer() {
1415
array = new int[50];
1516
fillArray();
1617
algorithmFrame = new AlgorithmFrame(array);
1718

1819
}
1920

20-
public AlgorithmVisualizer(int size){
21-
if(size >= 25 && size <= 50) {
21+
public AlgorithmVisualizer(int size) {
22+
if (size >= 25 && size <= 50) {
2223
array = new int[size];
23-
}else{
24+
} else {
2425
array = new int[50];
2526
}
2627
fillArray();
2728
algorithmFrame = new AlgorithmFrame(array);
2829

2930
}
3031

31-
private void fillArray(){
32-
for(int i = 0; i < array.length; i++){
33-
arrayList.add ((i + 1) * 6);
32+
private void fillArray() {
33+
for (int i = 0; i < array.length; i++) {
34+
arrayList.add((i + 1) * 6);
3435
}
3536

3637
Random random = new Random();
3738

38-
for(int i = 0; i < array.length; i++){
39+
for (int i = 0; i < array.length; i++) {
3940
int randomIndex = random.nextInt(arrayList.size());
4041
array[i] = arrayList.get(randomIndex);
4142
arrayList.remove(randomIndex);
@@ -46,13 +47,13 @@ public int[] getArray() {
4647
return array;
4748
}
4849

49-
public void resetArray(){
50+
public void resetArray() {
5051
fillArray();
5152
}
5253

5354
public void bubblesort(int animationSpeed) throws InterruptedException {
5455
int temp;
55-
for(int x = 1; x < array.length; x++){
56+
for (int x = 1; x < array.length; x++) {
5657
for (int b = 0; b < array.length - x; b++) {
5758
if (array[b] > array[b + 1]) {
5859
Thread.sleep(animationSpeed);
@@ -64,5 +65,19 @@ public void bubblesort(int animationSpeed) throws InterruptedException {
6465
}
6566
}
6667

68+
public void insertionSort(int animationSpeed) throws InterruptedException {
69+
int temp;
70+
for (int i = 1; i < array.length; i++) {
71+
temp = array[i];
72+
int j = i;
73+
while (j > 0 && array[j - 1] > temp) {
74+
Thread.sleep(animationSpeed);
75+
array[j] = array[j - 1];
76+
j--;
77+
}
78+
array[j] = temp;
79+
}
80+
}
81+
6782

6883
}

src/com/marcomsl/algorithmvisualization/Main.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ public class Main {
66
// main function for testing
77
public static void main(String[] args) throws InterruptedException {
88

9-
AlgorithmVisualizer algorithmVisualizer = new AlgorithmVisualizer(40);
10-
algorithmVisualizer.bubblesort(15);
9+
AlgorithmVisualizer algorithmVisualizer = new AlgorithmVisualizer(50);
10+
algorithmVisualizer.insertionSort(25);
1111
algorithmVisualizer.resetArray();
1212
algorithmVisualizer.bubblesort(15);
1313
}

0 commit comments

Comments
 (0)