Home | Ajax | BioInformatics | Dojo | EAI | EJB | Hibernate | J2ME | Java | Java Glossary | Java Servlets | JavaScript | Jboss | JDBC | JDO | Jmeter | JSF | JSP | JUnit | Maven | MySQL | Spring Framework | SQL | Struts | Technology | WAP | Web Services | XML


 
  
 
Programming Tutorials: Ajax | Articles | JSP | Bioinformatics | Database | Free Books | Hibernate | J2EE | J2ME | Java | JavaScript | JDBC | JMS | Linux | MS Technology | PHP | RMI | Web-Services | Servlets | Struts | UML
 

 
Facing Programming Problem?
Ask Questions?, Browse Latest Questions, Question-Answer Guidelines
Java
  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification
  Java Applet
Questions
Comments

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.printarray[i]+"  ");
    System.out.println();
    selection_srt(array, array.length);        
    System.out.print("Values after the sort:\n");    
    for(i = 0; i <array.length; i++)
      System.out.print(array[i]+"  ");
    System.out.println();
    System.out.println("PAUSE");
  }

  public static void selection_srt(int array[]int n){
    for(int x=0; x<n; x++){
      int index_of_min = x;
      for(int y=x; y<n; y++){
        if(array[index_of_min]<array[y]){
          index_of_min = y;
        }
      }
      int temp = array[x];
      array[x= array[index_of_min];
      array[index_of_min= temp;
    }
  }
}

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>_

Download this example.

                         

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Latest Searches:
factory
JTable Renderer to be
Time
Photoshop Drawing 3D P
Web Site promotion ser
Math.random()
create csv file
Create PDF file exampl
layout
how to select an drawn
yield function in thre
java merge two xml doc
Javascript DHTML Javas
selecting option value
php socket
log4j
primitive data types
remove rectangle awt
pagination taglib
static
Java Pass Value
array element
Date Examples java
ttcn
selectonelistbox examp
techiques to use subst
string arrays jsp pro
Convert IST time to no
java.nio
jsp to microsoft sql
optgroup
where we place ...-in
collection classes
a sample example forh
rmi
parseInt
spring application
Sample paging program
Flex TreeGrid
Tiles definition richf
download package com.s
button launches dialog
sleep
getRequestDispatcher s
how to override...ng i
ibatis fundamental
Hibernate Mapping
jQuery To Slide Effect
Nested WHILE LOOPS
pos
source code for change
)How
textfield password
Delete database
java random numbers
checkbox struts 1
FILEUPLOADEXAMPLEWITHD
day number given date
update all columns of
create xml using java
J2EE interview questio
PHP User Authenticatio
jdbc connections
executequery
Combattons la programm
remove rectangle
string variable
Merge Sort In Java
Flash Math Physics Fla
Photoshop Effects Crea
Photoshop Basics Kodak
lazy load in hibernate
increase size of strut
logout code in jsp
calculator code usind
java strings
what is static inner c
using set in java
EncoderTest
Copy One Database Tabl
Java String toLowerCase Example
Java String toCharArray Example
Java String substring Example
Java String indexOf Example
Java String startsWith Example
Java String hashCode Example
Java String matches Example
Java String length Example
Java String lastIndexOf Example
Java String isEmpty Example
Java String equalsIgnoreCase Example
Java String equals Example
Java String endsWith Example
Java String copyValueOf Example
Java String contentEquals Example
  EAI Articles
  Java Certification
Tell A Friend
Your Friend Name
Search Tutorials

 

 
 
Browse all Java Tutorials
Java JSP Struts Servlets Hibernate XML
Ajax JDBC EJB MySQL JavaScript JSF
Maven2 Tutorial JEE5 Tutorial Java Threading Tutorial Photoshop Tutorials Linux Technology
Technology Revolutions Eclipse Spring Tutorial Bioinformatics Tutorials Tools SQL
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.