Question

Scenario Selection sort is best understood by imagining that you have two lists, Aand B. Initially,...

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

  • The sort() method should accept an integer array and sort it.

Steps for Completion

  1. Split the input array in two by using an array index pointer.
  2. The sort method should accept an integer array and sort it.
  3. Iterate over the unsorted portion of the array to find the minimum.
  4. The minimum item is then swapped so that it can be added to the end of the sorted portion.

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.

    */

  }

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

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

Add a comment
Know the answer?
Add Answer to:
Scenario Selection sort is best understood by imagining that you have two lists, Aand B. Initially,...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT