I currently have this but it doesn't work for the three item arrays
public class Quicksort extends Partitioner {
public static void quicksort(int[] values) {
if (values == null || values.length < 2) {
return;
}
quicksort(values, 0, values.length - 1);
}
private static void quicksort(int[] values, int start, int end)
{
System.out.println(values);
System.out.println(start);
System.out.println(end);
if (values == null || Math.abs(start - end) == 1) {
return;
}
if (end > start) {
int pivotValueIndex = partition(values, start, end);
quicksort(values, start, pivotValueIndex - 1);
quicksort(values, pivotValueIndex + 1, end);
}
}
}
The sort function should be:
void sort(int values[], int start, int end)
{
if (start < end)
{
// get the partition index
int pi = partition(values, start, end);
// recursively sort the elements that lie on the left
and
// the right part of the elements
sort(values, start, pi-1);
sort(values, pi+1, end);
}
}
If replacing this code still doesn't work then check whether the partition function's code is correct or not. The partition function's code looks like:
int partition(int values[], int start, int end)
{
int pivot = values[end];
int i = (start-1); // index of smaller element
for (int j=start; j<end; j++)
{
// If current element is smaller than the pivot
if (values[j] < pivot)
{
i++;
// swap values[i] and values[j]
int x = values[i];
values[i] = values[j];
values[j] = x;
}
}
// swap the value of the pivot with it's correct
position
// i.e swap pivot with the element are ith index
int x = values[i+1];
values[i+1] = values[end];
values[end] = x;
return i+1;
}
The full code for quick sort should look like this:
I currently have this but it doesn't work for the three item arrays public class Quicksort...
HW60.1. Array Quicksort You've done partition so now it's time to finish Quicksort. Create a public non-final class named Quicksort that extends Partitioner. Implement a public static method void quicksort (int] values) that sorts the input array of ints in ascending order. You will want this method to be recursive, with the base case being an array with zero or one value. You should sort the array in place, which is why your function is declared to return void. If...
import java.util.Arrays; public class lab { public static void main(String args[]) { int arr[] = {10, 7, 8, 9, 1, 5,6,7}; int arr2[] = {9, 8, 7, 6, 5, 4, 3, 2, 1}; int arr3[] = {1, 3, 5, 3, 2, 6, 20}; quicksort(arr,0,arr.length-1); quicksort(arr2,0,arr2.length-1); quicksort(arr3,0,arr3.length-1); System.out.println(Arrays.toString(arr)); System.out.println(Arrays.toString(arr2)); System.out.println(Arrays.toString(arr3)); } private static int partition(int[] items,int low, int high) { int i=0; int j=0;...
must provide the following public interface: public static void insertSort(int [] arr); public static void selectSort(int [] arr); public static void quickSort(int [] arr); public static void mergeSort(int [] arr); The quick sort and merge sort must be implemented by using recursive thinking. So the students may provide the following private static methods: //merge method //merge two sorted portions of given array arr, namely, from start to middle //and from middle + 1 to end into one sorted portion, namely,...
quickSort function. Calling previous functions already implemented. This is a bit different type of quicksort function where my partition function has 3 parameters instead of 4. So I need to write a function quicksort that actually puts all 3 of my functions together and finalizes the sorting process "There should be almost nothing done besides calling the other 3 functions and recursively calling itself (except to check for basecase) class quicksort { private: int left; int right; int* array; ...
I am currently using eclipse to write in java. A snapshot of the output would be greatly appreciated to verify that the program is indeed working. Thanks in advance for both your time and effort. Here is the previous exercise code: /////////////////////////////////////////////////////Main /******************************************* * Week 5 lab - exercise 1 and exercise 2: * * ArrayList class with search algorithms * ********************************************/ import java.util.*; /** * Class to test sequential search, sorted search, and binary search algorithms * implemented in...
This is the assignment..... Write a class DataSet that stores a number of values of type double. Provide a constructor public DataSet(int maxNumberOfValues) and a method public void addValue(double value) that add a value provided there is still room. Provide methods to compute the sum, average, maximum and minimum value. This is what I have, its suppose to be using arrays also the double smallest = Double.MAX_VALUE; and double largest = Double.MIN_VALUE; are there so I don't need to create...
JAVA- Trace the recursive quick sort and partition methods in Lab6.java for this list of numbers: 47 71 15 35 66 61 44 26 68 56 18 19 36 84 69 55 1. Find the value of pivot 2. Show the result after partitionIt() is called first time 3. Show the value of partition 4. Show the content of the array ///////////////////////////// Lab6.java class ArrayIns { private long[] theArray; // ref to array theArray private int nElems; // number of...
/** * A collection of methods related to multi-dimensional arrays. */ public class Array2Lab { /** * Return whether k is in list. * Precondition: the elements of list are not null. * @param list the array to be searched. * @param k the number to search for. * @return true if k is an element of list, and false otherwise. */ public static boolean contains(Object[][] list, Object k) { return false; } /** * Create a String that...
HW58.1. Array Merge Sort You've done merge (on Lists), so now it's time to do merge sort (on arrays). Create a public non-final class named Mergesort that extends Merge. Implement a public static method int[] mergesort(int[] values) that returns the input array of ints sorted in ascending order. You will want this method to be recursive, with the base case being an array with zero or one value. If the passed array is null you should return null. If the...
public class ConsCell { private int head; private ConsCell tail; public ConsCell(int h, ConsCell t) { head = h; tail = t; } public int getHead() { return head; } public ConsCell getTail() { return tail; } public void setTail(ConsCell t) { tail = t; } } public class IntList { private ConsCell start; public IntList (ConsCell s) { start = s; } public IntList cons(int h) { return new IntList(new ConsCell(h, start)); } public int length() { int len...