Question
Java assistance : The part in bold for quick sort/ partition is causing the most issues. But the bold part in bold in public static
Java assistance: The part in bold for quick sort/ partition is causing the most issues. But the bold part in bold in public static void selectionSort (int[] nums) shouldn't need to be in the code ( i think, but can't get it to look right without it)
Directions:
My code (leaving out correct parts for sack of space :
package sorttimer2;
import java.util.Arrays; import java.util.Random; import java.util.Scanner;
..... public static void mergeSort(int[] nums, int left, int right) { if (left mid +1) { if (m System.out.println("nums = " + Arrays.toString(nums)); *** dont think this should need to be here**** } public static void mergeSortPrint(int[] nums, int left, int right) { if (left mid +1) { if (m public static void quickSort(int[] nums, int left, int right ) { if (left
// Partitions the subarray nums[left..right]. public static int partition(int[] nums, int left, int right) { int pivot = nums[left]; int i = left; int j = right; while (i pivot) j--; if (i
// Uses the quick sort to sort the subarray nums[left..right]. // Prints the nums array after the subarray nums[left..right] has been // partitioned. public static void quickSortPrint(int[] nums, int left, int right ) { if (left
public static void main(String[] args) { .... int[] numsMerge = {5,3,6,4,2,0,1}; System.out.println("Merge Sort:"); System.out.println("nums = " + Arrays.toString(numsMerge)); mergeSortPrint(numsMerge, 0, numsMerge.length-1); System.out.println(); int[] numsQuick = {5,8,4,7,2,1,6,3}; System.out.println("Quick Sort:"); System.out.println("nums = " + Arrays.toString(numsQuick)); quickSortPrint(numsQuick, 0, numsQuick.length-1); System.out.println();
My Output:
...
Merge Sort: nums = [5, 3, 6, 4, 2, 0, 1] nums = [3, 5, 6, 4, 2, 0, 1] nums = [3, 5, 4, 6, 2, 0, 1] nums = [3, 4, 5, 6, 2, 0, 1] nums = [3, 4, 5, 6, 0, 2, 1] nums = [3, 4, 5, 6, 0, 1, 2] nums = [0, 1, 2, 3, 4, 5, 6]
Quick Sort: nums = [5, 8, 4, 7, 2, 1, 6, 3] nums = [3, 1, 4, 2, 5, 7, 6, 8] nums = [2, 1, 3, 4, 5, 7, 6, 8] nums = [1, 2, 3, 4, 5, 7, 6, 8] nums = [1, 2, 3, 4, 5, 6, 7, 8]
The output its supposed to have
Name this project SortTimer2 CSc 135134 Spring 2019 Lab5 te a ciasa Sort Timerz vith the tollowing Methoda This method uses the selection aort to Bort an array of integere, named numa, into increaning order 1. pabiie static void selectionsort (int nuna) Create thia method by copying and pasting the aelectionsort method in step 1 above and then adding the statenent 2, pablie atatie void selectionSortPrint(int t) nuns Syaten.out print in ("numnArrays, tostring (nuna)) at the end of the first for 1oop (right before the that enda the tirat tor loop) 3. publie statio void insertionsort (inttl numa) ses the insertion sort to sort an array of integers, named numa, into increasing order 4, public atatio void insertionsortPrint lintl) nuns) then adaing ched past ing the ineert ionsort method in step 3 above iret for loop (right betore the 1 that enda the tirst for loop) System.out.println("numArrays.tostring (numa)3 a public atatic void mergeSort (int1 numa, int left. int right) This method sorts the subarray nums (left.right] of the nums array into ncreasing order using the merge sort algorithm. Use the following paeudocode for thie method If left right, do the following a. Let mid (left right)/2 mid, make a recursive call nergeSort (nums, left, mid) to sort b. If left c. If mid.1 right, make a recursive call mergeSort (nums, mid+i, right) to d. Create a new integer array result of size right-left+1 by merging the the subarray numa [left..mid] sort the subarray nums (mid-1. .right) sorted subarrays nuns (left. .mid and numsmid.l..right] as follows di. Inicialize m left, n mid+i, k 0 d2. While (in mid)&&(n s right)), copy the smaller of nums [m) and nums (n] to result (k] and then increase k by 1 and increase by 1 either n or n depend ing on whether nums im) or nums In) was copied. d3. It rn > mid+1), then if (mS mld) copy the remainder of nums [iefc. .mid] into the result array, otherwise copy the remainder of nums (mid+l..right] into the result array. Then copy the result array to the subazray nums [left. .right) pablic static void mergesortPrint (int l numa, int left, int right) Create this method by copying and pasting the mergesort and then adding the statement: method in Step 5 above stem.out.printin("numsArrays.tostring (nums)) Sy at the end of the method (right before the ) at the end of method) blic atatie void quicksort (int nums, int le this method sorts the nums(left..rig increasing order usine dtbaray nung tleft.rht nuns array into increasing order using the quick algor ort algorithm. Use the following pseudocode for this sethod: It left * pivot c2. Keep on decrementing until nums (33 pivot c3. If i
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started