-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSelectionSort.java
More file actions
56 lines (40 loc) · 1.39 KB
/
SelectionSort.java
File metadata and controls
56 lines (40 loc) · 1.39 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
import java.util.Scanner;
public class SelectionSort {
public static void main(String[] args){
//SORTING THE ARRAY USING SELECTION SORT.
//SELECTS THE MINIMUM/SMALLEST ELEMENT FROM THE ARRAY AND SWAPS IT WITH THE FIRST UNSORTED ARRAY ELEMENT.
Scanner scanner = new Scanner(System.in);
int[] arr;
int[] sortedArr;
int size;
System.out.print("Enter the size of array : ");
size=scanner.nextInt();
arr=new int[size];
sortedArr=new int[size];
System.out.print("\nEnter the array elements : \n");
for(int i=0;i<size;i++){
System.out.printf("Enter element %d : ",i);
arr[i]=scanner.nextInt();
}
System.out.print("\nUnsorted Array is : ");
for(int i=0;i<size;i++){
System.out.print(arr[i] + " ");
}
for(int i=0;i<=size-2;i++){
for(int j=i;j<=size-1;j++){
int min=i;
if(arr[j]<arr[min]){
min=j;
int temp=arr[min];
arr[min]=arr[i];
arr[i]=temp;
}
}
}
System.out.print("\nSorted Array is : ");
for(int i=0;i<size;i++){
System.out.print(arr[i] + " ");
}
scanner.close();
}
}