-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsortMethods4.java
More file actions
177 lines (159 loc) · 5.3 KB
/
Copy pathsortMethods4.java
File metadata and controls
177 lines (159 loc) · 5.3 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
import java.util.ArrayList;
import java.util.Arrays;
public class sortMethods4 {
public static void selectionSort(int[] arr) {
// selection sort - T:O(N**2) S:O(1)
// select the minimum and set it at the start
for (int i = 0; i < arr.length; i++) {
int min = Integer.MAX_VALUE;
int idx = 0;
for (int j = i; j < arr.length; j++) {
if (arr[j] < min) {
min = arr[j];
idx = j;
}
}
int temp = arr[i];
arr[i] = arr[idx];
arr[idx] = temp;
}
System.out.println(Arrays.toString(arr));
}
public static void bubbleSort(int[] arr) {
// Bubble Sort - T:O(N**2) S:O(1)
boolean swapped;
for (int i = 0; i < arr.length - 1; i++) {
swapped = false;
for (int j = 0; j < arr.length - 1 - i; j++) {
if (arr[j] > arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
swapped = true;
}
}
if (!swapped)
break;
}
System.out.println(Arrays.toString(arr));
}
public static void insertionSort(int[] arr) {
// Insertion Sort - T:O(N**2) S:O(1)
for (int i = 0; i < arr.length; i++) {
int key = arr[i];
int j = i - 1;
while (j >= 0 && arr[j] > key) {
arr[j + 1] = arr[j--];
}
arr[j + 1] = key;
}
System.out.println(Arrays.toString(arr));
}
public class InnersortMethods {
public static void mergeSort(int[] arr, int l, int h) {
if (l >= h) {
return;
}
int mid = (h + l) / 2;
mergeSort(arr, l, mid);
mergeSort(arr, mid + 1, h);
merge(arr, l, mid, h);
}
private static void merge(int[] arr, int l, int m, int h) {
ArrayList<Integer> alist = new ArrayList<>();
int left = l, right = m + 1;
while (left <= m && right <= h) {
if (arr[left] < arr[right]) {
alist.add(arr[left++]);
} else {
alist.add(arr[right++]);
}
}
while (left <= m) {
alist.add(arr[left++]);
}
while (right <= h) {
alist.add(arr[right++]);
}
for (int i = l; i <= h; i++) {
arr[i] = alist.get(i - l);
}
}
}
public static void recursiveBubble(int[] arr, int n) {
// Best case T: O(N), Worst case: O(N**2) S: O(N)
// Remeber it has limit till the stack of the OS or env.
// Base case: range == 1.
if (n == 1)
return;
int didSwap = 0;
for (int j = 0; j <= n - 2; j++) {
if (arr[j] > arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
didSwap = 1;
}
}
// if no swapping happens means array already sorted so stops further recursive
// calls
if (didSwap == 0)
return;
// Range reduced after recursion:
recursiveBubble(arr, n - 1);
}
public static void recursiveInsertion(int[] arr, int n) {
// Best case T: O(N) S:O(1)
if (n <= 0) {
return;
}
recursiveInsertion(arr, n - 1);
int j = n;
while (j > 0 && arr[j] < arr[j - 1]) {
int temp = arr[j];
arr[j] = arr[j - 1];
arr[j - 1] = temp;
j--;
}
}
public class QuickSort {
public static void quickSort(int[] arr, int low, int high) {
if (low < high) {
// Partition the array
int pivotIndex = partition(arr, low, high);
// Recursively sort left and right subarrays
quickSort(arr, low, pivotIndex - 1);
quickSort(arr, pivotIndex + 1, high);
}
}
private static int partition(int[] arr, int low, int high) {
int pivot = arr[high]; // Choose last element as pivot
int i = low - 1; // Index of smaller element
for (int j = low; j < high; j++) {
if (arr[j] < pivot) {
i++;
// Swap arr[i] and arr[j]
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
// Swap pivot (arr[high]) to its correct position
int temp = arr[i + 1];
arr[i + 1] = arr[high];
arr[high] = temp;
return i + 1; // Return pivot index
}
}
public static void main(String[] args) {
int[] arr = { 7, 5, 9, 2, 8 };
// selectionSort(arr);
// bubbleSort(arr);
// insertionSort(arr);
InnersortMethods.mergeSort(arr, 0, arr.length - 1);
// recursiveBubble(arr, arr.length);
// recursiveInsertion(arr, arr.length - 1);
// QuickSort.quickSort(arr, 0, arr.length - 1);
System.out.println(Arrays.toString(arr));
}
}