forked from HarshCasper/NeoAlgo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SelectionSort.java
30 lines (28 loc) · 922 Bytes
/
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
import java.util.*;
class SelectionSort
{
public static void main(String[] args) {
int indexI,arrayLength,indexJ,temp=0,minElementIndex=0;
Scanner sc=new Scanner(System.in);
System.out.println("Enter size of array");
arrayLength=sc.nextInt();
int array[]=new int[arrayLength];
System.out.println("Enter Elements of array");
for(indexI=0;indexI<arrayLength;indexI++)
array[indexI]=sc.nextInt();
for(indexI=0;indexI<arrayLength;indexI++)
{
minElementIndex=indexI;
for(indexJ=indexI+1;indexJ<=arrayLength-1;indexJ++)
{
if(array[minElementIndex]>array[indexJ])
minElementIndex=indexJ;
}
temp=array[indexI];
array[indexI]=array[minElementIndex];
array[minElementIndex]=temp;
}
for(indexI=0;indexI<arrayLength;indexI++)
System.out.println(array[indexI]);
}
}