Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

JAVA Selection sort is best understood by imagining that you have two lists, A and B. Initially, we have list A, containing all the unsorted

JAVA

Selection sort is best understood by imagining that you have two lists, A and 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.

REQUIRED TASKS:

The sort() method handles sorting even and odd elements

The sort() method handles sorting an empty array and an array with one element.

The sort() method handles a sorted array as a parameter and an array with identical elements.

GIVEN CODE:

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. */ }

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Object Databases The Essentials

Authors: Mary E. S. Loomis

1st Edition

020156341X, 978-0201563412

Students also viewed these Databases questions

Question

Acceptance of the key role of people in this process of adaptation.

Answered: 1 week ago

Question

preference for well defined job functions;

Answered: 1 week ago