Modify the sorts (selection sort, insertion sort, bubble sort, quick sort, and merge sort) by adding code to each to tally the total number of comparisons and total execution time of each algorithm. Execute the sort algorithms against the same list, recording information for the total number of comparisons and total execution time for each algorithm. Try several different lists, including at least one that is already in sorted order.
----------------------------------------------------------------------------------------------------------------
/**
* Sorting demonstrates sorting and searching on an array
* of objects.
*
* @author Java Foundations
* @version 4.0
*/
public class Sorting
{
/**
* Sorts the specified array of integers using the
selection
* sort algorithm.
*
* @param data the array to be sorted
*/
public static <T extends
Comparable<T>>
void selectionSort(T[] data)
{
int min;
T temp;
for (int index = 0; index <
data.length - 1; index++)
{
min =
index;
for (int scan =
index + 1; scan < data.length; scan++)
if (data[scan].compareTo(data[min]) <
0)
min = scan;
swap(data,
min, index);
}
}
/**
* Swaps to elements in an array. Used by various
sorting algorithms.
*
* @param data the array in which the elements are
swapped
* @param index1 the index of the first element to be
swapped
* @param index2 the index of the second element to be
swapped
*/
private static <T extends
Comparable<T>>
void swap(T[] data, int index1, int index2)
{
T temp = data[index1];
data[index1] = data[index2];
data[index2] = temp;
}
/**
* Sorts the specified array of objects using an
insertion
* sort algorithm.
*
* @param data the array to be sorted
*/
public static <T extends
Comparable<T>>
void insertionSort(T[] data)
{
for (int index = 1; index <
data.length; index++)
{
T key =
data[index];
int position =
index;
// shift
larger values to the right
while (position
> 0 && data[position-1].compareTo(key) > 0)
{
data[position] = data[position - 1];
position--;
}
data[position] = key;
}
}
/**
* Sorts the specified array of objects using a bubble
sort
* algorithm.
*
* @param data the array to be sorted
*/
public static <T extends
Comparable<T>>
void bubbleSort(T[] data)
{
int position, scan;
for (position = data.length - 1;
position >= 0; position--)
{
for (scan = 0;
scan <= position - 1; scan++)
{
if (data[scan].compareTo(data[scan + 1]) >
0)
swap(data, scan, scan +
1);
}
}
}
/**
* Sorts the specified array of objects using the quick
sort algorithm.
*
* @param data the array to be sorted
*/
public static <T extends
Comparable<T>>
void quickSort(T[] data)
{
quickSort(data, 0, data.length -
1);
}
/**
* Recursively sorts a range of objects in the
specified array using the
* quick sort algorithm.
*
* @param data the array to be sorted
* @param min the minimum index in the range to be
sorted
* @param max the maximum index in the range to be
sorted
*/
private static <T extends
Comparable<T>>
void quickSort(T[] data, int min, int max)
{
if (min < max)
{
// create
partitions
int
indexofpartition = partition(data, min, max);
// sort the
left partition (lower values)
quickSort(data,
min, indexofpartition - 1);
// sort the
right partition (higher values)
quickSort(data,
indexofpartition + 1, max);
}
}
/**
* Used by the quick sort algorithm to find the
partition.
*
* @param data the array to be sorted
* @param min the minimum index in the range to be
sorted
* @param max the maximum index in the range to be
sorted
*/
private static <T extends
Comparable<T>>
int partition(T[] data, int min, int max)
{
T partitionelement;
int left, right;
int middle = (min + max) / 2;
// use the middle data value as
the partition element
partitionelement =
data[middle];
// move it out of the way for
now
swap(data, middle, min);
left = min;
right = max;
while (left < right)
{
// search for an
element that is > the partition element
while (left <
right && data[left].compareTo(partitionelement) <=
0)
left++;
// search for
an element that is < the partition element
while
(data[right].compareTo(partitionelement) > 0)
right--;
// swap the
elements
if (left <
right)
swap(data, left, right);
}
// move the partition element
into place
swap(data, min, right);
return right;
}
/**
* Sorts the specified array of objects using the merge
sort
* algorithm.
*
* @param data the array to be sorted
*/
public static <T extends
Comparable<T>>
void mergeSort(T[] data)
{
mergeSort(data, 0, data.length -
1);
}
/**
* Recursively sorts a range of objects in the
specified array using the
* merge sort algorithm.
*
* @param data the array to be sorted
* @param min the index of the first element
* @param max the index of the last element
*/
private static <T extends
Comparable<T>>
void mergeSort(T[] data, int min, int max)
{
if (min < max)
{
int mid = (min +
max) / 2;
mergeSort(data,
min, mid);
mergeSort(data,
mid+1, max);
merge(data, min,
mid, max);
}
}
/**
* Merges two sorted subarrays of the specified
array.
*
* @param data the array to be sorted
* @param first the beginning index of the first
subarray
* @param mid the ending index fo the first
subarray
* @param last the ending index of the second
subarray
*/
@SuppressWarnings("unchecked")
private static <T extends
Comparable<T>>
void merge(T[] data, int first, int mid, int
last)
{
T[] temp = (T[])(new
Comparable[data.length]);
int first1 = first, last1 = mid;
// endpoints of first subarray
int first2 = mid + 1, last2 = last;
// endpoints of second subarray
int index = first1; // next index
open in temp array
// Copy smaller item from each
subarray into temp until one
// of the subarrays is
exhausted
while (first1 <= last1
&& first2 <= last2)
{
if
(data[first1].compareTo(data[first2]) < 0)
{
temp[index] = data[first1];
first1++;
}
else
{
temp[index] = data[first2];
first2++;
}
index++;
}
// Copy remaining elements from
first subarray, if any
while (first1 <= last1)
{
temp[index] =
data[first1];
first1++;
index++;
}
// Copy remaining elements from
second subarray, if any
while (first2 <= last2)
{
temp[index] =
data[first2];
first2++;
index++;
}
// Copy merged data into
original array
for (index = first; index <=
last; index++)
data[index] =
temp[index];
}
}
/**
* Sorting.java
*
*
* Modify the sorts listed in Chapter 18 (selection sort, insertion
sort, bubble sort, quick sort, and merge sort)
* by adding code to each to tally the total number of comparisons
and total execution time of each algorithm.
* Execute the sort algorithms against the same list, recording
information for the total number of comparisons
* and total execution time for each algorithm. Try several
different lists, including at least one that is already
* in sorted order. It is recommend that you use a spreadsheet to
present the test cases you have prepared,
* along with the comparisons and execution time.
*/
public class Sorting
{
private static double startTime;
private static double finishTime;
public static double selectionSortTime;
public static double insertionSortTime;
public static double bubbleSortTime;
public static double quickSortTime;
public static double mergeSortTime;
public static long
selectionSortComparisons;
public static long
insertionSortComparisons;
public static long bubbleSortComparisons;
public static long quickSortComparisons;
public static long mergeSortComparisons;
/**
* Sorts the specified array of integers
using the selection
* sort algorithm.
*
* @param data the array to be sorted
*/
public static <T extends
Comparable<T>>
void selectionSort(T[] data)
{
int min;
T temp;
startTime =
System.nanoTime();
for (int index = 0;
index < data.length - 1; index++)
{
min = index;
for (int scan = index + 1; scan < data.length; scan++) {
selectionSortComparisons++;
if (data[scan].compareTo(data[min]) < 0)
min = scan;
}
swap(data, min, index);
}
finishTime =
System.nanoTime();
selectionSortTime =
(finishTime - startTime) / 1000;
}
/**
* Swaps to elements in an array. Used by
various sorting algorithms.
*
* @param data the array in
which the elements are swapped
* @param index1 the index of the first
element to be swapped
* @param index2 the index of the second
element to be swapped
*/
private static <T extends
Comparable<T>>
void swap(T[] data, int index1, int
index2)
{
T temp =
data[index1];
data[index1] =
data[index2];
data[index2] =
temp;
}
/**
* Sorts the specified array of objects
using an insertion
* sort algorithm.
*
* @param data the array to be sorted
*/
public static <T extends
Comparable<T>>
void insertionSort(T[] data)
{
startTime =
System.nanoTime();
for (int index = 1;
index < data.length; index++)
{
T key = data[index];
int position = index;
// shift larger values to the right
while (position > 0 && data[position-1].compareTo(key)
> 0)
{
data[position] = data[position - 1];
position--;
insertionSortComparisons++;
}
data[position] = key;
insertionSortComparisons++;
}
finishTime =
System.nanoTime();
insertionSortTime =
(finishTime - startTime) / 1000;
}
/**
* Sorts the specified array of objects
using a bubble sort
* algorithm.
*
* @param data the array to be sorted
*/
public static <T extends
Comparable<T>>
void bubbleSort(T[] data)
{
int position, scan;
startTime = System.nanoTime();
for (position =
data.length - 1; position >= 0; position--)
{
for (scan = 0; scan <= position - 1; scan++)
{
bubbleSortComparisons++;
if (data[scan].compareTo(data[scan + 1]) > 0)
swap(data, scan, scan + 1);
}
}
finishTime =
System.nanoTime();
bubbleSortTime =
(finishTime - startTime) / 1000;
}
/**
* Sorts the specified array of objects
using the quick sort algorithm.
*
* @param data the array to be sorted
*/
public static <T extends
Comparable<T>>
void quickSort(T[] data)
{
startTime =
System.nanoTime();
quickSort(data, 0, data.length - 1);
finishTime =
System.nanoTime();
quickSortTime =
(finishTime - startTime) / 1000;
}
/**
* Recursively sorts a range of objects in
the specified array using the
* quick sort algorithm.
*
* @param data the array to be sorted
* @param min the minimum index in the
range to be sorted
* @param max the maximum index in the
range to be sorted
*/
private static <T extends
Comparable<T>>
void quickSort(T[] data, int min, int max)
{
if (min < max)
{
// create partitions
int indexofpartition = partition(data, min, max);
// sort the left partition (lower values)
quickSort(data, min, indexofpartition - 1);
// sort the right partition (higher values)
quickSort(data, indexofpartition + 1, max);
}
}
/**
* Used by the quick sort algorithm to find
the partition.
*
* @param data the array to be sorted
* @param min the minimum index in the
range to be sorted
* @param max the maximum index in the
range to be sorted
*/
private static <T extends
Comparable<T>>
int partition(T[] data, int min, int max)
{
T
partitionelement;
int left, right;
int middle = (min + max)
/ 2;
// use the middle
data value as the partition element
partitionelement =
data[middle];
// move it out of the
way for now
swap(data, middle,
min);
left = min;
right = max;
while (left <
right)
{
// search for an element that is > the partition element
while (left < right &&
data[left].compareTo(partitionelement) <= 0)
left++;
quickSortComparisons++;
// search for an element that is < the partition element
while (data[right].compareTo(partitionelement) > 0)
right--;
quickSortComparisons++;
// swap the elements
if (left < right)
swap(data, left, right);
}
// move the partition
element into place
swap(data, min,
right);
return right;
}
/**
* Sorts the specified array of objects
using the merge sort
* algorithm.
*
* @param data the array to be sorted
*/
public static <T extends
Comparable<T>>
void mergeSort(T[] data)
{
startTime =
System.nanoTime();
mergeSort(data, 0, data.length - 1);
finishTime =
System.nanoTime();
mergeSortTime =
(finishTime - startTime) / 1000;
}
/**
* Recursively sorts a range of objects in
the specified array using the
* merge sort algorithm.
*
* @param data the array to be sorted
* @param min the index of the first
element
* @param max the index of the last
element
*/
private static <T extends
Comparable<T>>
void mergeSort(T[] data, int min, int max)
{
if (min < max)
{
int mid = (min + max) / 2;
mergeSort(data, min, mid);
mergeSort(data, mid+1, max);
merge(data, min, mid, max);
}
}
/**
* Merges two sorted subarrays of the
specified array.
*
* @param data the array to be sorted
* @param first the beginning index of the
first subarray
* @param mid the ending index fo the first
subarray
* @param last the ending index of the
second subarray
*/
@SuppressWarnings("unchecked")
private static <T extends
Comparable<T>>
void merge(T[] data, int first, int mid, int
last)
{
T[] temp = (T[])(new
Comparable[data.length]);
int first1 = first,
last1 = mid; // endpoints of first subarray
int first2 = mid + 1,
last2 = last; // endpoints of second subarray
int index = first1; //
next index open in temp array
// Copy smaller item
from each subarray into temp until one
// of the subarrays is
exhausted
while (first1 <=
last1 && first2 <= last2)
{
if (data[first1].compareTo(data[first2]) < 0)
{
temp[index] = data[first1];
first1++;
mergeSortComparisons++;
}
else
{
temp[index] = data[first2];
first2++;
mergeSortComparisons++;
}
index++;
}
// Copy remaining
elements from first subarray, if any
while (first1 <=
last1)
{
temp[index] = data[first1];
first1++;
index++;
}
// Copy remaining
elements from second subarray, if any
while (first2 <=
last2)
{
temp[index] = data[first2];
first2++;
index++;
}
// Copy merged data
into original array
for (index = first;
index <= last; index++)
data[index] = temp[index];
}
}
Modify the sorts (selection sort, insertion sort, bubble sort, quick sort, and merge sort) by adding code to each to tally the total number of comparisons and total execution time of each algorithm. E...
Using Merge Sort: (In Java) (Please screenshot or copy your output file in the answer) In this project, we combine the concepts of Recursion and Merge Sorting. Please note that the focus of this project is on Merging and don't forget the following constraint: Programming Steps: 1) Create a class called Art that implements Comparable interface. 2) Read part of the file and use Merge Sort to sort the array of Art and then write them to a file. 3)...
The file Sorting.java contains the Sorting class from Listing 9.9 in the text. This class implements both the selection sort and the insertion sort algorithms for sorting any array of Comparable objects in ascending order. In this exercise, you will use the Sorting class to sort several different types of objects. 1. The file Numbers.java reads in an array of integers, invokes the selection sort algorithm to sort them, and then prints the sorted array. Save Sorting.java and Numbers.java to...
Objective: GUI Layout manager Download one of the sample GUI layout program. Use any GUI layout to add buttons to start each sort and display the System.nanoTime in common TextArea panel. The question is a bit confusing so i will try to simplify it. Using the GUI ( I made a unclick able one so you have to make it clickable), allow a user to sort the text file based on what they click on. example: if i click merge...
Java Programming Write a program to find the number of comparison using binarySearch and the sequentialSearch algorithms as follows: Suppose list is an array of 2500 elements. 1. Use a random number generator to fill list; 2. Use a sorting algorithm to sort list; 3. Search list for some items as follows: a) Use the binary search algorithm to search list (please work on SearchSortAlgorithms.java and modify the algorithm to count the number of comparisons) b) Use the sequential search...
//Generic interface that describes various searching and sorting //algorithms. Note that the type parameter is unbounded. However, //for these algorithms to work correctly, the data objects must //be compared using the method compareTo and equals. //In other words, the classes implementing the list objects //must implement the interface Comparable. The type parameter T //is unbounded because we would like to use these algorithms to //work on an array of objects as well as on objects of the classes //UnorderedArrayList and...
// ArrayIns.java // demonstrates insertion sort 11--- class ArrayIns private long[] a; private int nElems; // ref to array a // number of data items public ArrayIns(int max) // constructor a = new long[max]; nElems - © // create the array // no items yet --- public void insert(long value) // put element into array a[nElems] = value; nElems++; // insert it // increment size public void display() // displays array contents for(int j=0; j<ntlems; 1++) 1/ for each element,...
JAVA: Already completed: MyList.java, MyAbstractList.java, MyArrayList.java, MyLinkedLink.java, MyStack.java, MyQueue.java. Need to complete: ReversePoem.java. This program has you display a pessimistic poem from a list of phrases. Next, this program has you reverse the phrases to find another more optimistic poem. Use the following algorithm. 1. You are given a list of phrases each ending with a pound sign: ‘#’. 2. Create a single String object from this list. 3. Then, split the String of phrases into an array of phrases...