Scenario
Selection sort is best understood by imagining that you have two lists, Aand B. Initially, we have list A, containing all the unsorted elements, and list B is empty. The idea is to use B to store the sorted elements. The algorithm would work by finding the smallest element from A and moving it to the end of B. We keep on doing this until A is empty and B is full. Instead of using two separate lists, we can use the same input array, keeping a pointer to divide the array in two.
In real life, this can be explained by picturing how you would sort a deck of cards. Using a shuffled deck, you can go through the cards one by one until you find the lowest card. You set this aside as a new, second pile. You then look for the next-lowest card and once found, you put it at the bottom of the second pile. You repeat this until the first pile is empty.
One way to arrive at the solution is to first write the pseudocode that uses two arrays (A and B, in the preceding description). Then, adopt the pseudocode to store the sorted list (array B) in the same input array by using the swap method.
Aim
Implement selection sort in Java.
Prerequisites
Steps for Completion
public class SelectionSort {
// Write your code here
public static void main(String[] args) {
/*
* This main method is a stub.
* It does nothing.
* Feel free to write your own code to test your implementation.
* In this case, we have nothing actionable in here, just this comment block, so the JVM should rapidly lose interest and move on to the rest of your code.
*/
}
}
Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks
// SelectionSort.java
public class SelectionSort {
// method to sort an array using selection sort
static void sort(int[] array) {
// index is the variable used as a pointer to the index of end of
// unsorted array. initialized to 0
int index = 0;
// loops until index is equal to the array length. i.e the whole array
// is sorted
while (index < array.length) {
// finding the index of smallest element in unsorted array
// initially assuming index as the index of smallest element
int index_min = index;
// looping through the unsorted part of array
for (int i = index + 1; i < array.length; i++) {
// if current element is smaller than element at index_min,
// updating index_min
if (array[i] < array[index_min]) {
index_min = i;
}
}
// now we simply swap elements at index_min and index
int temp = array[index_min];
array[index_min] = array[index];
array[index] = temp;
// updating index
index++;
// now elements from 0 to index-1 are sorted. this will continue
// until whole array is sorted
}
}
public static void main(String[] args) {
//creating an array
int array[] = { 1, -3, 4, 7, 3, 0, -2, 15, 14, 1, 8 };
//printing the contents
System.out.print("Array before sorting: ");
for (int i = 0; i < array.length; i++) {
System.out.print(array[i] + " ");
}
System.out.println();
//sorting the array
sort(array);
//printing the contents
System.out.print("Array after sorting: ");
for (int i = 0; i < array.length; i++) {
System.out.print(array[i] + " ");
}
System.out.println();
}
}
/*OUTPUT*/
Array before sorting: 1 -3 4 7 3 0 -2 15 14 1 8
Array after sorting: -3 -2 0 1 1 3 4 7 8 14 15
Scenario Selection sort is best understood by imagining that you have two lists, Aand B. Initially,...