-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSelectionSort.java
More file actions
40 lines (40 loc) · 1.1 KB
/
Copy pathSelectionSort.java
File metadata and controls
40 lines (40 loc) · 1.1 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
import java.util.Scanner;
/*
* ---------------
* Time Complexity
* Best, Average, Worst Case = O(n^2)
* ---------------
*/
class SelectionSort{
public static void main(String []args){
int i, size;
try (Scanner scan = new Scanner(System.in)) {
System.out.println("Enter the size of the array:");
size = scan.nextInt();
int arr[] = new int[size];
System.out.println("Enter the elements: ");
for(i=0;i<size;i++){
arr[i] = scan.nextInt();
}
sort(arr,size);
for(i=0;i<size;i++){
System.out.print(arr[i]+" ");
}
}
}
static void sort(int arr[], int n){
int i, j, index, temp;
for(i=0;i<n-1;i++){
index = i;
for(j=i+1;j<n;j++){
/*Key Logic*/
if(arr[j] < arr[index]){
index = j;
}
}
temp = arr[index];
arr[index] = arr[i];
arr[i] = temp;
}
}
}