Selection Sort Program Concept with Java code.
Selection Sort Program Concept- What is Selection Sort in Java?
The Selection Sort is a combination of searching and sorting. It
sorts an array by repeatedly finding the minimum element from
the unsorted part and putting it at the beginning. In every iteration of selection sort, the minimum element from the unsorted subarray is picked and moved to the sorted subarray.
Let us understand it more clearly through a Java program.
import java.util.*;
class Select{ //class name
public static void main(String args[]){ //method main
int i,j,temp,cap;
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int array[]=new int[n];
System.out.println("Enter Values");
for(i=0;i<n;i++){
array[i]=sc.nextInt();
}
System.out.println("Arranged values are");
for(i=0;i<n;i++){
cap=i;
for(j=i+1;j<n;j++){
if(array[j]<array[cap]){
cap=j;
}
}
temp=array[i];
array[i]=array[cap];
array[cap]=temp;
}
for(i=0;i<n;i++){
System.out.println(array[i]);
}
}
}
Hope you have understood the Selection Sort now.
For more Program codes and videos visit my Youtube channel :
https://youtube.com/channel/UC27TQ7zJKapHF78bwvi5iVw
For more Posts based on Programming visit my Blog:http://studytable10.blogspot.com/
Phele mereko kuch samahj nhi aata tha pr ye video dekh ne ke baad sb kuch samahj aagya hai iss tarah aur v video banayi ga
ReplyDelete