-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
Selection_Sort.java
48 lines (40 loc) · 1.07 KB
/
Selection_Sort.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
import java.util.Scanner;
class Selection_Sort
{
// function for selection sort
public static void SelectionSort(int[] array, int size)
{
int min_index, temp;
for(int i = 0; i < size - 1; i++)
{
min_index = i;
for(int j = i + 1; j < size; j++)
if(array[j] < array[min_index])
min_index = j;
temp = array[i];
array[i] = array[min_index];
array[min_index] = temp;
}
}
// function ro print array
public static void Print_Array(int[] array, int size)
{
for(int i = 0; i < size; i++)
System.out.print(array[i] + " ");
System.out.println();
}
public static void main(String[] args)
{
Scanner s = new Scanner(System.in);
int num;
num = s.nextInt();
int array[] = new int[num];
for(int i = 0; i < num; i++) {
array[i] = s.nextInt();
}
SelectionSort(array, num);
Print_Array(array, num);
}
}
// Output
// 1 2 3 4 4 6 8