Question: The Bubble Sort is named due to how its sorting mechanism moves data items around during its sorting procedure. Heavy items sink to the bottom
The Bubble Sort is named due to how its sorting mechanism moves data items around during its sorting procedure. Heavy items sink to the bottom while smaller items bubble their way to the top by comparing neighbors and checking for inversions that is two elements out of order that require a swap. By comparing successive neighbors, we are guaranteed the max element in the last place after the first pass, so we are able to optimize on this search by only inspecting neighbors in the unsorted part of the array the unsorted partition This sort grows the sorted partition by one element the largest each pass and shrinks the unsorted partition accordingly.
Download the Sort.java file.
Implement the swap method, which swaps two elements of an array. The method should swap the element at indexA with the element at indexB. You can assume that indexA and indexB are within the bounds of the array.
Implement the bubbleSort method by following the pseudocode in the lecture slides or your textbook.
Test your bubble sort by running the main method.
Experiment with optimizing the bubble sort. Can you reduce the inner loop relative to the outer loop? Can you rewrite the outer loop so that it never executes additional times if the list is sorted early For example, the array will need the outer loop to execute only one time, and not three times.
Implement another version of the bubble sort method that sorts an array of strings. You will need to write another swap method as well. To compare strings, you can use the compareTo method. This method compares two strings "lexicographically", that is by dictionary order. If you call stringA.compareTostringB the result is a negative integer if stringA is less than stringB, a positive integer if stringA is greater than stringB, and zero if the strings are equal.
Test your string sort in the main method.
This class implements multiple sort algorithms.
@author jack Crelencia
@version CSSSKL
public class Sort
public static final int SIZE ;
public static void mainString args
int bubbleArray new intSIZE;
int selectionArray new intSIZE;
int insertionArray new intSIZE;
for int i ; i SIZE; i
bubbleArrayiintMathrandom;
selectionArrayiintMathrandom;
insertionArrayiintMathrandom;
System.out.printlnArray before bubble sort:";
System.out.printlnArraystoStringbubbleArray;
bubbleSortbubbleArray;
System.out.printlnArray after bubble sort:";
System.out.printlnArraystoStringbubbleArray;
System.out.println;
System.out.printlnArray before selection sort:";
System.out.printlnArraystoStringselectionArray;
selectionSortselectionArray;
System.out.printlnArray after selection sort:";
System.out.printlnArraystoStringselectionArray;
System.out.println;
System.out.printlnArray before insertion sort:";
System.out.printlnArraystoStringinsertionArray;
insertionSortinsertionArray;
System.out.printlnArray after insertion sort:";
System.out.printlnArraystoStringinsertionArray;
TODO Test your string sort here
@param numbers
public static void bubbleSortint numbers
Implement your sort, call a helper swap method
@param numbers
@param indexA
@param indexB
public static void swapint numbers, int indexA, int indexB
swap the elements at indexA and indexB
selection sort for ints
public static void selectionSortint numbers
Implement your sort, call swapSelectionint intList, int i in nextMin
public static int findSmallestint arr, int begin, int end
int minIndex begin;
int minValue arrbegin;
for int i begin ; i end; i
if arri minValue
minIndex i;
minValue arri;
return minIndex;
@param numbers
public static void insertionSortint numbers
Implement your insertion sort
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
