-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSortingII.java
More file actions
134 lines (97 loc) · 3.7 KB
/
Copy pathSortingII.java
File metadata and controls
134 lines (97 loc) · 3.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
public class SortingII{
// Merge Sort Algorithm - Divide and Conquer:
// Time Complexity: O(n log n) in all cases (worst, average, best)
// Space Complexity: O(n) due to the temporary array used for merging
public static void mergeSort(int[] arr,int low,int high){
if(low >= high) return;
int mid = low + (high - low)/2; // written as low + (high - low)/2 to avoid overflow
mergeSort(arr,low,mid);
mergeSort(arr,mid+1,high);
merge(arr,low,mid,high);
}
public static void merge(int[] arr,int low,int mid, int high ){
int[] temp = new int[high - low + 1]; //Why high - low + 1 ? because we are merging two subarrays from low to high inclusive
int left = low; // pointer for left subarray
int right = mid+1; // pointer for right subarray
int i = 0;
while(left <= mid && right <= high){ // comparing and merging two subarrays
if(arr[left] <= arr[right]){ // can be written as <= to make it stable
temp[i] = arr[left];
i++;
left++;
} else{
temp[i] = arr[right];
i++;
right++;
}
}
while(left <= mid){ // copying remaining elements from left subarray if any
temp[i] = arr[left]; // can be wriiten as temp[i++] = arr[left++];
i++;
left++;
}
while(right <= high){ // copying remaining elements from right subarray if any
temp[i] = arr[right]; // can be wriiten as temp[i++] = arr[right++];
i++;
right++;
}
for (int k = 0; k < temp.length; k++) { // copying back the sorted elements to original array
arr[low + k] = temp[k];
}
}
// Recursive Bubble Sort:
public static void recursiveBubbleSort(int[] arr,int size){
if(size == 1) return; // base case
for(int i = 0;i < size - 1;i++){ // one pass of bubble sort
if(arr[i] > arr[i + 1]){
swap(arr,i,i+1); // swap if elements are out of order
}
}
recursiveBubbleSort(arr,size - 1); // recursive call for the remaining array
// size - 1 because last element is already sorted
}
public static void swap(int[] arr,int i,int j){
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
// public static void recursiveInsertionSort(int[] arr){
// }
public static void quickSort(int[] arr,int low, int high){
if(low < high){
int partitionIndex = partition(arr,low,high);
quickSort(arr,low,partitionIndex-1);
quickSort(arr,partitionIndex+1,high);
}
}
public static int partition(int[] arr,int low,int high){
int pivot = arr[low]; // first element as pivot
int i = low;
int j = high;
while(i < j){
while(pivot >= arr[i] && i < high){
i++;
}
while(pivot < arr[j] && j > low){
j--;
}
if(i < j){
swap(arr, i, j);
}
}
swap(arr, low, j);
return j;
}
public static void main(String[] args) {
int[] arr = new int[]{4,3,5,2,1};
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
//mergeSort(arr,0,arr.length-1);
quickSort(arr,0,arr.length-1);
System.out.println("");
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
}
}