Array: Selection Sort

package Array;

 * Created by aarushi on 1/5/21.
public class ArraySortSelction {
    /* Selection sort selects the smallest number in the array and swaps it with the first element
       Step 1: Declare a pointer index which stores the division between sorted and unsorted array.
               Initialize it to 0
       Step 2: Find the smallest element in the array
       Step 3: Swap it with the element at the index pointer

    public static void selectionSort (int [] array){
        int index=0;

        for (int i=0; i<array.length; i++){
            //intialize smallest to index (first element of unsorted array)
            int smallest= index;
            for (int j=i+1; j<array.length; j++){
                //find smallest number
                if (array[j]<array[smallest]){
                    smallest= j;

            //swap the smallest number and the number at the first position of unsorted array
            int temp= array[smallest];
            array[smallest]= array[index];
            array[index]= temp;

    public static void main (String [] args){
        int [] array = {4,1,9,7,5,2};

        for (int i=0; i<array.length; i++){
            System.out.println(array[i]+ " ");


Leave a Reply


Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this:
search previous next tag category expand menu location phone mail time cart zoom edit close