I have a pseudo code of a Quicksort in my book, that I am following step-by-step. But the end output is not what I want. I have turned it into this code:
public class quickSort {
public int[] quick(int[] A, int p, int r){
int q;
if(p<r){
q = partition(A, p, r);
//First partition comfirmed to "work"
quick(A, p, q-1);
quick(A, q+1, r);
}
return A;
}
public int partition(int[] A, int p, int r){
int x = A[r];
int i = p-1;
int temp;
for(int j=p; j<r-1; j++){
if(A[j]<=x){
i = i+1;
temp = A[i];
A[i] = A[j];
A[j] = temp;
}
}
temp = A[i+1];
A[i+1] = A[r];
A[r] = temp;
return i+1;
}
}
My book illustrates how the first partition is being handled. With the input:
2 8 7 1 3 5 6 4
.. the first partition "sorts" this into
2 1 3 4 7 5 6 8
.. and this I have comfirmed. And so, if this work, and it is just calling itself with smaller parts to do the exact same thing, why is it in the end giving the output:
2 3 1 4 5 7 8 7
.. and not something that is sorted???
The error is that only elements [p, r-2] are going to be processed in public int partition() instead of [p, r-1]. A[r-1] might happen to be less than pivot, but it is not swapped and stays in its place
Alright so here's your answer tweaked and it works:
import java.util.Arrays;
public class QuickSort
{
public QuickSort()
{
int array[] = { 2, 8, 7, 1, 3, 5, 6, 4 };
quickSort(array, 0, array.length - 1);
System.out.println(Arrays.toString(array));
}
void quickSort(int[] array, int p, int r)
{
if (p < r)
{
int q = partition(array, p, r);
quickSort(array, p, q - 1);
quickSort(array, q + 1, r);
}
}
int partition(int array[], int p, int r)
{
int x = array[r];
int i = p - 1;
for (int j = p; j < r; j++)
{
if (array[j] <= x)
{
i += 1;
int temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
int temp = array[i + 1];
array[i + 1] = array[r];
array[r] = temp;
return i + 1;
}
public static void main(String[] args)
{
new QuickSort();
}
}
But by now I'm sure you got to know what went wrong. I had to keep my word though and that's why I'm updating my answer here.
it should be for(int j=p; j<=r-1; j++)
public int partition(int[] A, int p, int r){
int x = A[r];
int i = p-1;
int temp;
for(int j=p; j<=r-1; j++){
if(A[j]<=x){
i = i+1;
temp = A[i];
A[i] = A[j];
A[j] = temp;
}
}
temp = A[i+1];
A[i+1] = A[r];
A[r] = temp;
return i+1;
}
Related
I was trying to implement in Java the merge sort algorithm according to Cormen's Introduction to Algorithms. The problem with my code (below) is that the main array is duplicating some of its entries during the merge step.
Is someone able to catch what I'm doing wrong?
Thank you!
static void merge(int a[], int p, int q, int r)
{
int n1 = q - p;
int n2 = (r - q);
int [] left = new int[n1 + 1];
int [] right = new int[n2 + 1];
int pp = p;
int qq = q;
for(int i = 0; i < n1; i++)
{
left[i] = a[++pp];
}
for(int i = 0; i < n2; i++)
{
right[i] = a[++qq];
}
left[left.length-1] = Integer.MAX_VALUE;
right[right.length-1] = Integer.MAX_VALUE;
int i = 0;
int j = 0;
for(int k = p; k < r; k++)
{
if(left[i] <= right[j])
{
a[k] = left[i];
i++;
}
else
{
a[k] = right[j];
j++;
}
}
}
static int [] mergeSort(int a[], int p, int r)
{
if(p < r)
{
int q = (p + r)/2;
mergeSort(a, 1, q);
mergeSort(a, q + 1, r);
merge(a, p, q, r);
}
return a;
}
Part of the issue here is the example from the book apparently uses index range from 1 to length. It will be simpler if you change the index range from 0 to length-1, which I assume in the rest of my answer.
Use post increment while copying to left[] and right[] as answered by laune (since index range 0 to length-1).
left[i] = a[pp++];
...
right[i] = a[qq++];
The main issue is the merge function is not checking to see if it reached the end of the left or right run during a merge. This can be fixed by changing the inner if to:
if (i < n1 && (j >= n2 || left[i] <= right[j]))
The recursive calls to merge sort should be:
mergeSort(a, p, q);
mergeSort(a, q, r);
Not shown, but the initial call to mergeSort should be:
mergeSort(a, 0, a.length);
There's no need to allocate the extra element in left and right (since index range is 0 to length-1).
int [] left = new int[n1];
int [] right = new int[n2];
I think that this is in error (as well as its sibling in the next loop):
left[i] = a[++pp];
You want to copy starting with pp = p, so don't increment before you access the array element:
left[i] = a[pp++];
I tried to convert this Merge Sort pseudocode into Java but don't get the right output. Here is the pseudocode:
Merge-Sort(A, p, r )
if p < r
then q←(p+r)/2
Merge-Sort(A, p, q)
Merge-Sort(A, q + 1, r )
Merge(A, p, q, r )
Merge(A, p, q, r )
for k←1 to r−p+1 do
if j>r or (i ≤ q and A[i] ≤ A[j])
then B[k]←A[i]; i←i+1 else B[k]←A[j];j←j+1
for k←1 to r−p+1 do A[k+p−1]←B[k]
And this is my Java code for it:
public class MergeSort {
public static void main(String[] args) {
int[] a = {2, 6, 3, 5, 1};
mergeSort(a, 0, a.length - 1);
for (int i = 0; i < a.length; i++) {
System.out.print(" " + a[i]);
}
}
public static void mergeSort(int[] a, int from, int to) {
final int begin = from, end = to;
if (begin < end) {
final int mid = (begin + end) / 2;
MergeSort.mergeSort(a, begin, mid);
MergeSort.mergeSort(a, mid+1, end);
MergeSort.merge(a, begin, mid, end);
}
}
private static void merge(int[] a, int from, int mid, int to) {
final int begin = from, mitte = mid, end = to;
int[] B = new int[a.length];
int i = begin, j = mitte;
for (int k = 0; k <= end-begin; k++) {
if (j > end || (i <= mitte && a[i] <= a[j])) {
B[k] = a[i];
i++;
} else {
B[k] = a[j];
j++;
}
}
for (int k = 0; k < end-begin; k++) {
a[k + begin] = B[k];
}
}
Sadly it is not working like that. I think i do something wrong with some indexes but I can't figure out where exactly the error is.
I need to stick as close as possible to this pseudocode.
It would be great if someone could show me what I am doing wrong.
The pseudocode given for the Merge algorithm is somewhat incorrect because it does not say anything about the situation when only one pointer moves while other remains stationary.
In the above mentioned case you would have to separately fill out temporary array for by moving that stationary pointer.
Also the required length of B is to - from + 1 and it should be j = mitte + 1 instead of j = mitte The correct code for the merge is :
private static void merge(int[] a, int from, int mid, int to) {
final int begin = from, mitte = mid, end = to;
int[] B = new int[end-begin+1];
int k=0;
int i = begin, j = mitte+1;
while(i<=mid&&j<=end)
if(a[i]<=a[j]){
B[k++] = a[i];
i++;
} else {
B[k++] = a[j];
j++;
}
//in case i remained stationary
while(i<=mid)
B[k++] = a[i++];
//in case j remained stationary
while(j<=end)
B[k++] = a[j++];
//Now copy the array
i=0;
for(k=begin;k<=end;++k)
a[k]=B[i++];
}
I am trying to implement quick sort using Java.The partition function does what it should do.That is,partition the array around the pivot(I've chosen the element as the pivot). But the final output is not in sorted order.I cannot figure the error out.Can someone help?
public class Quick_sort {
public static int arr[] = {11, 2, 7, 1, 5, 4, 12, 65, 23};
public static int temp = 0;
public static void main(String args[]) {
int p=0;
int r=arr.length;
quick_sort(p,r);
for(int i: arr)
System.out.println(i);
}
public static int partition(int p, int r) {
if(p < r) {
int pivot=arr[p];
int i=1;
for(int j=1;j<r;j++) {
if(arr[j]<pivot) {
temp=arr[j];
arr[j]=arr[i];
arr[i]=temp;
i++;
}
}
temp=arr[i-1];
arr[i-1]=arr[p];
arr[p]=temp;
for(int m=0;m<r;m++) {
if(arr[m]==pivot) {
temp=m;
}
}
}
return temp;
}
public static void quick_sort(int p,int r) {
if(p>=r) return;
int index=partition(p,r);
quick_sort(p,index-1);
quick_sort(index+1,r-1);
}
}
In your last line
quick_sort(index+1,r-1);
You skip the last element of the array. But the last element should be sorted as well. Try it with:
quick_sort(index+1,r);
And it is better to adapt the variables i and j in the partition method to the current processed part of the array.
So I tried to fix it. Try it with (main function):
int r=arr.length-1;
and change the partition function to:
public static int partition(int p, int r) {
if(p < r) {
int pivot=arr[p];
int i= p ;
for(int j=(p+1);j<=r;j++) {
if(arr[j]<pivot) {
temp=arr[j];
arr[j]=arr[i + 1];
arr[i + 1] = arr[i];
arr[i] = temp;
i++;
}
}
temp = i;
}
return temp;
}
as well as the in the quick-sort method:
quick_sort(p,index-1);
quick_sort(index+1,r);
Do you see your problem? Your main problem was to not adapt the variables to the smaller parts you are actually looking at at the moment. It did well for the first partition round, but not for the following, as you had the former variables.
This is a complete example of a QuickSort implementation :
public class QuickSort {
public static void main(String[] args) {
int[] x = { 9, 2, 4, 7, 3, 7, 10 };
System.out.println(Arrays.toString(x));
int low = 0;
int high = x.length - 1;
quickSort(x, low, high);
System.out.println(Arrays.toString(x));
}
public static void quickSort(int[] arr, int low, int high) {
if (arr == null || arr.length == 0)
return;
if (low >= high)
return;
// pick the pivot
int middle = low + (high - low) / 2;
int pivot = arr[middle];
// make left < pivot and right > pivot
int i = low, j = high;
while (i <= j) {
while (arr[i] < pivot) {
i++;
}
while (arr[j] > pivot) {
j--;
}
if (i <= j) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
i++;
j--;
}
}
// recursively sort two sub parts
if (low < j)
quickSort(arr, low, j);
if (high > i)
quickSort(arr, i, high);
}
}
you can find more here.
I'm trying to implement this:
In pseudo things start with 1. In my code things start with 0. Other than that there is no difference.
I'm getting incorrect result:
arr content:
11 2 4 1 7 5
New arr content:
1 2 4 7 11 5
What am I doing wrong?
public static void hQuickSort(int[] A, int p, int r) {
if (p < r) {
int q = hPartition(A, p, r);
hQuickSort(A, p, q);
hQuickSort(A, q + 1, r);
}
}
public static int hPartition(int[] A, int p, int r) {
int pivot = A[p];
int i = p;
int j = r;
int temp;
while (true) {
while (A[i] < pivot) {
i++;
}
while (A[j] > pivot) {
j--;
}
if (i < j) {
temp = A[i];
A[i] = A[j];
A[j] = temp;
} else {
return j;
}
}
}
invoke with an array of 6 elements:
hQuickSort(arr, 0, 5);
I am working on a quicksort from my data structures and algorithms book. In the book it lists a quicksort method then a hoare partition that it wants you to use with the quick sort. I seem to be having an issue where my hoare partition is using out of bounds numbers on the array. Either it uses 8 or if I try to fix that it goes to -1. Am I converting the books pseudo correctly into java?
Quicksort pseudo code
QuickSort(A, p, r)
if p<r
q = partition(A, p, r);
QuickSort(A, p, q - 1);
QuickSort(A, q, r);
Hoare-Partition Pseudo Code
Hoare-Partition(A,p,r)
x= A[p]
i = p-1
j=r+1
while true
repeat
j=j-1
until A [j] <= x
repeat
i = i +1
until A[i] >= x
if i < l
exchange A[i] with A[j]
else return j
My code
public class RunSort {
/**
* #param args
*/
public static void main(String[] args) {
int[] sortNumbers = {4,5,6,2,3,7,2,1};
int[] sorted = new int[sortNumbers.length];
sorted = QuickSort(sortNumbers, 1, sortNumbers.length);
System.out.print(sorted);
}
public static int[] QuickSort(int[] A, int p, int r){
if(p < r){
int q = partition(A, p, r);
QuickSort(A, p, q - 1);
QuickSort(A, q, r);
}
return A;
}
public static int partition(int[] A, int p, int r){
int x = A[p];
int i = p - 1;
int j = r + 1;
int temp;
while(true){
while(A[j] <= x && j != 0){
j--;
}
while(A[i] >= x && i != A.length){
i++;
}
if(i < j){
temp = A[i];
A[i] = A[j];
A[j] = temp;
}else{
return j;
}
}
}
}
Hint: repeat {...} until (condition) does not do the same thing as while (condition) {...}.
Depending on the text, pseudocode often uses 1..arrayLength as the index bounds on an array, but in Java, et al., it's 0..arrayLength-1. You'll need to adjust the arguments to the main QuickSort call in main.
(As a nitpick, QuickSort should start with a lowercase letter by convention.)