-
Notifications
You must be signed in to change notification settings - Fork 0
/
arr10.java
48 lines (35 loc) · 841 Bytes
/
arr10.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
//SELECTION SORT(SORTING ALGORITHM)
package Array;
public class arr10 {
// programme for swapping element in array
public static void swapArr(int arr[] , int i , int j) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
//SELECTION SORT
public static void selectionSort(int arr[]) {
int n = arr.length;
for(int i=0 ; i< n-1; i++) {
int min_idx = i;
for(int j=i+1; j< n; j++) {
if(arr[j] < arr[min_idx]) {
min_idx = j;
}
}
swapArr(arr, i, min_idx);
}
}
//function for print Arr
public static void printArr(int arr[]) {
System.out.println("your required array =>");
for(int i=0; i< arr.length; i++) {
System.out.print(arr[i] +" ");
}
}
public static void main(String[] args) {
int array[] = {4,7,1,2,5,3,6,8};
selectionSort(array);
printArr(array);
}
}