Quantcast
Channel: Java Tutorials » Java Sorting alogorithms
Viewing all articles
Browse latest Browse all 3

Selection Sort Example Program in Java

$
0
0

package in.malliktalksjava;

/**
* @author malliktalksjava
* @version 1.0
*
* Below class sorts the given array using SelectionSort
* mechanism and prints the output into console.
*/
public class SelectionSortExample {

/**
* @param args
*/
public static void main(String args[]){

int[] arr1 = {12,434,2,23,7,44,66,42};
int[] arr2 = doSelectionSort(arr1);
for(int var:arr2){
System.out.print(var);
System.out.print(“, “);
}
}

/**
* @param arr
* @return sorted array
*/
public static int[] doSelectionSort(int[] arr){

for (int count = 0; count < arr.length – 1; count++)
{
int index = count;
for (int count2 = count + 1; count2 < arr.length; count2++){
if (arr[count2] < arr[index]){
index = count2;
}
}

int smallerNumber = arr[index];
arr[index] = arr[count];
arr[count] = smallerNumber;
}
return arr;
}
}

 

Other Useful Links:

Bubble Sort Example in JAVA

Insertion Sort Example program in JAVA

Quick Sort Example Program in Java

Merge Sort Example in Java


Filed under: JAVA Tagged: How to write a selection sort in JAva, Java examples, java programs, Java selection sort sample program, Java Sorting alogorithms, Java Tutorials, public class, Selection Sort Example in Java

Viewing all articles
Browse latest Browse all 3

Trending Articles