Skip to content

Commit 24b255c

Browse files
committed
initial Java files upload
1 parent ebcf80a commit 24b255c

File tree

5 files changed

+180
-0
lines changed

5 files changed

+180
-0
lines changed

bubbleSort.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
public int[] bubbleSort (int[] list) {
2+
int i, j, temp = 0;
3+
for (i = 0; i < list.length - 1; i++) {
4+
for (j = 0; j < list.length - 1 - i; j++) {
5+
if (list[j] > list[j + 1]) {
6+
temp = list[j];
7+
list[j] = list[j + 1];
8+
list[j + 1] = temp;
9+
}
10+
}
11+
}
12+
return list;
13+
}

insertionSort.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// using Array
2+
public int[] insertionSort (int[] list) {
3+
int i, j, key, temp;
4+
for (i = 1; i < list.length; i++) {
5+
key = list[i];
6+
j = i - 1;
7+
while (j >= 0 && key < list[j]) {
8+
temp = list[j];
9+
list[j] = list[j + 1];
10+
list[j + 1] = temp;
11+
j--;
12+
}
13+
}
14+
return list;
15+
}
16+
17+
// using ArrayList
18+
public ArrayList<Integer> insertionSort (ArrayList<Integer> list) {
19+
int i, j, key, temp;
20+
for (i = 1; i < list.size(); i++) {
21+
key = list.get(i);
22+
j = i - 1;
23+
while (j >= 0 && key < list.get(j)) {
24+
temp = list.get(j);
25+
list.set(j, list.get(j + 1));
26+
list.set(j + 1, temp);
27+
j--;
28+
}
29+
}
30+
return list;
31+
}

mergeSort.java

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
public void mergeSort (int[] list, int lowIndex, int highIndex) {
2+
if (lowIndex == highIndex)
3+
return;
4+
else {
5+
int midIndex = (lowIndex + highIndex) / 2;
6+
mergeSort(list, lowIndex, midIndex);
7+
mergeSort(list, midIndex + 1, highIndex);
8+
merge(list, lowIndex, midIndex, highIndex);
9+
}
10+
}
11+
12+
public void merge(int[] list, int lowIndex, int midIndex, int highIndex) {
13+
int[] L = new int[midIndex - lowIndex + 2];
14+
15+
for (int i = lowIndex; i <= midIndex; i++) {
16+
L[i - lowIndex] = list[i];
17+
}
18+
L[midIndex - lowIndex + 1] = Integer.MAX_VALUE;
19+
int[] R = new int[highIndex - midIndex + 1];
20+
21+
for (int i = midIndex + 1; i <= highIndex; i++) {
22+
R[i - midIndex - 1] = list[i];
23+
}
24+
R[highIndex - midIndex] = Integer.MAX_VALUE;
25+
int i = 0, j = 0;
26+
27+
for (int k = lowIndex; k <= highIndex; k++) {
28+
if (L[i] <= R[j]) {
29+
list[k] = L[i];
30+
i++;
31+
}
32+
else {
33+
list[k] = R[j];
34+
j++;
35+
}
36+
}
37+
}
38+


primes.txt

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
// version 1
2+
public class Primes {
3+
4+
public static void main(String[] args) {
5+
int max = 10;
6+
for (int x = 2; x <= max; x++) {
7+
boolean isPrime = true;
8+
for (int y = 2; y < x; y++)
9+
if (x % y == 0)
10+
isPrime = false;
11+
if (isPrime)
12+
System.out.println(x);
13+
}
14+
}
15+
}
16+
--------------------------------------------
17+
// version 2: break
18+
public class Primes {
19+
20+
public static void main(String[] args) {
21+
int max = 10;
22+
for (int x = 2; x <= max; x++) {
23+
boolean isPrime = true;
24+
for (int y = 2; y < x; y++)
25+
if (x % y == 0) {
26+
isPrime = false;
27+
break;
28+
}
29+
if (isPrime)
30+
System.out.println(x);
31+
}
32+
}
33+
}
34+
--------------------------------------------
35+
// version 3: break, add to list
36+
import java.util.ArrayList;
37+
38+
public class Primes {
39+
40+
public static void main(String[] args) {
41+
ArrayList<Integer> primeList = new ArrayList<>();
42+
43+
int max = 10000;
44+
for (int x = 2; x <= max; x++) {
45+
boolean isPrime = true;
46+
for (int y = 2; y < x; y++)
47+
if (x % y == 0) {
48+
isPrime = false;
49+
break;
50+
}
51+
if (isPrime)
52+
primeList.add(x);
53+
}
54+
System.out.println(primeList);
55+
}
56+
}
57+
--------------------------------------------
58+
// version 4: break, add to list, square root
59+
import java.util.ArrayList;
60+
61+
public class Primes {
62+
63+
public static void main(String[] args) {
64+
ArrayList<Integer> primeList = new ArrayList<>();
65+
66+
int max = 200000;
67+
for (int x = 2; x <= max; x++) {
68+
boolean isPrime = true;
69+
for (int y = 2; y < Math.sqrt(x); y++)
70+
if (x % y == 0) {
71+
isPrime = false;
72+
break;
73+
}
74+
if (isPrime)
75+
primeList.add(x);
76+
}
77+
System.out.println(primeList);
78+
}
79+
}

selectionSort.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
public int[] selectionSort (int[] list) {
2+
int i, j, minValue, minIndex, temp = 0;
3+
for (i = 0; i < list.length; i++) {
4+
minValue = list[i];
5+
minIndex = i;
6+
for (j = i; j < list.length; j++) {
7+
if (list[j] < minValue) {
8+
minValue = list[j];
9+
minIndex = j;
10+
}
11+
}
12+
if (minValue < list[i]) {
13+
temp = list[i];
14+
list[i] = list[minIndex];
15+
list[minIndex] = temp;
16+
}
17+
}
18+
return list;
19+
}

0 commit comments

Comments
 (0)