Question: Complete the question in java language............ 1. .Modify the sorts listed in source code below(selection sort, insertion sort, bubble sort, quick sort, and merge sort)
Complete the question in java language............
1..Modify the sorts listed in source code below(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. Remember that arrays are passed by reference in Java. Use Arrays.copyOf() or a similar method to test each sorting method on the exact same data content in separate arrays. Source code for the question is written below public class Sorting { /** * Sorts the specified array of integers using the selection * sort algorithm. * * @param data the array to be sorted */ public static
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
/** * Sorts the specified array of objects using an insertion * sort algorithm. * * @param data the array to be sorted */ public static
// 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
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
/** * 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
// 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
// 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
/** * 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
/** * 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
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]; }
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
