-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPracticeQuestion.java
More file actions
66 lines (63 loc) · 1.73 KB
/
Copy pathPracticeQuestion.java
File metadata and controls
66 lines (63 loc) · 1.73 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
import java.util.Arrays;
public class PracticeQuestion {
public static void main(String[] args) {
int[] myarr = {5,4,3,2,1};
cyclic(myarr);
System.out.println(Arrays.toString(myarr));
}
static void bubble(int[]arr){
for(int i=0;i<arr.length;i++){
boolean swap = false;
for(int j=0;j< arr.length-i-1;j++){
if(arr[j]>arr[j+1]){
swap(arr,j,j+1);
swap = true;
}
}
if(!swap){
break;
}
}
}
static void selection(int[]arr){
for(int i=0;i< arr.length;i++){
int elemeantIdx = arr.length-1-i;
int max = findMax(arr,0,elemeantIdx);
if(max!= elemeantIdx){
swap(arr,max,elemeantIdx);
}
}
}
static void insertion(int[]arr){
for(int i=0;i< arr.length-1;i++){
for(int j=i+1;j>0;j--){
if(arr[j]<arr[j-1]){
swap(arr,j,j-1);
}
}
}
}
static void cyclic(int[]arr){
int count = 0;
while(count < arr.length){
if(arr[count] != count+1){
swap(arr,count,arr[count]-1);
}
else{count++;}
}
}
static int findMax(int[]arr,int start,int end){
int max = 0;
for(int j = start;j<=end;j++){
if(arr[j]>arr[max]){
max = j;
}
}
return max;
}
static void swap(int[]arr,int first,int second){
int temp = arr[first];
arr[first] = arr[second];
arr[second] = temp;
}
}