I wrote the code of merge sort today, and I meet a StackOverFlow error when I run int mid = (left - right)/2 + right;. But when I use int mid = left + (right - left) / 2; everything is fine. I'm very confused because the mathematical meaning of the two of them is the same(if it's not, why?).
I have seen this question but I'm not sure it answers this problem.
why left+(right-left)/2 will not overflow?
Here's my code,I am sorry that I did not upload the code before.
import java.util.Arrays;
public class Solution {
public static void main(String[] args) {
int[] array = {3,5,1,2,4,8};
int[] result = mergeSort(array);
System.out.println(Arrays.toString(result));
}
public static int[] mergeSort(int[] array) {
if (array == null || array.length <= 1) {
return array;
}
int[] helper = new int[array.length];
sort(array, helper, 0, array.length - 1);
return array;
}
public static void sort(int[] array, int[] helper, int left, int right) {
if (left >= right) {
return;
}
int mid = (left - right)/2 + right;
//int mid = left + (right - left) / 2;
sort(array, helper, left, mid);
sort(array, helper, mid + 1, right);
combine(array, helper, left, mid, right);
}
public static void combine(int[] array, int[] helper, int left, int mid, int right) {
for (int i = left; i <= right; i++) {
helper[i] = array[i];
}
int leftIndex = left;
int rightIndex = mid + 1;
while (leftIndex <= mid && rightIndex <= right) {
if (helper[leftIndex] <= helper[rightIndex]) {
array[left] = helper[leftIndex];
left++;
leftIndex++;
} else {
array[left] = helper[rightIndex];
left++;
rightIndex++;
}
}
while (leftIndex <= mid) {
array[left] = helper[leftIndex];
left++;
leftIndex++;
}
}
}
As you run this, sooner or later, it's likely that left will be one less than right - for example, maybe left = 3 and right = 4.
In that case, (left - right) / 2 and (right - left) / 2 both evaluate to 0, since integer division rounds towards zero. So, if you have
int mid = (left - right)/2 + right;
sort(array, helper, left, mid);
within your call to sort(array, helper, left, right), then you're repeating a call with the exact same values. This causes infinite recursion - a StackOverflowError.
But when you have
int mid = left + (right - left)/2;
sort(array, helper, left, mid);
then you're repeating a call with different values, so the recursion has a chance to end.
Initially, you have left < right. And, of course, right - left > 0, and furthermore left + (right - left) = right (follows from basic algebra).
And consequently left + (right - left) / 2 <= right. So no overflow can happen since every step of the operation is bounded by the value of right.
Related
I am trying to implement the median of medians algorithm in Java. The algorithm shall determine the median of a set of numbers. I tried to implement the pseudo code on wikipedia:
https://en.wikipedia.org/wiki/Median_of_medians
I am getting a buffer overflow and don't know why. Due to the recursions it's quite difficult to keep track of the code for me.
import java.util.Arrays;
public class MedianSelector {
private static final int CHUNK = 5;
public static void main(String[] args) {
int[] test = {9,8,7,6,5,4,3,2,1,0,13,11,10};
lowerMedian(test);
System.out.print(Arrays.toString(test));
}
/**
* Computes and retrieves the lower median of the given array of
* numbers using the Median algorithm presented in the lecture.
*
* #param input numbers.
* #return the lower median.
* #throw IllegalArgumentException if the array is {#code null} or empty.
*/
public static int lowerMedian(int[] numbers) {
if(numbers == null || numbers.length == 0) {
throw new IllegalArgumentException();
}
return numbers[select(numbers, 0, numbers.length - 1, (numbers.length - 1) / 2)];
}
private static int select(int[] numbers, int left, int right, int i) {
if(left == right) {
return left;
}
int pivotIndex = pivot(numbers, left, right);
pivotIndex = partition(numbers, left, right, pivotIndex, i);
if(i == pivotIndex) {
return i;
}else if(i < pivotIndex) {
return select(numbers, left, pivotIndex - 1, i);
}else {
return select(numbers, left, pivotIndex + 1, i);
}
}
private static int pivot(int numbers[], int left, int right) {
if(right - left < CHUNK) {
return partition5(numbers, left, right);
}
for(int i=left; i<=right; i=i+CHUNK) {
int subRight = i + (CHUNK-1);
if(subRight > right) {
subRight = right;
}
int medChunk = partition5(numbers, i, subRight);
int tmp = numbers[medChunk];
numbers[medChunk] = numbers[(int) (left + Math.floor((double) (i-left)/CHUNK))];
numbers[(int) (left + Math.floor((double) (i-left)/CHUNK))] = tmp;
}
int mid = (right - left) / 10 + left +1;
return select(numbers, left, (int) (left + Math.floor((right - left) / CHUNK)), mid);
}
private static int partition(int[] numbers, int left, int right, int idx, int k) {
int pivotVal = numbers[idx];
int storeIndex = left;
int storeIndexEq = 0;
int tmp = 0;
tmp = numbers[idx];
numbers[idx] = numbers[right];
numbers[right] = tmp;
for(int i=left; i<right; i++) {
if(numbers[i] < pivotVal) {
tmp = numbers[i];
numbers[i] = numbers[storeIndex];
numbers[storeIndex] = tmp;
storeIndex++;
}
}
storeIndexEq = storeIndex;
for(int i=storeIndex; i<right; i++) {
if(numbers[i] == pivotVal) {
tmp = numbers[i];
numbers[i] = numbers[storeIndexEq];
numbers[storeIndexEq] = tmp;
storeIndexEq++;
}
}
tmp = numbers[right];
numbers[right] = numbers[storeIndexEq];
numbers[storeIndexEq] = tmp;
if(k < storeIndex) {
return storeIndex;
}
if(k <= storeIndexEq) {
return k;
}
return storeIndexEq;
}
//Insertion sort
private static int partition5(int[] numbers, int left, int right) {
int i = left + 1;
int j = 0;
while(i<=right) {
j= i;
while(j>left && numbers[j-1] > numbers[j]) {
int tmp = numbers[j-1];
numbers[j-1] = numbers[j];
numbers[j] = tmp;
j=j-1;
}
i++;
}
return left + (right - left) / 2;
}
}
Confirm n (in the pseudo code) or i (in my code) stand for the position of the median? So lets assume our array is number = {9,8,7,6,5,4,3,2,1,0}. I would call select{numbers, 0, 9,4), correct?
I don't understand the calculation of mid in pivot? Why is there a division by 10? Maybe there is a mistake in the pseudo code?
Thanks for your help.
EDIT: It turns out the switch from iteration to recursion was a red herring. The actual issue, identified by the OP, was in the arguments to the 2nd recursive select call.
This line:
return select(numbers, left, pivotIndex + 1, i);
should be
return select(numbers, pivotIndex + 1, right, i);
I'll leave the original answer below as I don't want to appear to be clever than I actually was.
I think you may have misinterpreted the pseudocode for the select method - it uses iteration rather than recursion.
Here's your current implementation:
private static int select(int[] numbers, int left, int right, int i) {
if(left == right) {
return left;
}
int pivotIndex = pivot(numbers, left, right);
pivotIndex = partition(numbers, left, right, pivotIndex, i);
if(i == pivotIndex) {
return i;
}else if(i < pivotIndex) {
return select(numbers, left, pivotIndex - 1, i);
}else {
return select(numbers, left, pivotIndex + 1, i);
}
}
And the pseudocode
function select(list, left, right, n)
loop
if left = right then
return left
pivotIndex := pivot(list, left, right)
pivotIndex := partition(list, left, right, pivotIndex, n)
if n = pivotIndex then
return n
else if n < pivotIndex then
right := pivotIndex - 1
else
left := pivotIndex + 1
This would typically be implemented using a while loop:
private static int select(int[] numbers, int left, int right, int i) {
while(true)
{
if(left == right) {
return left;
}
int pivotIndex = pivot(numbers, left, right);
pivotIndex = partition(numbers, left, right, pivotIndex, i);
if(i == pivotIndex) {
return i;
}else if(i < pivotIndex) {
right = pivotIndex - 1;
}else {
left = pivotIndex + 1;
}
}
}
With this change your code appears to work, though obviously you'll need to test to confirm.
int[] test = {9,8,7,6,5,4,3,2,1,0,13,11,10};
System.out.println("Lower Median: " + lowerMedian(test));
int[] check = test.clone();
Arrays.sort(check);
System.out.println(Arrays.toString(check));
Output:
Lower Median: 6
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 13]
I have a quick sort method which sorts elements in ascenidng order but seem to keep getting a stackoverflowerror.
For some reason it is showing the error on the while loop, when the logic makes sense to me.
Here is the code for the quick sort class:
public T[] sort(T[] arr, int left, int right)
{
int l = left;
int r = right;
if (right <= left)
return null;
//Find the pivot in the middle
T pivot = arr[(left + (right - left)) / 2];
T temp;
while (l <= r)
{
// check values on left are bigger than the pivot
while (arr[l].compareTo(pivot) < 0)
{
l++;
}
// check if values are smaller than the pivot
while (arr[r].compareTo(pivot) > 0)
{
r--;
}
// l and r have gone past each other swap them
if (l <= r)
{
//swap process
temp = arr[l];
arr[l] = arr[r];
arr[r] = temp;
// left pointer goes up 1
// right pointer goes down 1
l++;
r--;
}
}
if (left < r)
sort(arr, left, r);
if (l < right)
sort(arr, l, right);
return arr;
}
The error seems to be pointing to
//Find the pivot in the middle
T pivot = arr[(left + (right - left)) / 2];
I then seem to be getting many occuring errors.
I believe you are calculating Pivot incorrectly
You should use T pivot = arr[left + (right - left) / 2];
Below is working quick sort program using middle element as pivot:
public void quickSort(T arr[],int left, int right){
int low =left, high = right;
int pivot = arr[left + (right - left) / 2];
while(low<=high){
while (arr[low] < pivot) {
low++;
}
while (arr[high] > pivot) {
high--;
}
if (low <= high) {
int temp = arr[low];
arr[low] = arr[high];
arr[high] = temp;
low++;
high--;
}
if (left < high) {
quickSort(arr,left, high);
}
if (low < high) {
quickSort(arr,low, right);
}
}
}
Hope it helps !!
Stream API way:
T[] result = Arrays.stream(a)
.skip(left)
.limit(right - left)
.sorted((o1, o2) -> {{you logic of comparing}})
.toArray(String[]::new);
the difference in what you get only sorted part of an array. So you should concatenate them after if it is necessary.
You have a typo in that line, yes. T pivot = arr[(left + (right - left)) / 2];
Due to the extra parentheses, everything is divided by 2. It should be
T pivot = arr[left + (right - left) / 2];
A couple stylistical remarks:
as this is an in-place sort, returning T[] is not really necessary
T temp could be moved into the swap-block
Putting it together:
import java.util.Arrays;
public class QuickSort<T extends Comparable<? super T>> {
public void sort (T[] arr)
{
if (arr == null || arr.length <= 1)
return;
sort(arr, 0, arr.length - 1);
}
public void sort(T[] arr, int left, int right)
{
int l = left;
int r = right;
if (right <= left)
return;
//Find the pivot in the middle
T pivot = arr[(left + (right - left)/2)];
while (l <= r)
{
// check values on left are bigger than the pivot
while (arr[l].compareTo(pivot) < 0)
{
l++;
}
// check if values are smaller than the pivot
while (arr[r].compareTo(pivot) > 0)
{
r--;
}
// l and r have gone past each other swap them
if (l <= r)
{
//swap process
T temp = arr[l];
arr[l] = arr[r];
arr[r] = temp;
// left pointer goes up 1
// right pointer goes down 1
l++;
r--;
}
}
if (left < r)
sort(arr, left, r);
if (l < right)
sort(arr, l, right);
}
public static void main(String args[])
{
Integer[] numbers=new Integer[] {3,2,5,4,1};
System.out.println(Arrays.asList(numbers));
new QuickSort<Integer>().sort(numbers);
System.out.println(Arrays.asList(numbers));
}
}
Output:
[3, 2, 5, 4, 1]
[1, 2, 3, 4, 5]
I have a quicksort code in java which I want to improve. The improved code should take less time than the quicksort code. However when using my improved code which implement median of 3 partitioning, it takes 400 miliseconds more. Can anybody help me solve this out? if possible, can you suggest me other possible ways to improve my code. I have from 10,000 to 10 million integers to sort.
Quicksort
public void quickSort(int arr[], int begin, int end) {
if (begin < end) {
int partitionIndex = partition(arr, begin, end);
quickSort(arr, begin, partitionIndex-1);
quickSort(arr, partitionIndex+1, end);
}
}
private int partition(int arr[], int begin, int end) {
int pivot = arr[end];
int i = (begin-1);
for (int j = begin; j < end; j++) {
if (arr[j] <= pivot) {
i++;
int swapTemp = arr[i];
arr[i] = arr[j];
arr[j] = swapTemp;
}
}
int swapTemp = arr[i+1];
arr[i+1] = arr[end];
arr[end] = swapTemp;
return i+1;
}
}
Improved code
package sorting;
public class improvement {
Clock c = new Clock();
public void quickSort(int[] intArray) {
recQuickSort(intArray, 0, intArray.length - 1);
}
public static void recQuickSort(int[] intArray, int left, int right) {
int size = right - left + 1;
if (size <= 3)
manualSort(intArray, left, right);
else {
double median = medianOf3(intArray, left, right);
int partition = partitionIt(intArray, left, right, median);
recQuickSort(intArray, left, partition - 1);
recQuickSort(intArray, partition + 1, right);
}
}
public static int medianOf3(int[] intArray, int left, int right) {
int center = (left + right) / 2;
if (intArray[left] > intArray[center])
swap(intArray, left, center);
if (intArray[left] > intArray[right])
swap(intArray, left, right);
if (intArray[center] > intArray[right])
swap(intArray, center, right);
swap(intArray, center, right - 1);
return intArray[right - 1];
}
public static void swap(int[] intArray, int dex1, int dex2) {
int temp = intArray[dex1];
intArray[dex1] = intArray[dex2];
intArray[dex2] = temp;
}
public static int partitionIt(int[] intArray, int left, int right, double
pivot) {
int leftPtr = left;
int rightPtr = right - 1;
while (true) {
while (intArray[++leftPtr] < pivot)
;
while (intArray[--rightPtr] > pivot)
;
if (leftPtr >= rightPtr)
break;
else
swap(intArray, leftPtr, rightPtr);
}
swap(intArray, leftPtr, right - 1);
return leftPtr;
}
public static void manualSort(int[] intArray, int left, int right) {
int size = right - left + 1;
if (size <= 1)
return;
if (size == 2) {
if (intArray[left] > intArray[right])
swap(intArray, left, right);
return;
} else {
if (intArray[left] > intArray[right - 1])
swap(intArray, left, right - 1);
if (intArray[left] > intArray[right])
swap(intArray, left, right);
if (intArray[right - 1] > intArray[right])
swap(intArray, right - 1, right);
}
}
}
I'm implementing a recursive quicksort however I'm receiving stackoverflow and not sure where the bug lies :(
I'm sorting 1 million ints from 10-50.
I works for sizes less than 1 million like 100 thousand etc.
public Quicksort(int NUM_TESTS, int NUM_ELEMENTS){
num_tests = NUM_TESTS;
num_elements = NUM_ELEMENTS;
}
private void start(){
for (int i = 0; i < num_tests; i++){
int[] d1 = dataGeneration(num_elements);
qSortRecursive(d1,0,d1.length-1);
}
}
public static void main(String args[]){
Quicksort q = new Quicksort(1,1000000);
q.start();
}
private int[] dataGeneration(int n) {
int[] d1 = new int[n];
for (int i = 0; i < n; i++){
d1[i] = (int)(Math.random() * ((50 - 10) + 1) + 10);
}
return d1;
}
private void qSortRecursive(int[] data, int left, int right){
if(left < right){
int pivot = partition(data,left,right);
qSortRecursive(data,left,pivot-1);
qSortRecursive(data,pivot+1,right);
}
}
private int partition(int[] data, int left, int right){
int pivot = left ;
left++;
while (left <= right){
while (left <= right && data[left] <= data[pivot]) {
left++;
}
while (left <= right && data[right] >= data[pivot]){
right--;
}
if (left < right){
swap(data,left,right);
left++;
right--;
}
}
if (data[right] <= data[pivot]){
if (data[right] != data[pivot]){
swap(data,right,pivot);
}
pivot = right;
}
return pivot;
}
private void swap(int[] data, int i, int j){
int temp = data[i];
data[i] = data[j];
data[j] = temp;
}
private void qSortRecursive(int[] data, int left, int right){
while (left < right){
int pivot = partition(data,left,right);
if (pivot - left < right - pivot){
qSortRecursive(data, left, pivot - 1);
left = pivot + 1;
} else {
qSortRecursive(data, pivot + 1, right);
right = pivot - 1;
}
}
Performing a tail call by reducing number of recursion solved my problem, thanks for help everyone :)
You can try to rewrite the algorithm without recursion. Well, you remove recursion by adding your own stack and in that case you can have available the entire memory, not just size of stack.
Something like: http://alienryderflex.com/quicksort/
I am trying to implement In-Place Quicksort, with the last element as my pivot. Attached below is my code
public static void main(String[] args){
int[] input = {3,2,4,6,10,1,9,7,5};
quickSort(input, 0, input.length-1);
}
public static void swap(int[] array, int i, int j) {
int tmp = array[i];
array[i] = array[j];
array[j] = tmp;
}
public static int partition(int arr[], int left, int right) {
int pivot = arr[right];
int high = right-1;
while(left < high){
while(arr[left] < pivot){
left++;
}
while(arr[high] > pivot){
high--;
}
swap(arr,left, high);
left++;
high--;
}
swap(arr, left, pivot);
System.out.println(Arrays.toString(arr));
return left;
}
public static void quickSort(int arr[], int left, int right) {
int index = partition(arr, left, right);
quickSort(arr, left, index - 1);
quickSort(arr, index, right);
}
For some reason, my code is giving me an IndexOutOfBounds Exception, and it does not accurately sort the array. I am not sure why I am getting this error.
If I understand correctly, we should make the last element our pivot. Then, we iterate the left pointer right, until we find an element greater than the pivot. After that, we do the same from the right side (keep moving left), until we find an element smaller than the pivot. Then we swap these elements and continue doing the same thing.
Finally, when the left/right pointer are the same, we swap the center value with our pivot.
Is this the correct way of thinking about it? And if so, what errors does my code have? Any help would be appreciated
A few errors:
Add left < high checks to your inner loops. You should check it every time you modify left or right.
Check arr[high] >= pivot not arr[high] > pivot.
swap(arr, left, pivot); is wrong. You should swap left with pivot using positions, not values. It should be swap(arr, left, right);.
You should check left < right in your quicksort method.
When you fix these errors, your code should look like this:
public static void main(String[] args){
int[] input = {3,2,4,6,10,1,9,7,5};
quickSort(input, 0, input.length-1);
}
public static void swap(int[] array, int i, int j) {
int tmp = array[i];
array[i] = array[j];
array[j] = tmp;
}
public static int partition(int arr[], int left, int right) {
int pivot = arr[right];
int high = right;
while(left < high){
while(left < high && arr[left] < pivot){
left++;
}
while(left < high && arr[high] >= pivot){
high--;
}
swap(arr,left, high);
}
swap(arr, left, right);
System.out.println( Arrays.toString(arr));
return left;
}
public static void quickSort(int arr[], int left, int right) {
if( left < right)
{
int index = partition(arr, left, right);
quickSort(arr, left, index - 1);
quickSort(arr, index, right);
}
}
Trace through this code on the assumption that the final array element is the smallest element. The loop to adjust high will continuously decrement high because each other array element is bigger than the pivot, so eventually high will drop off the front of the array, leading to your out of bounds access.
To fix this, add in another check in that loop to ensure you haven't had high and left cross. You may then need to add in some extra logic afterwards to handle that as a special case.