Sort the given number using selection sort algorithm in java
Sourcecode:-
import java.util.Scanner;
public class whileloop {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("enter the size of array :");
int n=sc.nextInt();
int A[]=new int[n];
for(int i=0;i<n;i++) {
A[i]=sc.nextInt();
}
for(int i=0;i<n-1;i++) {
int minIndex=i;
for(int j=i;j<n;j++) {
if(A[j]<A[minIndex]) {
minIndex=j;
}
}
int temp=A[i];
A[i]=A[minIndex];
A[minIndex]=temp;
}
for(int sorteditem:A) {
System.out.print(sorteditem+" ");
}
}
}
output:-
enter the size of array :
5
21 34 213 43 56
21 34 43 56 213
Comments
Post a Comment