Skip to content

Commit cbf5c6b

Browse files
authored
Renaming sorting package/directory.
Renaming sorting package/directory to sortingalgorithms. Added MergeSort.
1 parent 76408ce commit cbf5c6b

3 files changed

Lines changed: 171 additions & 0 deletions

File tree

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.tozu.sorting;
2+
3+
public class BubbleSort {
4+
5+
public static void bubbleSort(int[] arr) {
6+
int n = arr.length;
7+
boolean swapped;
8+
9+
// Outer loop for passes
10+
for (int i = 0; i < n - 1; i++) {
11+
swapped = false;
12+
13+
// Inner loop for comparisons
14+
for (int j = 0; j < n - i - 1; j++) {
15+
// Adjacent element comparison
16+
if (arr[j] > arr[j + 1]) {
17+
// Swap, if out of order
18+
int temp = arr[j];
19+
arr[j] = arr[j + 1];
20+
arr[j + 1] = temp;
21+
swapped = true;
22+
}
23+
}
24+
25+
// If no swaps happened the array was already sorted
26+
if (!swapped) break;
27+
}
28+
}
29+
30+
public static void printArray(int[] arr) {
31+
for (int num : arr) {
32+
System.out.print(num + " ");
33+
}
34+
}
35+
36+
public static void main(String[] args) {
37+
int[] arr = {64, 34, 25, 22, 12, 90};
38+
System.out.print("Original array: ");
39+
printArray(arr);
40+
System.out.println();
41+
42+
bubbleSort(arr);
43+
44+
System.out.print("Sorted array: ");
45+
printArray(arr);
46+
}
47+
48+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package com.tozu.sorting;
2+
3+
import java.util.Random;
4+
5+
public class MergeSort {
6+
7+
public static void mergeSort(int[] arr) {
8+
// In this case, the array only contains one element.
9+
// That means, that it is already sorted.
10+
if (arr.length <= 1) {
11+
return;
12+
}
13+
14+
int mid = arr.length / 2;
15+
16+
int[] left = new int[mid];
17+
int[] right = new int[arr.length - mid];
18+
19+
// Data copying
20+
System.arraycopy(arr, 0, left, 0, mid);
21+
System.arraycopy(arr, mid, right, 0, arr.length - mid);
22+
23+
// Recursive sorting
24+
mergeSort(left);
25+
mergeSort(right);
26+
27+
// Merge
28+
merge(arr, left, right);
29+
}
30+
31+
private static void merge(int[] arr, int[] left, int[] right) {
32+
int i = 0, j = 0, k = 0;
33+
34+
while (i < left.length && j < right.length) {
35+
if (left[i] <= right[j]) {
36+
arr[k++] = left[i++];
37+
} else {
38+
arr[k++] = right[j++];
39+
}
40+
}
41+
42+
// Remaining elements
43+
while (i < left.length) {
44+
arr[k++] = left[i++];
45+
}
46+
47+
while (j < right.length) {
48+
arr[k++] = right[j++];
49+
}
50+
}
51+
52+
public static void main(String[] args) {
53+
54+
Random random = new Random();
55+
56+
// Declare an array with X amount of elements
57+
int[] arr = new int[100000000];
58+
59+
// Add random elements to the array
60+
for (int i = 0; i < arr.length; i++) {
61+
arr[i] = random.nextInt(1000);
62+
}
63+
64+
mergeSort(arr);
65+
66+
for (int num : arr) {
67+
System.out.print(num + " ");
68+
}
69+
}
70+
71+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package com.tozu.sorting;
2+
3+
public class SelectionSort {
4+
5+
public static void selectionSort(int[] arr) {
6+
int n = arr.length;
7+
8+
for (int i = 0; i < n - 1; i++) {
9+
int minIndex = i;
10+
11+
for (int j = i + 1; j < n; j++) {
12+
if (arr[j] <= arr[minIndex]) {
13+
minIndex = j;
14+
}
15+
}
16+
17+
int temp = arr[minIndex];
18+
arr[minIndex] = arr[i];
19+
arr[i] = temp;
20+
}
21+
}
22+
23+
public static void printArray(int[] arr) {
24+
for (int value : arr) {
25+
System.out.print(value + " ");
26+
}
27+
System.out.println();
28+
}
29+
30+
public static void main(String[] args) {
31+
32+
int[] data = {33, 64, 25, 12, 22, 11};
33+
int[] data2 = {104, 55, 78, 224, 5, 19, 34, 7};
34+
35+
System.out.println("Original array (1):");
36+
printArray(data);
37+
38+
selectionSort(data);
39+
40+
System.out.println("Sorted array (1):");
41+
printArray(data);
42+
43+
System.out.println("Original array (2):");
44+
printArray(data2);
45+
46+
selectionSort(data2);
47+
48+
System.out.println("Sorted array (2):");
49+
printArray(data2);
50+
}
51+
52+
}

0 commit comments

Comments
 (0)