I'm trying to implement a QuickSort algorithm as a homework at the university, but I just fail to understand where there is a mistake in my code. I suppose it's a logical mistake, I think I swap my pivot wrongly. I could really use some help, thank you in advance. There is the code:
public class QuickSort {
private int [] array;
public QuickSort(int [] array){
this.array = array;
}
public void sort(){
partition(0, array.length - 1);
}
public void partition(int start, int end){
if (end - start < 2){
return;
}
int pivot_index = end;
int i = start;
int j = end - 1;
while (i < j){
//both elements are not in the right place
if(array[i] > array[pivot_index] && array[j] <= array[pivot_index]){
swap(array, i, j);
i++;
j--;
}
//the element on the left is not in place
else if (array[i] > array[pivot_index] && array[j] > array[pivot_index]){
j--;
}
//the element on the right is not in place
else if (array[i] < array[pivot_index] && array[j] < array[pivot_index]){
i++;
}
//both elements are in place
else {
i++;
j--;
}
}
if (array[i] > array[pivot_index]){
swap(array, pivot_index, i);
pivot_index = i;
}
partition(start, pivot_index - 1);
partition(pivot_index + 1, end);
}
private static void swap(int [] tab, int index1, int index2){
int temp = tab[index1];
tab[index1] = tab[index2];
tab[index2] = temp;
}
}
Solution one
The idea is iterating through the array to check whether the current element is smaller than the last one (the pivot), if yes swap with the first one that is not (index is lastSmaller + 1).
private void partition(int start, int end) {
if (start >= end) {
return;
}
int lastSmallest = start - 1;
for (int i = start; i < end; i++) {
if (array[i] < array[end])
swap(++lastSmallest, i);
}
swap(++lastSmallest, end);
partition(start, lastSmallest - 1);
partition(lastSmallest + 1, end);
}
Solution two
I think this is what you want to implement. Iterate through the array, skip all the elements in good place on the left and right. Swap the two in wrong place.
private void partition(int start, int end) {
if (end <= start) {
return;
}
int k = end;
int i = start;
int j = end - 1;
while (i < j) {
// left is in place
while (i < j && array[i] < array[k]) {
i++;
}
// right is in place
while (i < j && array[j] >= array[k]) {
j--;
}
// both are not good
if (i < j) {
// swap
int temp = array[i];
array[i] = array[j];
array[j] = temp;
i++;
j--;
}
}
// swap left and pivot
if (array[i] >= array[k]) {
int temp = array[i];
array[i] = array[k];
array[k] = temp;
}
partition(start, i - 1);
partition(i + 1, end);
}
The reason why your solution does not work is that when you find both are not in place, you swap them and continue to partition the rest of the array. However, you can not guarantee what you have swapped does not violate the rule. Therefore, you need to skip all the elements in place on both side first then swap.
Related
I have an assignment to draw a graph and analyze the Hoare's partitioning algorithm and compare theoretical and practical worst-case scenario values. It says that I need a count variable to keep track of critical steps (i.e comparison and swapping). I wrote this code for Hoare's partitioning.
public static int partition(Comparable[] a, int left, int right) {
Comparable pivot = a[(left + right) / 2];
int i = left - 1, j = right + 1;
while (true) {
do {
i++;
} while (a[i].compareTo(pivot) < 0);
do {
j--;
} while (a[j].compareTo(pivot) > 0);
if (i < j) {
Comparable temp = a[i];
a[i] = a[j];
a[j] = temp;
} else {
return j;
}
}
}
Now I am confused about where to add the count variable. I know that count variable will be incremented in if (i < j) line. But do I also need to increment count in both do-while loops to keep track of comparisons?
So, the updated code will be like this?
public static int partition(Comparable[] a, int left, int right) {
int count = 0;
Comparable pivot = a[(left + right) / 2];
int i = left - 1, j = right + 1;
while (true) {
do {
count++;
i++;
} while (a[i].compareTo(pivot) < 0);
do {
count++;
j--;
} while (a[j].compareTo(pivot) > 0);
if (i < j) {
count++;
Comparable temp = a[i];
a[i] = a[j];
a[j] = temp;
} else {
return j;
}
}
}
Or like this?
public static int partition(Comparable[] a, int left, int right) {
int count = 0;
Comparable pivot = a[(left + right) / 2];
int i = left - 1, j = right + 1;
while (true) {
do {
i++;
} while (a[i].compareTo(pivot) < 0);
do {
j--;
} while (a[j].compareTo(pivot) > 0);
if (i < j) {
count++;
Comparable temp = a[i];
a[i] = a[j];
a[j] = temp;
} else {
return j;
}
}
}
I'm trying the implement the MergeSort algorithm in Java so that it takes it sorts an array from A[start..end]. I'm really struggling to implement it so that it doesn't include the last index passed in, in the merge. I'm trying to trace my code but keep getting confused.
Here is my code:
public class MergeSort {
public static void main(String[] args) {
int[] list = new int[] { 3, 7, 5, 2, 9 };
int[] result = mergeSort(list, 0, list.length);
System.out.print("[");
for (int i = 0; i < result.length; i++) {
System.out.print(" " + result[i]);
}
System.out.println("]");
}
public static int[] mergeSort(int[] list, int start, int end) {
if (end - start < 2) {
return list;
} else {
int mid = (start + end) / 2;
mergeSort(list, start, mid);
mergeSort(list, mid + 1, end);
merge(list, start, mid, end);
return list;
}
}
public static void merge(int[] list, int start, int mid, int end) {
int[] copy = new int[list.length];
for (int i = 0; i < list.length; i++) {
copy[i] = list[i];
}
int i = start;
int k = start;
int j = mid + 1;
while (i <= mid && j <= end) {
if (copy[i] <= copy[j]) {
list[k] = copy[i];
i++;
} else {
list[k] = copy[j];
j++;
}
k++;
}
while (i <= mid) {
list[k] = copy[i];
i++;
k++;
}
while (j < end) {
list[k] = copy[j];
j++;
k++;
}
}
}
Calling mergesort with a slice defined with start included and end excluded is indeed a sensible approach as the calling sequence is simpler: merge(array, 0, array.length) and it allows for empty slices, which is necessary for empty arrays.
Your mergesort method has a bug: the right slice starts at mid and ends before end, hence the call should be mergeSort(list, mid, end);
There are problems in the merge method too:
you should not duplicate the whole list, but just the slice from start to end (excluded). It is simpler if you merge into the temporary array and copy it back after merging. With this approach, you can stop the merge when the left part is exhausted as the remaining values from the right part are already in the proper place.
you should use the < operator instead of <= when comparing the running index values to the upper boundaries that are excluded with this approach.
Here is a corrected version:
public class MergeSort {
public static void main(String[] args) {
int[] list = new int[] { 3, 7, 5, 2, 9 };
int[] result = mergeSort(list, 0, list.length);
System.out.print("[");
for (int i = 0; i < result.length; i++) {
System.out.print(" " + result[i]);
}
System.out.println(" ]");
}
public static int[] mergeSort(int[] list, int start, int end) {
if (end - start < 2) {
return list;
} else {
// compute the mid point:
// the left part spans from start included to mid excluded
// the right part spans from mid included to end excluded
// avoid adding start and end to prevent overflow overflow for very large arrays
int mid = start + (end - start) / 2;
mergeSort(list, start, mid);
mergeSort(list, mid, end);
merge(list, start, mid, end);
return list;
}
}
public static void merge(int[] list, int start, int mid, int end) {
int[] temp = new int[end - start];
int k = 0; // index into the temporary array
int i = start; // index into the left part, stop at mid
int j = mid; // index into the right part, stop at end
// select from left or right slices and store into the temp array
while (i < mid && j < end) {
if (list[i] <= list[j]) {
temp[k++] = list[i++];
} else {
temp[k++] = list[j++];
}
}
// copy the remaining elements from the left part
while (i < mid) {
temp[k++] = list[i++];
}
// copy the sorted elements back to the original list
for (i = 0; i < k; i++) {
list[start + i] = temp[i];
}
}
}
Either call mergeSort(list, 0, list.length - 1);
Or call a helper function let's say F. So for example you would call F(list, 0, list.length); Where the only thing F would do is it would call mergeSort(list, 0, list.length - 1);.
That way you don't even touch mergeSort anymore. You just call F.
Edit:
public class MergeSort {
public static void main(String[] args) {
int[] list = new int[] {3, 7, 5, 2, 9};
int[] result = mergeSort(list, 0, list.length);
System.out.print("[");
for (int i = 0; i < result.length; i++) {
System.out.print(" " + result[i]);
}
System.out.println("]");
}
public static int[] mergeSort(int[] list, int start, int end) {
return F(list, start, end - 1);
}
public static int[] F(int[] list, int start, int end) {
if (end - start < 2) {
return list;
} else {
int mid = (start + end) / 2;
F(list, start, mid);
F(list, mid + 1, end);
merge(list, start, mid, end);
return list;
}
}
public static void merge(int[] list, int start, int mid, int end) {
int[] copy = new int[list.length];
for (int i = 0; i < list.length; i++) {
copy[i] = list[i];
}
int i = start;
int k = start;
int j = mid + 1;
while (i <= mid && j <= end) {
if (copy[i] <= copy[j]) {
list[k] = copy[i];
i++;
} else {
list[k] = copy[j];
j++;
}
k++;
}
while (i <= mid) {
list[k] = copy[i];
i++;
k++;
}
while (j < end) {
list[k] = copy[j];
j++;
k++;
}
}
}
I need to count the number of loops and comparisons which occur over four different sorting methods. I am using the Selection, Bubble, Insertion, and Quick sort methods. Ideally, I would just place a int such as loopCounter and ++ it every time it loops/compares. Although, being quite new to all of this, I am having trouble distinguishing when I need to include such counters. As you will be able to see in the following code, I attempted to create multiple counters. Although, I think only the selection counters are correct so far.
Additionally, I am required to count the number of times that a value is shifted. In other words, how many times integers are swapped.
Any help with this would be extremely appreciated!
Thanks
ArrayList<Integer> list = new ArrayList<Integer>();
//Counters for Selection Sort
int loopCounter = 0;
int compCounter = 0;
//Counters for Bubble Sort
int loopCounter2 = 0;
int compCounter2 = 0;
//Counters for Insertion Sort
int loopCounter3 = 0;
int compCounter3 = 0;
//Counters for Quick Sort
int loopCounter4 = 0;
int compCounter4 = 0;
public void selectionSort(Integer[] a) {
for(int i = 0; i < a.length; i++) {
int smallestValue = a[i];
int smallestIndex = i;
if(ascButton.isSelected()){
for(int j = i+1; j < a.length; j++) {
if (smallestValue > a[j]) {
smallestValue = a[j];
smallestIndex = j;
loopCounter++;
compCounter++;
}
}
a[smallestIndex] = a[i];
a[i] = smallestValue;
} else if(desButton.isSelected()){
for(int j = i+1; j < a.length; j++) {
if (smallestValue < a[j]) {
smallestValue = a[j];
smallestIndex = j;
loopCounter++;
compCounter++;
}
}
a[smallestIndex] = a[i];
a[i] = smallestValue;
}
}
}
public void bubbleSort(Integer[] a) {
int temp;
for (int i = a.length - 1; i > 0; i--) {
if(ascButton.isSelected()) {
for(int j = 0; j < i; j++) {
loopCounter2++;
compCounter2++;
if(a[j] > a[j + 1]) {
temp = a[j];
a[j] = a[j + 1];
a[j + 1] = temp;
}
}
} else if(desButton.isSelected()) {
for(int j = 0; j < i; j++) {
loopCounter2++;
compCounter2++;
if(a[j] < a[j + 1]) {
temp = a[j];
a[j] = a[j + 1];
a[j + 1] = temp;
}
}
}
}
}
public void insertionSort(Integer[] a) {
for(int i = 1; i < a.length; i++) {
loopCounter3++;
compCounter3++;
int temp = a[i];
int j = i - 1;
if(ascButton.isSelected()) {
while(j >= 0 && a[j] > temp) {
a[j + 1] = a[j];
j--;
}
a[j + 1] = temp;
} else if(desButton.isSelected()) {
while(j >= 0 && a[j] < temp) {
a[j + 1] = a[j];
j--;
}
a[j + 1] = temp;
}
}
}
public void quickSort(Integer[] a, int left, int right) {
int i = left;
int j = right;
int temp;
int pivot = a[(left + right)/2];
while(i <= j) {
if(ascButton.isSelected()) {
while(a[i] < pivot)
i++;
while(a[j] > pivot)
j--;
} else if(desButton.isSelected()) {
while(a[i] > pivot)
i++;
while(a[j] < pivot)
j--;
}
if(i <= j) {
temp = a[i];
a[i] = a[j];
a[j] = temp;
i++;
j--;
}
}
if(left < j) {
quickSort(a,left,j);
}
if(i < right) {
quickSort(a, i, right);
}
}
With counters, you simply want to "count" when you want to program to count something. So if you don't understand your own code, then it will be difficult to know when you want to "count" something. I suggest you figure out when a swap is happening, when that is happening in your code, that is when you want to do some sort of:
swapCount++;//Each time a swap happens increment by 1
iterationCount++//A full pass has happened increment by 1
Note: above just because a full pass has happened in many sort, which you probably know, does not mean it is sorted, it is just saying it has done 1 pass.
I'm not sure if this theory will help you any. Give me some feedback on what your still having trouble with and I'll see if I can change my answer to better reflect what your looking for.
As #Andreas suggested, your loop and comparison counters are in place correctly.
As far as the swap counter is concerned, think of it this way - you cannot swap without a temp variable. As a result, whenever a temp variable is involved you want to increase your swap counter.
As an example, for your quicksort, it would look like this:
public void quickSort(Integer[] a, int left, int right) {
int i = left;
int j = right;
int temp;
int pivot = a[(left + right)/2];
while(i <= j) {
if(ascButton.isSelected()) {
while(a[i] < pivot)
i++;
while(a[j] > pivot)
j--;
} else if(desButton.isSelected()) {
while(a[i] > pivot)
i++;
while(a[j] < pivot)
j--;
}
if(i <= j) {
temp = a[i];
a[i] = a[j];
a[j] = temp;
i++;
j--;
swapCounterForQuickSort++;
}
}
if(left < j) {
quickSort(a,left,j);
}
if(i < right) {
quickSort(a, i, right);
}
}
Follow the same logic for your other sorts.
Also, some general suggestions:
Always name variables so that they tell you what they are used for. Rather than loopCounter1 try using loopCounterForSelectionSort and so on. Don't be afraid of long variable names. Information is power!
Make your functions as short and reusable as possible. For example, you swap integers a lot in your code. Maybe you can just copy the swap code and paste it into a swapIntegers() function. Then everytime you just call this function when you want to swap! Also, notice how this makes your swap counter question easier to answer since you can put a counter in the swap method to do the counting for you. (Although be aware since multiple methods will call the swap counter so you may want to pass it as an argument etc.)
Can someone show me how to handle duplicates in QuickSort with my function here?
private static int[] quickSort(int[] input, int left, int right) {
int mid = (left + right) / 2;
int i = left;
int j = right;
if((right - left) < 1) {
return new int[]{};
}
while(i <= j) {
while((input[i] < input[mid])) {
i++;
}
while((input[j] > input[mid])) {
j--;
}
if(i <= j) {
int temp = input[i];
input[i] = input[j];
input[j] = temp;
i++;
j--;
}
}
if(j > left) {
input = quickSort(input, left, j);
}
if(i < right) {
input = quickSort(input, i, right);
}
return input;
}
The code has several problems.
"return new int[]{};" This will generate new arrays and consumes memory unnecessarily. You may return "input".
For the first inner loops, you may want to have "input[i] <= input[mid] && i < mid". This will handle duplicates.
There are some well-tested code here: Quick Sort
I've been trying to do a project using quick sort and bottom-up merge sort but I am stuck on quick sort. I've been able to come up with some code but whenever I try to run my program I get the error "quickSort(int[],int,int) in sort cannot be applied to (int[])" Any tips?
Here's code...
import java.util.Random;
public class main {
public static void main(String[] args) {
Random gen = new Random();
int[] a = new int[20];
for (int i = 0; i < a.length; i++)
a[i] = gen.nextInt(100);
printArray(a);
quickSort(a);
}
private static void printArray(int[] a){
for (int i : a)
System.out.print(i + " ");
System.out.println("");
}
private static void quickSort(int a[], int left, int right){
int i = left, j = right;
int tmp;
int pivot = a[(left + right) / 2];
while (i <= j) {
while (a[i] < pivot)
i++;
while (a[j] > pivot)
j--;
if (i <= j) {
tmp = a[i];
a[i] = a[j];
a[j] = tmp;
i++;
j--;
}
}
if (left < j)
quickSort(a, left, j);
if (i < right)
quickSort(a, i, right);
}
Your quickSort function takes three arguments, but you are calling it with only one.
your first call to quicksort doesn't have the right syntax. The method takes 3 parameters.
quickSort(a);
I think you want
quickSort(a, 0, 19); //might be 20 I don't know if it's size or last element index