Java example- selection sort algorithm


Selection sort algorithm

This is the Java example code for a selection sort algorithm. If you are not sure about the process of the selection sort, i recommend you to visit this tutorial:

Algorithm and Data Structure  

public class SelectionSort {

public static void main(String[] args){
//unsorted array
int[] arr={23,2,4,56,80};
//sorted array using selection sort
selectsort(arr,arr.length);
//display the content of sorted array
int i;
for(i=0;i<arr.length;i++) System.out.println(arr[i]);

}

public static void selectsort(int[] dataset, int n){

int i,j;
for(i=0;i<n;i++){
  int min=i;
  for(j=i+1;j<n;j++)
      if(dataset[j]<dataset[min]) min=j; //find min value
  //then swap it with the beginning item of the unsorted list
  int temp=dataset[min];
  dataset[min]=dataset[i];
  dataset[i]=temp;
        
}

 
}

}




Comments

romam comment

 romam

can u solve this question?
....question is:1) The Car class has two attributes, which correspond to the description and the customer's name. The description and the customer's name are each represented as a string of text. The description is initialised in the constructor, by being assigned the value of the constructor's only parameter. The customer's name is assigned an empty string ("").

Each attribute has a corresponding accessor method and there is another method to set the customer's name to a new name. A display method will output (suitably annotated) the description and, if the customer's name is not an empty string, the customer's name too.


2016-12-16
Doni comment

 Doni

Great place for newbie


2016-07-03
yogesh comment

 yogesh

Nice Example


2015-11-02
ROhit khatri comment

 ROhit khatri

very nice example for beginers


2015-09-30
java2novice comment

 java2novice

very nice. for more java examples visit http://java2novice.com site


2014-04-09



This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.