-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSelectionSort_smallApproach.java
More file actions
43 lines (37 loc) · 1.16 KB
/
SelectionSort_smallApproach.java
File metadata and controls
43 lines (37 loc) · 1.16 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
42
43
import java.io.*;
public class SelectionSort_smallApproach {
public static void main(String args[]) throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter the size of array");
int s=Integer.parseInt(br.readLine());
int arr[]=new int[s];
System.out.print("Enter values of array");
String abc=br.readLine();
String[] src=abc.trim().split("\\s+");
for(int i=0;i<s;i++)
{
arr[i]=Integer.parseInt(src[i]);
}
//This approach uses small element approach as defined in major textbooks
for(int j=0;j<arr.length-1;j++)
{
int smallest=j;
for(int k=j+1;k<arr.length;k++)
{
if(arr[k]<arr[smallest])
{
smallest=k;
}
}
int temp=arr[j];
arr[j]=arr[smallest];
arr[smallest]=temp;
}
System.out.println("Sorted array is "+" ");
for(int x=0;x<arr.length;x++)
{
System.out.print(arr[x]+" ");
}
}
}