-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjava-sorting-algorithms-classic.java
More file actions
205 lines (170 loc) · 5.7 KB
/
java-sorting-algorithms-classic.java
File metadata and controls
205 lines (170 loc) · 5.7 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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
import java.util.Arrays;
public class SortingAlgorithms {
// Bubble Sort
// Selection Sort
// Merge Sort
// Insertion Sort
// Quick Sort
// Heap Sort
// 1. Bubble Sort
public static void bubbleSort(int[] array) {
int n = array.length;
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (array[j] > array[j + 1]) {
// Swap array[j] and array[j+1]
int temp = array[j];
array[j] = array[j + 1];
array[j + 1] = temp;
}
}
}
}
// 2. Selection Sort
public static void selectionSort(int[] array) {
int n = array.length;
for (int i = 0; i < n - 1; i++) {
int minIndex = i;
for (int j = i + 1; j < n; j++) {
if (array[j] < array[minIndex]) {
minIndex = j;
}
}
// Swap array[minIndex] and array[i]
int temp = array[minIndex];
array[minIndex] = array[i];
array[i] = temp;
}
}
// 3. Merge Sort
public static void mergeSort(int[] array) {
if (array.length > 1) {
int mid = array.length / 2;
// Split the array into two halves
int[] left = Arrays.copyOfRange(array, 0, mid);
int[] right = Arrays.copyOfRange(array, mid, array.length);
// Recursively sort the two halves
mergeSort(left);
mergeSort(right);
// Merge the sorted halves
merge(array, left, right);
}
}
private static void merge(int[] array, int[] left, int[] right) {
int i = 0, j = 0, k = 0;
// Merge the two arrays
while (i < left.length && j < right.length) {
if (left[i] <= right[j]) {
array[k++] = left[i++];
} else {
array[k++] = right[j++];
}
}
// Copy remaining elements
while (i < left.length) {
array[k++] = left[i++];
}
while (j < right.length) {
array[k++] = right[j++];
}
}
// 4. Insertion Sort
public static void insertionSort(int[] array) {
for (int i = 1; i < array.length; i++) {
int key = array[i];
int j = i - 1;
// Move elements that are greater than key one position ahead
while (j >= 0 && array[j] > key) {
array[j + 1] = array[j];
j--;
}
array[j + 1] = key;
}
}
// 5. Quick Sort
public static void quickSort(int[] array, int low, int high) {
if (low < high) {
int pi = partition(array, low, high);
// Recursively sort elements before and after partition
quickSort(array, low, pi - 1);
quickSort(array, pi + 1, high);
}
}
private static int partition(int[] array, int low, int high) {
int pivot = array[high];
int i = low - 1;
for (int j = low; j < high; j++) {
if (array[j] <= pivot) {
i++;
// Swap array[i] and array[j]
int temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
// Swap array[i+1] and array[high]
int temp = array[i + 1];
array[i + 1] = array[high];
array[high] = temp;
return i + 1;
}
// 6. Heap Sort
public static void heapSort(int[] array) {
int n = array.length;
// Build max heap
for (int i = n / 2 - 1; i >= 0; i--) {
heapify(array, n, i);
}
// Extract elements from heap one by one
for (int i = n - 1; i > 0; i--) {
// Swap array[0] and array[i]
int temp = array[0];
array[0] = array[i];
array[i] = temp;
// Heapify the reduced heap
heapify(array, i, 0);
}
}
private static void heapify(int[] array, int n, int i) {
int largest = i;
int left = 2 * i + 1;
int right = 2 * i + 2;
// Find largest among root, left child, and right child
if (left < n && array[left] > array[largest]) {
largest = left;
}
if (right < n && array[right] > array[largest]) {
largest = right;
}
// Swap and continue heapifying if root is not the largest
if (largest != i) {
int temp = array[i];
array[i] = array[largest];
array[largest] = temp;
heapify(array, 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
bubbleSort(array.clone());
System.out.println("Bubble Sorted: " + Arrays.toString(array));
// Selection Sort
selectionSort(array.clone());
System.out.println("Selection Sorted: " + Arrays.toString(array));
// Merge Sort
mergeSort(array.clone());
System.out.println("Merge Sorted: " + Arrays.toString(array));
// Insertion Sort
insertionSort(array.clone());
System.out.println("Insertion Sorted: " + Arrays.toString(array));
// Quick Sort
int[] quickSortArray = array.clone();
quickSort(quickSortArray, 0, quickSortArray.length - 1);
System.out.println("Quick Sorted: " + Arrays.toString(quickSortArray));
// Heap Sort
heapSort(array.clone());
System.out.println("Heap Sorted: " + Arrays.toString(array));
}
}