-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSortingAlgorithms.java
More file actions
209 lines (171 loc) · 5.22 KB
/
Copy pathSortingAlgorithms.java
File metadata and controls
209 lines (171 loc) · 5.22 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
206
207
208
209
import java.util.ArrayList;
import java.util.Arrays;
class SortingSolutions {
// Selection Sort
public int[] selectionSort(int nums[]) {
int n = nums.length, minIdx, temp;
for (int idx = 0; idx < n - 1; idx++) {
minIdx = idx;
for (int subIdx = idx; subIdx < n; subIdx++) {
if (nums[subIdx] < nums[minIdx]) {
minIdx = subIdx;
}
}
if (minIdx == idx)
continue;
temp = nums[minIdx];
nums[minIdx] = nums[idx];
nums[idx] = temp;
}
return nums;
}
public void bubbleSortRecursive(int[] arr, int n) {
if (n == 1)
return;
boolean isSwapped = false;
int temp;
for (int idx = 0; idx < n - 1; idx++) {
if (arr[idx] > arr[idx + 1]) {
temp = arr[idx + 1];
arr[idx + 1] = arr[idx];
arr[idx] = temp;
isSwapped = true;
}
}
if (!isSwapped)
return;
bubbleSortRecursive(arr, n - 1);
}
// Bubble Sort
public int[] bubbleSort(int[] nums) {
// int n = nums.length, temp;
// boolean isSwapped;
// for (int idx = 0; idx <= n - 1; idx++) {
// isSwapped = false;
// for (int subIdx = 0; subIdx < n - 1 - idx; subIdx++) {
// if (nums[subIdx] > nums[subIdx + 1]) {
// temp = nums[subIdx + 1];
// nums[subIdx + 1] = nums[subIdx];
// nums[subIdx] = temp;
// isSwapped = true;
// }
// }
// if (!isSwapped)
// break;
// }
// return nums;
// Using recursion O(n^2) time and O(n) space
bubbleSortRecursive(nums, nums.length);
return nums;
}
public void insertionSortRecursive(int[] arr, int n) {
if (n == 1)
return;
insertionSortRecursive(arr, n - 1);
int lastIdx = n - 1;
int temp;
while (lastIdx > 0) {
if (arr[lastIdx] < arr[lastIdx - 1]) {
temp = arr[lastIdx - 1];
arr[lastIdx - 1] = arr[lastIdx];
arr[lastIdx] = temp;
}
lastIdx--;
}
}
// Insertion Sort
public int[] insertionSort(int[] nums) {
// Linear approach O(n ^ 2) space & O(1) time
// int n = nums.length, temp;
// for (int idx = 0; idx < n; idx++) {
// int subIdx = idx;
// while (subIdx > 0) {
// if (nums[subIdx - 1] > nums[subIdx]) {
// temp = nums[subIdx];
// nums[subIdx] = nums[subIdx - 1];
// nums[subIdx - 1] = temp;
// }
// subIdx--;
// }
// }
// return nums;
// Recursive approach O(n ^ 2) time and O(n) space
insertionSortRecursive(nums, nums.length);
return nums;
}
public void merge(int arr[], int low, int mid, int high) {
int left = low;
int right = mid + 1;
ArrayList<Integer> temp = new ArrayList<Integer>();
while (left <= mid && right <= high) {
if (arr[left] < arr[right]) {
temp.add(arr[left]);
left++;
} else {
temp.add(arr[right]);
right++;
}
}
while (left <= mid) {
temp.add(arr[left]);
left++;
}
while (right <= high) {
temp.add(arr[right]);
right++;
}
for (int idx = low; idx <= high; idx++) {
arr[idx] = temp.get(idx - low);
}
}
public void divide(int arr[], int low, int high) {
if (low >= high)
return;
int mid = (low + high) / 2;
divide(arr, low, mid);
divide(arr, mid + 1, high);
merge(arr, low, mid, high);
}
public int[] mergeSort(int nums[]) {
divide(nums, 0, nums.length - 1);
return nums;
}
public int partition(int[] arr, int low, int high) {
int left = low;
int right = high;
int pivot = arr[low];
int temp;
while (left < right) {
while (arr[left] <= pivot && left <= high - 1)
left++;
while (arr[right] > pivot && right >= low - 1)
right--;
if (left < right) {
temp = arr[right];
arr[right] = arr[left];
arr[left] = temp;
}
}
temp = arr[right];
arr[right] = arr[low];
arr[low] = temp;
return right;
}
public void divideAndConquer(int[] arr, int low, int high) {
if (low < high) {
int partitionIdx = partition(arr, low, high);
divideAndConquer(arr, low, partitionIdx - 1);
divideAndConquer(arr, partitionIdx + 1, high);
}
}
public int[] quickSort(int[] nums) {
divideAndConquer(nums, 0, nums.length - 1);
return nums;
}
}
public class SortingAlgorithms {
public static void main(String[] args) {
SortingSolutions solution = new SortingSolutions();
System.out.println(Arrays.toString(solution.insertionSort(new int[] { 7, 4, 1, 5, 3 })));
}
}