-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjava-sorting-algorithms-stream-api.java
More file actions
169 lines (140 loc) · 4.97 KB
/
java-sorting-algorithms-stream-api.java
File metadata and controls
169 lines (140 loc) · 4.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
import java.util.Arrays;
import java.util.stream.IntStream;
public class SortingAlgorithmsStream {
// Bubble Sort
// Selection Sort
// Merge Sort
// Insertion Sort
// Quick Sort
// Heap Sort
// Bubble Sort using iteration (no direct Stream API equivalent)
public static int[] bubbleSort(int[] array) {
int[] arr = array.clone();
int n = arr.length;
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
// Swap arr[j] and arr[j + 1]
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
return arr;
}
// Selection Sort using iteration (no direct Stream API equivalent)
public static int[] selectionSort(int[] array) {
int[] arr = array.clone();
int n = arr.length;
for (int i = 0; i < n - 1; i++) {
int minIndex = i;
for (int j = i + 1; j < n; j++) {
if (arr[j] < arr[minIndex]) {
minIndex = j;
}
}
// Swap arr[minIndex] and arr[i]
int temp = arr[minIndex];
arr[minIndex] = arr[i];
arr[i] = temp;
}
return arr;
}
// Merge Sort using recursion (no direct Stream API equivalent)
public static int[] mergeSort(int[] array) {
if (array.length <= 1) {
return array;
}
int mid = array.length / 2;
int[] left = Arrays.copyOfRange(array, 0, mid);
int[] right = Arrays.copyOfRange(array, mid, array.length);
return merge(mergeSort(left), mergeSort(right));
}
private static int[] merge(int[] left, int[] right) {
return IntStream.concat(Arrays.stream(left), Arrays.stream(right))
.sorted()
.toArray();
}
// Insertion Sort using iteration (no direct Stream API equivalent)
public static int[] insertionSort(int[] array) {
int[] arr = array.clone();
for (int i = 1; i < arr.length; i++) {
int key = arr[i];
int j = i - 1;
while (j >= 0 && arr[j] > key) {
arr[j + 1] = arr[j];
j--;
}
arr[j + 1] = key;
}
return arr;
}
// Quick Sort using recursion (no direct Stream API equivalent)
public static int[] quickSort(int[] array) {
if (array.length <= 1) {
return array;
}
int pivot = array[array.length / 2];
int[] left = Arrays.stream(array).filter(x -> x < pivot).toArray();
int[] middle = Arrays.stream(array).filter(x -> x == pivot).toArray();
int[] right = Arrays.stream(array).filter(x -> x > pivot).toArray();
return IntStream.concat(
IntStream.concat(Arrays.stream(quickSort(left)), Arrays.stream(middle)),
Arrays.stream(quickSort(right))
)
.toArray();
}
// Heap Sort using iteration (no direct Stream API equivalent)
public static int[] heapSort(int[] array) {
int[] arr = array.clone();
int n = arr.length;
// Build max heap
for (int i = n / 2 - 1; i >= 0; i--) {
heapify(arr, n, i);
}
// Extract elements from heap one by one
for (int i = n - 1; i > 0; i--) {
// Swap arr[0] and arr[i]
int temp = arr[0];
arr[0] = arr[i];
arr[i] = temp;
// Heapify the reduced heap
heapify(arr, i, 0);
}
return arr;
}
private static void heapify(int[] arr, int n, int i) {
int largest = i;
int left = 2 * i + 1;
int right = 2 * i + 2;
if (left < n && arr[left] > arr[largest]) {
largest = left;
}
if (right < n && arr[right] > arr[largest]) {
largest = right;
}
if (largest != i) {
int temp = arr[i];
arr[i] = arr[largest];
arr[largest] = temp;
heapify(arr, n, largest);
}
}
public static void main(String[] args) {
int[] array = {64, 34, 25, 12, 22, 11, 90};
System.out.println("Original Array: " + Arrays.toString(array));
// Bubble Sort
System.out.println("Bubble Sorted: " + Arrays.toString(bubbleSort(array)));
// Selection Sort
System.out.println("Selection Sorted: " + Arrays.toString(selectionSort(array)));
// Merge Sort
System.out.println("Merge Sorted: " + Arrays.toString(mergeSort(array)));
// Insertion Sort
System.out.println("Insertion Sorted: " + Arrays.toString(insertionSort(array)));
// Quick Sort
System.out.println("Quick Sorted: " + Arrays.toString(quickSort(array)));
// Heap Sort
System.out.println("Heap Sorted: " + Arrays.toString(heapSort(array)));
}
}