-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSelectionSort.java
55 lines (41 loc) · 1.18 KB
/
SelectionSort.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
49
50
51
52
53
54
55
import java.util.*;
class SelectionSort
{
public static void SortNo(int arr[]) //function declaration
{
System.out.println("Sorted Elements are");
for(int i=0 ; i<arr.length ; i++)
{ //output of sorted array
System.out.print(arr[i] + " ");
}
System.out.println();
}
public static void main(String[]args)
{
Scanner in = new Scanner(System.in);
System.out.println("Enter the size of array..");
int size=in.nextInt();
System.out.println("Enter numbers into array..");
int arr[]=new int[size]; // array declaration
for(int i=0;i<arr.length;i++)
{
arr[i]=in.nextInt(); //array input
}
//Selection Sort . . . . Time Complexity = O(n^2)
for(int i=0;i<arr.length;i++) //outer loop
{
int smallest = a[i];
for(int j=i+1;j<arr.length;j++) //inner loop
{
if(arr[smallest]>arr[j])
{
smallest=j;
}
}
int temp = arr[smallest]; //number swapping . . .
arr[smallest]=arr[i];
arr[i]=temp;
}
SortNo(arr); //function calling . .
}
}