Skip to content

Commit c7caca7

Browse files
authored
Create SelectionSort10
This program is written in java language. It inputs 10 integers from user ,performs selection sort on the integers and prints in ascending order. The program has been executed and it works fine. Please review and approve my PR for HACKTOBERFEST2021 with label hacktoberfest-accepted. Thank you :)
1 parent e8196c0 commit c7caca7

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

SelectionSort10

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import java.io.*;
2+
3+
public class SelectionSort10 {
4+
public static void selsort(int A[]) {
5+
int i, j, small, tmp, pos;
6+
for (i = 0; i < 10; i++) {
7+
small = A[i];
8+
pos = i;
9+
for (j = i + 1; j < 10; j++) {
10+
if (A[j] < small) {
11+
small = A[j];
12+
pos = j;
13+
}
14+
}
15+
tmp = A[i];
16+
A[i] = A[pos];
17+
A[pos] = tmp;
18+
}
19+
System.out.println("Array in ascending order=");
20+
for (i = 0; i < 10; i++)
21+
System.out.println(A[i]);
22+
}
23+
24+
public static void main(String[] args) {
25+
int A[] = new int[10];
26+
BufferedReader buf = new BufferedReader(new InputStreamReader(System.in));
27+
String inStr = null;
28+
System.out.println("Enter 10 elements of array=");
29+
try {
30+
for (int i = 0; i < 10; i++) {
31+
inStr = buf.readLine();
32+
A[i] = Integer.parseInt(inStr);
33+
}
34+
} catch (Exception e) {
35+
System.out.println("Error in data entry");
36+
System.out.println("Exception=" + e);
37+
return;
38+
}
39+
selsort(A);
40+
}
41+
}

0 commit comments

Comments
 (0)