-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSelection_sort.java
More file actions
50 lines (40 loc) · 968 Bytes
/
Selection_sort.java
File metadata and controls
50 lines (40 loc) · 968 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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
/**
* @author SAGAR
*
*/
import java.util.Scanner;
public class Selection_sort {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner x = new Scanner(System.in);
System.out.println("Enter the Size of Array");
int size = x.nextInt();
System.out.println("Enter the Array");
int array[] = new int[size];
for (int i = 0; i < size; i++) {
array[i] = x.nextInt();
}
selection_Sort(array, size);
x.close();
}
public static void selection_Sort(int a[], int n) {
int min_index = 0;
for (int i = 0; i < n - 1; i++) {
min_index = i;
for (int j = i + 1; j < n; j++) {
if (a[j] < a[min_index]) {
min_index = j;
}
}
int temp = a[min_index];
a[min_index] = a[i];
a[i] = temp;
}
printArray(a, n);
}
public static void printArray(int a[], int n) {
for (int i = 0; i < n; i++) {
System.out.print(a[i] + " ");
}
}
}