-
Notifications
You must be signed in to change notification settings - Fork 0
/
sorting.java
126 lines (106 loc) · 3.01 KB
/
sorting.java
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
public class sorting {
public static void bubbleSort(int[] args) {
for (int i = 0; i < args.length-1; i++) {
for (int j = 0; j < (args.length-1-i); j++) {
if(args[j]>args[j+1]){
int temp = args[j];
args[j] = args[j+1];
args[j+1] = temp;
}
}
}
}
public static void selectionSort(int[] nums) {
for (int i = 0; i < nums.length; i++) {
int minP = i;
for (int j = i+1; j < nums.length; j++) {
if(nums[j]<nums[minP]){
minP = j;
}
}
int temp = nums[i];
nums[i] = nums[minP];
nums[minP] = temp;
}
}
public static void insertionSort(int[] nums) {
for (int i = 1; i < nums.length; i++) {
int curr = nums[i];
int prv = i-1;
while (prv >= 0 && nums[prv] > curr) {
nums[prv+1] = nums[prv];
prv--;
}
nums[prv+1] = curr;
}
}
public static void mergeSort(int arr[], int si , int ei ){
if (si >= ei) {
return;
}
int mid = si + (ei-si)/2;
mergeSort(arr, si, mid);
mergeSort(arr, mid+1, ei);
merge(arr,si , mid ,ei);
}
public static void merge(int arr[], int si , int mid , int ei){
int temp[] = new int[ei-si + 1];
int i = si;
int j = mid+1;
int k = 0;
while (i <=mid && j <= ei) {
if (arr[i] <= arr[j]) {
temp[k] = arr[i];
i++;
}else{
temp[k] = arr[j];
j++;
}
k++;
}
while (i<=mid) {
temp[k++] = arr[i++];
}
while (j<=ei) {
temp[k++] = arr[j++];
}
for (k = 0 , i=si ; k<temp.length; i++,k++){
arr[i] = temp[k];
}
}
public static void quickSort(int[] arr, int si , int ei){
if (si >= ei){
return;
}
int pidx = partition(arr,si,ei);
quickSort(arr, si, pidx-1);
quickSort(arr, pidx+1, ei);
}
public static int partition(int[] arr , int si , int ei){
int i= si-1;
int pivit = arr[ei];
for(int j = si; j< ei; j++){
if (arr[j] <= pivit) {
i++;
int temp = arr[j];
arr[j] = arr[i];
arr[i] = temp;
}
}
i++;
int temp = pivit;
arr[ei] = arr[i];
arr[i] = temp;
return i;
}
public static void printArr(int[] arr){
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
}
public static void main(String[] args) {
int[] nums = {1,4,2,5,2,6,4,30};
quickSort(nums , 0 , nums.length-1);
printArr(nums);
}
}