-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathSelection1.java
More file actions
42 lines (35 loc) · 1.19 KB
/
Copy pathSelection1.java
File metadata and controls
42 lines (35 loc) · 1.19 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
41
import java.util.*;
public class Selection1 {
public static int[] doSelectionSort(int[] arr){
for (int i = 0; i < arr.length - 1; i++)
{
int index = i;
for (int j = i + 1; j < arr.length; j++)
if (arr[j] < arr[index])
index = j;
int smallerNumber = arr[index];
arr[index] = arr[i];
arr[i] = smallerNumber;
}
return arr;
}
public static void main(String a[]){
Scanner in = new Scanner(System.in); //Import java.util.Scanner for it
System.out.println("Enter length");
int size = in.nextInt();
System.out.println("Size of array is"+size);
int[] array = new int[size];
System.out.println("enter few values");
for (int j = 0; j < array.length ; j++) {
int k = in.nextInt();
array[j] = k;
}
in.close();
// int[] arr1 = {10,34,2,56,7,67,88,42};
int[] arr2 = doSelectionSort(array);
for(int i:arr2){
System.out.print(i);
System.out.print(", ");
}
}
}