-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSelection_Sort2.java
More file actions
31 lines (31 loc) · 868 Bytes
/
Selection_Sort2.java
File metadata and controls
31 lines (31 loc) · 868 Bytes
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
import java.util.*;
public class Selection_Sort2{
public static void selectionsort(int arr[]){
int n = arr.length;
//outer loop for number of passes
for(int i=0;i<n-1;i++){
int min=i;
//for
for(int j=i+1;j<n;j++){
if(arr[j]<arr[min]){//for decreasing order array || { arr[j]<arr[min]}
min=j;
}
}
//swapping
int temp=arr[min];
arr[min]=arr[i];
arr[i]=temp;
}
}
public static void printarr(int arr[]){
for(int i=0;i<arr.length;i++){
System.out.print(arr[i] + " ");
}
// System.out.println();
}
public static void main(String args[]){
int arr[]={3,6,2,1,8,7,4,5,3,1};
selectionsort(arr);
printarr(arr);
}
}