Skip to content

Commit 1a4c55d

Browse files
committed
Add BubbleSort folder with Java files
1 parent 981426d commit 1a4c55d

File tree

3 files changed

+156
-0
lines changed

3 files changed

+156
-0
lines changed

BubbleSort/BubbleSort.java

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
public class BubbleSort {
2+
3+
public int basicAscending(int[] collection) {
4+
int collectionLen = collection.length;
5+
int comparisons = 0;
6+
7+
for (int i = 0; i < collectionLen; i++) {
8+
for (int j = 0; j < (collectionLen - 1); j++) {
9+
comparisons++;
10+
if (collection[j] > collection[j + 1]) {
11+
int temp = collection[j];
12+
collection[j] = collection[j + 1];
13+
collection[j + 1] = temp;
14+
}
15+
}
16+
}
17+
return comparisons;
18+
}
19+
20+
public int basicDescending(int[] collection) {
21+
int collectionLen = collection.length;
22+
int comparisons = 0;
23+
24+
for (int i = 0; i < collectionLen; i++) {
25+
for (int j = 0; j < (collectionLen - 1); j++) {
26+
comparisons++;
27+
if (collection[j] < collection[j + 1]) {
28+
int temp = collection[j];
29+
collection[j] = collection[j + 1];
30+
collection[j + 1] = temp;
31+
}
32+
}
33+
}
34+
return comparisons;
35+
}
36+
37+
public int endAfterNoSwapsAscending(int[] collection) {
38+
int collectionLen = collection.length;
39+
boolean isSwapped = true;
40+
int comparisons = 0;
41+
int i = 0;
42+
43+
for (i = 0; (i < collectionLen) && (isSwapped); i++) {
44+
isSwapped = false;
45+
for (int j = 0; j < (collectionLen - 1); j++) {
46+
comparisons++;
47+
if (collection[j] > collection[j + 1]) {
48+
int temp = collection[j];
49+
collection[j] = collection[j + 1];
50+
collection[j + 1] = temp;
51+
isSwapped = true;
52+
}
53+
}
54+
}
55+
return comparisons;
56+
}
57+
58+
public int optimisedAscending(int[] collection) {
59+
int collectionLen = collection.length;
60+
int comparisons = 0;
61+
62+
for (int i = 0; i < collectionLen; i++) {
63+
for (int j = 0; j < (collectionLen - 1 - i); j++) {
64+
comparisons++;
65+
if (collection[j] > collection[j + 1]) {
66+
int temp = collection[j];
67+
collection[j] = collection[j + 1];
68+
collection[j + 1] = temp;
69+
}
70+
}
71+
}
72+
return comparisons;
73+
}
74+
75+
}

BubbleSort/InsertionSort.java

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
public class InsertionSort {
2+
3+
public int ascending(int[] collection) {
4+
int comparisons = 0;
5+
6+
for (int i = 1; i < collection.length; i++) {
7+
int key = collection[i];
8+
int j;
9+
for (j = (i - 1); (j >= 0) && (collection[j] > key); j--) {
10+
comparisons++;
11+
collection[j + 1] = collection[j];
12+
}
13+
collection[j + 1] = key;
14+
}
15+
System.out.println("This was completed with a total of " + comparisons + " iterations!");
16+
return comparisons;
17+
}
18+
19+
public int descending(int[] collection) {
20+
int comparisons = 0;
21+
22+
for (int i = 1; i < collection.length; i++) {
23+
int key = collection[i];
24+
int j;
25+
for (j = (i - 1); (j >= 0) && (collection[j] < key); j--) {
26+
comparisons++;
27+
collection[j + 1] = collection[j];
28+
}
29+
collection[j + 1] = key;
30+
}
31+
System.out.println("This was completed with a total of " + comparisons + " iterations!");
32+
return comparisons;
33+
}
34+
}

BubbleSort/SortingApp.java

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
public class SortingApp {
2+
3+
public static String toString(int collection[]) {
4+
String temp = "[";
5+
for (int i = 0; i < collection.length; i++) {
6+
temp += collection[i];
7+
if (i < (collection.length - 1)) {
8+
temp += ", ";
9+
}
10+
}
11+
temp += "]";
12+
return temp;
13+
}
14+
15+
public static void bubbleSort() {
16+
int collection[];
17+
int iterations;
18+
BubbleSort bubble = new BubbleSort();
19+
20+
collection = new int[] { 2, 1, 7, 8, 4 };
21+
iterations = bubble.basicAscending(collection);
22+
collection = new int[] { 2, 1, 7, 8, 4 };
23+
iterations = bubble.basicDescending(collection);
24+
collection = new int[] { 2, 1, 7, 8, 4 };
25+
iterations = bubble.endAfterNoSwapsAscending(collection);
26+
collection = new int[] { 2, 1, 7, 8, 4 };
27+
iterations = bubble.optimisedAscending(collection);
28+
29+
}
30+
31+
public static void insertionSort() {
32+
int collection[];
33+
int iterations;
34+
InsertionSort insert = new InsertionSort();
35+
36+
collection = new int[] { 2, 1, 7, 8, 4 };
37+
iterations = insert.ascending(collection);
38+
collection = new int[] { 2, 1, 7, 8, 4 };
39+
iterations = insert.descending(collection);
40+
41+
}
42+
43+
public static void main(String[] args) {
44+
bubbleSort();
45+
insertionSort();
46+
}
47+
}

0 commit comments

Comments
 (0)