Knowledge Walls
Venkatesan
Hyderabad, Andhra Pradesh, India
Passcode:
Selection Sort in Sorting of Data structures & Algorithm
2103 Views
Working style of Selection sort 
Selection sort is loop over the array till n records and select smallest no and move it to left one by one to.

Thus,
50,12,3,4,6
3,12,50,4,6
3,4,50,12,6
3,4,6,12,50

Otherwise move big numbers to right hand side one by one.
Selection sort example in JAVA
public class SelectionSort {
    public static void display(int numbers[]){
        for (int i=0;i<numbers.length;i++){
               System.out.print(numbers[i]+"\t");
        }
        System.out.println();
    }
    public static void main(String args[]) throws Exception{
           int numbers[] = {48,34,5,12,8};
    
           for (int i=0;i<numbers.length;i++){
               int small_no_index = i;
               for (int j=i+1;j<numbers.length;j++){
                   if (numbers[small_no_index] > numbers[j]){
                       small_no_index = j;
                   }
               }
               
               int tmp = numbers[i];
               numbers[i] = numbers[small_no_index];
               numbers[small_no_index] = tmp;
           }
           display(numbers);
    }
}
Next Topics
Next lessons of current book.
Previous Topics
Previous lessons of current book.
Best Lessons of "Data structures & Algorithm"
Top lessons which are viewed more times.
  Copyright © 2014 Knowledge walls, All rights reserved
KnowledgeWalls
keep your tutorials and learnings with KnowledgeWalls. Don't lose your learnings hereafter. Save and revise it whenever required.
Click here for more details