Selection Sort In Java

In this example we are going to sort the values of an array using selection sort.

Tutorials

Selection Sort In Java

Introduction

In this example we are going to sort the values of an array  using selection sort.

In selection sorting algorithm, find the minimum value in the array then swap it first position. In next step leave the first value and find the minimum value within remaining values. Then swap it with the value of minimum index position. Sort the remaining  values by using same steps. Selection sort  is probably the most intuitive sorting algorithm to invent.

The complexity of selection sort algorithm is in worst-case, average-case, and best-case run-time of Θ(n2), assuming that comparisons can be done in constant time.

Code description:

In selection sort algorithm to find the minimum value in the array. First assign minimum index in key (index_of_min=x). Then find the minimum value and assign the index of minimum value in key (index_of_min=y). Then swap the minimum value with the value of minimum index.
At next iteration leave the value of minimum index position and sort the remaining values by following same steps.

Working of the selection sort :

Say we have an array unsorted A[0],A[1],A[2]................ A[n-1] and A[n] as input. Then the following steps are followed by selection sort algorithm to sort the values of an array . (Say we have a key index_of_min that indicate the position of minimum value)
1.Initaily varaible  index_of_min=0;
2.Find the minimum value in the unsorted array.
3.Assign the index of the minimum value into index_of_min variable.
4.Swap minimum value to first position.
5.Sort the remaining values of array (excluding the first value).

The code of the program :

 ``` public class selectionSort{   public static void main(String a[]){   int i;   int array[] = {12,9,4,99,120,1,3,10};   System.out.println("\n\n RoseIndia\n\n");   System.out.println(" Selection Sort\n\n");    System.out.println("Values Before the sort:\n");     for(i = 0; i < array.length; i++)   System.out.print( array[i]+"  ");   System.out.println();   selection_srt(array, array.length);     System.out.print("Values after the sort:\n");     for(i = 0; i

Output of the example:

 `C:\array\sorting>javac selectionSort.java` ```C:\array\sorting>java selectionSort ``` ``` RoseIndia ``` ``` Selection Sort ``` `Values Before the sort:` ```12 9 4 99 120 1 3 10 Values after the sort: 120 99 12 10 9 4 3 1 PAUSE``` `C:\array\sorting>_`

Selection Sort In Java

Posted on: May 28, 2007 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Related Tutorials

Discuss: Selection Sort In Java   View All Comments

Your Name (*) :
Subject (*):

arti nigam
April 18, 2011
selection sort technique

i want the of selection sort in java
Jorge
July 1, 2011

i want insertion sort...!
carolinepablo
August 15, 2011
progrming

Nice and 2 easy 2 understand tnx u!!!
rajat sahay
February 12, 2013
computer

my fovourate subject computer
Samantha
January 13, 2013
Helppp!!!!

the code was very helpful but I am having a problem.I have to use Selection sort for a table with 1000 elements and they are listed from 1-1000 so are the indexes.I can display elements before being listed(even though the are already listed) but it seems that the selection sort isn't working cuz after being listed the console displays 111111111.....this is the code:public class SelectionSort { public static void main(String[] args) { long startTime =System.nanoTime(); int i; int []A =new int[1000]; int Total=0; for( i = 1; i < A.length; i++) {A[0]=0; A[i]=A[0]+1; Total =Total+ A[i]; System.out.print("Vlerat para renditjes "); System.out.print( Total+" "); System.out.println();} for( i = 1; i < A.length; i++) selection_sort(A, A.length); System.out.print("Vlerat pas renditjes :\n"); for(i= 1; i<A.length; i++) System.out.print(A[i]+" "); System.out.println(); long stopTime = System.nanoTime(); long elapsedTime=stopTime-startTime; System.out.println("Koha e ekzekutimit:"+elapsedTime+" nanosekonda"); } public static void selection_sort(int A[], int n){ for (int i=1;i<=A.length-1;i++) {int min=i; for(int j=i+1;j<=A.length;j++){ if(A[j]<A[min]){ min=j; } } int temp = A[i]; A[i] = A[min]; A[min] = temp; } } }
Mr. Ianine
February 24, 2012
Inefficient Program

Scrub, I can do this in one line
ali
November 2, 2012
selection sort

im confused in selection and insertion sort...it seems same to me ....help plz
ArmyHill01
April 26, 2013
Correction

The inequality in your if statement is reversed. Switch it and all is perfect. Thanks for the working algorithm!
Ahmed
June 17, 2013
there is an error

it must be (index_of_max) not (index_of_min)in the code