1. [2 points] What makes a program easy to modify? 2. [3 points] D...

Solution Preview

These solutions may offer step-by-step problem-solving explanations or good writing examples that include modern styles of formatting and construction of bibliographies out of text citations and references. Students may use these solutions for personal skill-building and practice. Unethical use is strictly forbidden.

import java.util.*;
import java.text.DecimalFormat;

public class Sorts
{
static final int SIZE = 50;            // size of array to be sorted
static int[] values = new int[SIZE];   // values to be sorted
static int swaps = 0;
static int compares = 0;

static void initValues()
// Initializes the values array with random integers from 0 to 99.
{
Random rand = new Random();
for (int index = 0; index < SIZE; index++)
values[index] = Math.abs(rand.nextInt()) % 100;
}

static public boolean isSorted()
// Returns true if the array values are sorted and false otherwise.
{
boolean sorted = true;
for (int index = 0; index < (SIZE - 1); index++)
if (values[index] > values[index + 1])
sorted = false;
return sorted;
}

static public void swap(int index1, int index2)
// Precondition: index1 and index2 are >= 0 and < SIZE.
//
// Swaps the integers at locations index1 and index2 of the values array.
{
int temp = values[index1];
values[index1] = values[index2];
values[index2] = temp;
swaps += 1;
}

static public void printValues()
// Prints all the values integers.
{
int value;
DecimalFormat fmt = new DecimalFormat("00");
System.out.println("The values array is:");
for (int index = 0; index < SIZE; index++)
{
value = values[index];
if (((index + 1) % 10) == 0)
System.out.println(fmt.format(value));
else
System.out.print(fmt.format(value) + " ");
}
System.out.println();
}

/////////////////////////////////////////////////////////////////
//
// Selection Sort

static int minIndex(int startIndex, int endIndex)
// Returns the index of the smallest value in
// values[startIndex]..values[endIndex].
{
int indexOfMin = startIndex;
for (int index = startIndex + 1; index <= endIndex; index++)
if (values[index] < values[indexOfMin])
indexOfMin = index;
return indexOfMin;
}

static void selectionSort()
// Sorts the values array using the selection sort algorithm.
{
swaps = 0;
compares = 0;
int endIndex = SIZE - 1;
for (int current = 0; current < endIndex; current++)
{
compares++;
swap(current, minIndex(current, endIndex));
// printValues();
}
System.out.println("Selection Sort");
System.out.println("Number of swaps: " + swaps);
System.out.println("Number of compares: " + compares);
}

/////////////////////////////////////////////////////////////////
//
// Bubble Sort

static void bubbleUp(int startIndex, int endIndex)
// Switches adjacent pairs that are out of order
// between values[startIndex]..values[endIndex]
// beginning at values[endIndex].
{
for (int index = endIndex; index > startIndex; index--) {
compares++;
if (values[index] < values[index - 1])
swap(index, index - 1);
}
}

static void bubbleSort()
// Sorts the values array using the bubble sort algorithm.
{
int current = 0;
swaps = 0;
compares = 0;
while (current < (SIZE - 1))
{
bubbleUp(current...

By purchasing this solution you'll be able to access the following files:
Solution.java and Solution.docx.

\$60.00
for this solution

PayPal, G Pay, ApplePay, Amazon Pay, and all major credit cards accepted.

Find A Tutor

View available Data Structures and Algorithms Tutors

Get College Homework Help.

Are you sure you don't want to upload any files?

Fast tutor response requires as much info as possible.