Sunday 17 January 2016

Java program for selection sort


import java.util.Scanner;
class Main{
public static void main(String args[]){
int i,j,min,loc,temp,number=8;
int list[]=new int[number];
Scanner input=new Scanner(System.in);
for(i=0;i<number;i++){
System.out.println("Enter the numbers ");
list[i]=input.nextInt();
}
min=list[0];
for(i=0;i<=number-1;i++){
min=list[i];
loc=i;
for(j=i+1;j<=number-1;j++){
if(list[j]<min){
min=list[j];
loc=j;
}
}
if(loc!=i){
temp=list[i];
list[i]=list[loc];
list[loc]=temp;
}
}
for(i=0;i<=number-1;i++){
System.out.println("Selection sorted list are "+list[i]);
}
}
}

OUTPUT:-
C:\Users\saty\Desktop>javac 1.java
C:\Users\saty\Desktop>java Main

Enter the numbers
45
Enter the numbers
2
Enter the numbers
12
Enter the numbers
58
Enter the numbers
64
Enter the numbers
98
Enter the numbers
22
Enter the numbers
8
Selection sorted list are 2
Selection sorted list are 8
Selection sorted list are 12
Selection sorted list are 22
Selection sorted list are 45
Selection sorted list are 58
Selection sorted list are 64
Selection sorted list are 98

8 comments:

  1. The best. This guy should teach "Teachers"

    ReplyDelete
  2. just beginned java today and your tutorial explained me the best

    ReplyDelete
  3. God bless you! :) Finally something useful, and also explained for beginners!

    ReplyDelete
  4. Thank you sir, you have done well! keep creating your greatness to assist the world.

    ReplyDelete
  5. Excellent Work. you have done a great job. this is easy to understand and it is very helpful in programming

    ReplyDelete
  6. Thanks a lot for your tutorials! Much appreciated.

    ReplyDelete
  7. Great teaching skills thank you so much! 
    thank you sir...

    ReplyDelete
  8. i am also learning this ... its easy to understand ... thanks for the code ...

    ReplyDelete