-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathSelectionSort.java
47 lines (39 loc) · 1.1 KB
/
SelectionSort.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
import java.util.Scanner;
public class SelectionSort
{
void selectionSort(int arr[])
{
int n = arr.length;
// Move the boundary of unsorted subarray one by one
for (int i = 0; i < n-1; i++)
{
// Find the minimum element in unsorted array
int min = i;
for (int j = i+1; j < n; j++)
if (arr[j] < arr[min])
min = j;
// Swap the found minimum element with the first
// element
int temp = arr[min];
arr[min] = arr[i];
arr[i] = temp;
}
}
public static void main(String args[])
{
SelectionSort ob = new SelectionSort();
Scanner sc= new Scanner(System.in);
System.out.println("Enter number of elements to be sorted: ");
int num=sc.nextInt();
int array[] = new int[num];
System.out.println("Enter array elements");
for(int i=0; i< num; i++){
array[i] = sc.nextInt();
}
ob.selectionSort(array);
System.out.println("Array after selection sort..");
for(int i=0;i<num;i++){
System.out.print(array[i] + " ");
}
}
}