-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSort.java
More file actions
56 lines (44 loc) · 1.22 KB
/
Sort.java
File metadata and controls
56 lines (44 loc) · 1.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
package sorting;
public class Sort {
public int[] insertionSort(int[] arr){
for(int i=1;i<arr.length;i++){
int temp = arr[i];
int j = i-1;
while(j>=0 && arr[j] > temp){
arr[j+1] = arr[j];
j--;
}
arr[j+1] = temp;
}
return arr;
}
public int[] bubbleSort(int arr[]){
for(int i=0; i<arr.length-1; i++){
for(int j=i+1;j<arr.length;j++){
if(arr[i]>arr[j]){
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
return arr;
}
public int[] selectionSort(int arr[]){
for(int i=0;i<arr.length-1;i++){
int minElement = arr[i];
int minIndex = i;
for(int j=i+1;j<arr.length;j++){
if(arr[j]<minElement){
minElement = arr[j];
minIndex = j;
}
}
if(minIndex !=i){
arr[minIndex] = arr[i];
arr[i] = minElement;
}
}
return arr;
}
}