Selection Sort Algorithm Java | Beginner’s Algorithm

Selection sort algorithm is also one of the easiest algorithm.

I have a taken an array as arr[ ] = {5, 3, 2, 1, 6} as an example. The result will be arr[]={1,2,3,5,6}.

Logic – Selection Sort Algorithm selects the smallest element in the array and it will swap it with first element of the array, this will be repeated for unsorted portion of the array.

Time Complexity – O(n2)

Space Complexity – 1

Algorithm with Separate Method for Selection Sort

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
public class Main {
public static void main(String[] args) {
int arr[] = {5, 3, 2, 1, 6};
//print all elements of array
for (int i : selectionSort(arr)) {
System.out.println(i);
}
}
public static int[] selectionSort(int[] arr) {
int n = arr.length;
for (int i = 0; i < n - 1; i++) {
int indx=i;
for (int j = i + 1; j < n; j++) {
if(arr[j]<arr[indx])
indx=j;
}
// swap
int temp = arr[indx];
arr[indx] = arr[i];
arr[i] = temp;
}
return arr;
}
}
public class Main { public static void main(String[] args) { int arr[] = {5, 3, 2, 1, 6}; //print all elements of array for (int i : selectionSort(arr)) { System.out.println(i); } } public static int[] selectionSort(int[] arr) { int n = arr.length; for (int i = 0; i < n - 1; i++) { int indx=i; for (int j = i + 1; j < n; j++) { if(arr[j]<arr[indx]) indx=j; } // swap int temp = arr[indx]; arr[indx] = arr[i]; arr[i] = temp; } return arr; } }
public class Main {
    public static void main(String[] args) {
        int arr[] = {5, 3, 2, 1, 6};

        //print all elements of array
        for (int i : selectionSort(arr)) {
            System.out.println(i);
        }
    }

    public static int[] selectionSort(int[] arr) {
        int n = arr.length;
        for (int i = 0; i < n - 1; i++) {
            int indx=i;
            for (int j = i + 1; j < n; j++) {
                if(arr[j]<arr[indx])
                    indx=j;
            }
            // swap
            int temp = arr[indx];
            arr[indx] = arr[i];
            arr[i] = temp;
        }
        return arr;
    }
}

Algorithm inside main method

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
public class Main {
public static void main(String[] args) {
int arr[]={5, 3, 2, 1, 6};
int n = arr.length;
for (int i = 0; i < n - 1; i++) {
int indx=i;
for (int j = i + 1; j < n; j++) {
if(arr[j]<arr[indx])
indx=j;
}
// swap
int temp = arr[indx];
arr[indx] = arr[i];
arr[i] = temp;
}
//print all elements of array
for (int i : arr) {
System.out.println(i);
}
}
}
public class Main { public static void main(String[] args) { int arr[]={5, 3, 2, 1, 6}; int n = arr.length; for (int i = 0; i < n - 1; i++) { int indx=i; for (int j = i + 1; j < n; j++) { if(arr[j]<arr[indx]) indx=j; } // swap int temp = arr[indx]; arr[indx] = arr[i]; arr[i] = temp; } //print all elements of array for (int i : arr) { System.out.println(i); } } }
public class Main {
    public static void main(String[] args) {
        int arr[]={5, 3, 2, 1, 6};
        int n = arr.length;
        for (int i = 0; i < n - 1; i++) {
            int indx=i;
            for (int j = i + 1; j < n; j++) {
                if(arr[j]<arr[indx])
                    indx=j;
            }
            // swap
            int temp = arr[indx];
            arr[indx] = arr[i];
            arr[i] = temp;
        }

        //print all elements of array
        for (int i : arr) {
            System.out.println(i);
        }
    }
}

Leave a Reply

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

WhatsApp Icon Join For Job Alerts