I have a task in the university where I need to sort student by their groupnumbers using Median-of-3 quick sort. But my sorting methods are not working properly - only some of the elements are sorted. But a few elements do not change their positions.
I have created a Student class with necessary data, Sortings class with Student type array. And I have 4 static methods for this type of sorting in main class, where I create an instance of Sortings and call my static methods for sorting.
public static void manual_Sort(Student[]st, int right, int left)
{
int size = right - left + 1;
if(size <= 1)
return;
if(size == 2)
{
if(st[left].Group_number > st[right].Group_number)
swap(st,right,left);
return;
}
else
{
if(st[left].Group_number > st[right-1].Group_number)
swap(st,left, right-1);
if(st[left].Group_number > st[right].Group_number)
swap(st,left, right);
if(st[right-1].Group_number > st[right].Group_number )
swap(st,right-1, right);
}
}
public static int partitionIt(Student[]st, int left, int right,double pivot)
{
int leftPtr = left;
int rightPtr = right - 1;
while(true)
{
while(st[++leftPtr].Group_number < pivot)
;
while(st[--rightPtr].Group_number > pivot)
;
if(leftPtr >= rightPtr)
break;
else
swap(st,leftPtr, rightPtr);
}
swap(st,leftPtr, right-1);
return leftPtr;
}
public static int median_OfThree(Student[]st, int left, int right)
{
int center = (left + right)/2;
if(st[left].Group_number > st[center].Group_number )
swap(st,left, center);
if(st[left].Group_number > st[right].Group_number )
swap(st,left, right);
if(st[center].Group_number > st[right].Group_number )
swap(st,center, right);
swap(st,center, right-1);
return st[right-1].Group_number;
}
public static void quick_Sort(Student[] st)
{
rec_quickSort(st, 0, st.length-1);
}
public static void rec_quickSort(Student[]st, int left, int right)
{
int size_ = right - left + 1;
if(size_ <= 3)
manual_Sort(st, left, right);
else
{
double median = median_OfThree(st, left, right);
int partition = partitionIt(st, left, right, median);
rec_quickSort(st, left, partition - 1);
rec_quickSort(st, partition + 1, right);
}
}
Related
I going to do searching the value in the array, did I need to create a method to handle it? For example, the array logged 32,21,13,44,22, and I going to find 22 of the comparison. How can I implement this?
public class binarySearch {
public static void main(String [] args) {
int i = binarySearch(0, new int[]{32,21,13,44,22});
System.out.println("Iterations: " + i);
}
public static int binarySearch(int key, int[] array) {
int left = 0;
int mid;
int right = array.length - 1;
int i = 0;
while (left <= right) {
mid = (left + right) / 2;
int comp = Integer.compare(key, array[mid]);
i++;
if (comp < 0) {
right = mid - 1;
} else if (comp > 0) {
left = mid + 1;
} else {
break; // success
}
}
return i;
}
}
My final answer is here. May help you all in the future.
public static int binarySearch(int key, int[] array) {
int left = 0;
int mid;
int right = array.length - 1;
int i = 0;
while (left <= right) {
mid = (left + right) / 2;
int comp = Integer.compare(key, array[mid]);
i++;
if (comp < 0) {
right = mid - 1;
} else if (comp > 0) {
left = mid + 1;
} else {
break; // success
}
}
return i;
}
If you have shuffled array, all you can do is go through an array and find your number.
BinarySearch works only with sorted array. I think your solution could look like this:
public static int binarySearch(int[] arr, int key) {
Arrays.sort(arr);
return Arrays.binarySearch(arr, key);
}
So i have this code to quick sort a list of students by grade.
public static void quickSort(Student[] school){
quickSort(school, 0, school.length - 1); // quicksort all the elements in the array
}
private static void quickSort(Student[] school, int start, int end) {
int i = start; // index of left-to-right scan
int k = end; // index of right-to-left scan
if (end - start >= 1) // check that there are at least two elements to sort
{
Student pivot = school[start]; // set the pivot as the first element in the partition
while (k > i) // while the scan indices from left and right have not met,
{
while (school[i].getStudentGrade() <= pivot.getStudentGrade() && i <= end && k > i) // from the left, look for the first
{
i++;
// element greater than the pivot
}
while (school[k].getStudentGrade() > pivot.getStudentGrade() && k >= start && k >= i) // from the right, look for the first
{
k--; // element not greater than the pivot
}
if (k > i) // if the left seekindex is still smaller than
{
swap(school, i, k); // the right index, swap the corresponding elements
}
}
swap(school, start, k); // after the indices have crossed, swap the last element in
// the left partition with the pivot
quickSort(school, start, k - 1); // quicksort the left partition
quickSort(school, k + 1, end); // quicksort the right partition
} else // if there is only one element in the partition, do not do any sorting
{
return; // the array is sorted, so exit
}
}
//Swap 2 index values in array
private static void swap(Student[] school, int index1, int index2)
{
Student temp = school[index1];
school[index1] = school[index2];
school[index2] = temp;
}
I only can't figure out how to add an extra sort criteria so students with the same grade are sorted based on there student number which i get by using student.getStudentNumber.
Instead of directly using a compare function, pass a java.util.Comparator<T>.
This way you can implement different Comparators for different sort criteria.
The comparator would look like this:
import java.util.Comparator;
public class StudentComparator implements Comparator<Student> {
#Override
public int compare(final Student s1, final Student s2) {
final int gradeDiff = s1.getStudentGrade() - s2.getStudentGrade();
if (0 != gradeDiff) {
return gradeDiff;
}
final int numberDiff = s1.getStudentNumber() - s2.getStudentNumber();
if (0 != numberDiff) {
return numberDiff;
}
// addd mor criteria here if wanted
return 0;
}
}
Used this now
public class QuickSort {
public static void quickSort(Student[] school) {
quickSort(school, 0, school.length - 1);
}
private static void quickSort(Student[] school, int start, int end) {
int pivotIndex = start;
int storeIndex = pivotIndex + 1;
if (end - start >= 1) {
for (int i = pivotIndex + 1; i <= end; i++) {
if (school[i].getStudentGrade() > school[pivotIndex].getStudentGrade()) {
swap(school, storeIndex, i);
storeIndex++;
} else if(school[i].getStudentGrade() == school[pivotIndex].getStudentGrade()){
if(school[i].getStudentNumber() > school[pivotIndex].getStudentNumber()){
swap(school, storeIndex, i);
storeIndex ++;
}
}
}
swap(school, pivotIndex, storeIndex - 1);
pivotIndex = storeIndex - 1;
quickSort(school, start, pivotIndex - 1);
quickSort(school, pivotIndex + 1, end);
} else {
return;
}
}
//Swap 2 index values in array
private static void swap(Student[] school, int index1, int index2) {
Student temp = school[index1];
school[index1] = school[index2];
school[index2] = temp;
}
}
I'm trying to practice quick sort list using java, I think this is correct but it shows "Exception in thread main..."
public class QuickSort {
public static void sort(List<Integer> list) {
sort(list, 0, list.size() - 1);
}
public static void sort(List<Integer> list, int from, int to) {
if (list.size() <= 1) {
return;
}
int pivot = from;
int left = from + 1;
int right = to;
while (left < right) {
while (list.get(pivot) >= list.get(left)) {
left++;
}
while (list.get(pivot) <= list.get(right)) {
right--;
}
if (left < right) {
Collections.swap(list, left, right);
}
}
Collections.swap(list, pivot, left - 1);
sort(list, from, pivot - 1);
sort(list, pivot + 1, to);
}
}
Your problem is here:
while (list.get(pivot) <= list.get(right)) {
right--;
}
As, In the worst case, say an already sorted list, your right-- will be negative.
Take a look the following code, this will give you idea:
public static void quicksort(List<Integer> list, int left, int right) {
int q;
if (right > left) {
q = partition(list, left, right);
// after ‘partition’
// list[left..q-1] ≤ list[q] ≤ list[q+1..right]
quicksort(list, left, q - 1);
quicksort(list, q + 1, right);
}
}
static int partition(List<Integer> list, int left, int right) {
int P = list.get(left);
int i = left;
int j = right + 1;
for (;;) { // infinite for-loop, break to exit
while (list.get(++i) < P)
if (i >= right)
break;
// Now, list[i]≥P
while (list.get(--j) > P)
if (j <= left)
break;
// Now, list[j]≤P
if (i >= j)
break; // break the for-loop
else
// swap(list[i],list[j]);
Collections.swap(list, i, j);
}
if (j == left)
return j;
// swap (list[left],list[j]);
Collections.swap(list, left, j);
return j;
}
See a demo run here.
Try with this:
public static void sort(List<Integer> list) {
sort(list, 0, list.size() - 1);
}
public static void sort(List<Integer> list, int from, int to) {
if (from < to) {
int pivot = from;
int left = from + 1;
int right = to;
int pivotValue = list.get(pivot);
while (left <= right) {
// left <= to -> limit protection
while (left <= to && pivotValue >= list.get(left)) {
left++;
}
// right > from -> limit protection
while (right > from && pivotValue < list.get(right)) {
right--;
}
if (left < right) {
Collections.swap(list, left, right);
}
}
Collections.swap(list, pivot, left - 1);
sort(list, from, right - 1); // <-- pivot was wrong!
sort(list, right + 1, to); // <-- pivot was wrong!
}
}
I'm implementing a select-kth algorithm using the Median of Medians pivot method. Specifically, I'm following the pseudocode listed here.. However, my code crashes (error discussed below), I see why it crashes, but I don't understand what I can do about it.
The Error
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 32017219
at Selection.partition(Selection.java:173)
The Reason
In the wiki link, the method select will return a value if left == right. However, when the mutual recursion is called from the return statement of pivot, that means it will return a value (and not an index) back to the parent and the parent will set that value as the pivotIndex.
The Code
public static int alg5(int[] a, int left, int right, int k) {
if (left == right) {
return a[left];
}
while (true) {
int pivotIndex = pivot(a, left, right);
pivotIndex = partition(a, left, right, pivotIndex);
if (k == pivotIndex) {
return a[k];
} else if (k < pivotIndex) {
right = pivotIndex - 1;
} else {
left = pivotIndex + 1;
}
}
}
public static int pivot(int[] a, int left, int right) {
for (int i = left; i <= right; i = i + 5) {
int subRight = i + 4;
if (subRight > right) {
subRight = right;
}
int median5 = partition5(a, i, subRight);
swap(a, median5, (int)(Math.floor(i / 5)));
}
return alg5(a, left, (int)(left + Math.ceil((right - left) / 5) - 1), ((right - left) / 10));
}
public static int partition5(int[] a, int left, int right) {
Arrays.sort(a);
return ((a.length - 1) / 2);
}
public static int partition(int[] a, int left, int right, int pivotIndex) {
int pivotValue = a[pivotIndex];
a = swap(a, pivotIndex, right);
int storeIndex = left;
for (int i = left; i <= right; i++) {
int aOfi = a[i];
if (a[i] < pivotValue) {
swap(a, storeIndex, i);
storeIndex++;
}
}
swap(a, right, storeIndex);
return storeIndex;
}
I completely understand why my code isn't working, I just don't understand how to fix it since it looks to be exactly what the algorithm specifies. Pointers are greatly appreciated!
There are quite a few mistakes:
The method pivot should not modify the array a. It should just find a pivot for the future partition. A dirty fix is to call pivot(a.clone(), left, right);. (You shouldn't do this, just to give you an idea.)
Math.ceil((right - left) / 5) is an integer division. You should cast them to floats: Math.ceil(((float)(right - left)) / 5f).
In partition5, you sort the whole array a! You should just do comparisons to find the index of the median between a[left] and a[right].
At some point you might have right < left, so the first line of alg5 you should write if (left >= right)
I have been reading through all of the QuickSort questions on SO, but I cannot resolve this specific problem. By referencing the other questions and comparing my faults to theirs I have gotten to a specific point, that I cannot find the answer to, even in Debug mode.
I was repeatedly getting out of bounds -1, so I added a conditional check for
if(pivot > 0)
and that stopped the overflow, but since I am using 0 as my partition, It partitions once and then terminates. The first partition is correct, but if I change that number to include 0, the I get infinite recursion again. If I completely take the line out, I get index out of bounds errors that I cannot seem to tackle.
Here's where I am so far:
public class QuickSort {
int[] array;
public static void main(String[] args) {
QuickSort qs = new QuickSort();
qs.array = new int[] {35, 82, 2, 24, 57, 17};
qs.quickSort(qs.array, 0, qs.array.length - 1);
for(int i = 0; i < qs.array.length; i++) {
System.out.println(qs.array[i]);
}
}
public void quickSort(int[] array, int left, int right) {
if(array.length == 1) {
return;
}
if(left < right) {
int pivot = partition(array, left, right);
quickSort(array, left, pivot - 1);
quickSort(array, pivot + 1, right);
}
}
public int partition(int[] array, int left, int right) {
if(array.length == 1) {
return right;
}
int pivot = array[0];
int pivotIndex = 0;
int leftPointer = left - 1;
int rightPointer = right + 1;
while(pivotIndex < right) {
if(leftPointer > rightPointer) {
break;
}
leftPointer++;
while(leftPointer < array.length - 1 && array[leftPointer] <= pivot) {
leftPointer++;
}
rightPointer--;
while(rightPointer > leftPointer && array[rightPointer] > pivot) {
rightPointer--;
}
if(leftPointer < rightPointer) {
int temp = array[leftPointer];
array[leftPointer] = array[rightPointer];
array[rightPointer] = temp;
} else {
int temp = array[rightPointer];
array[rightPointer] = array[pivotIndex];
array[pivotIndex] = temp;
}
}
return rightPointer;
}
EDIT: After a few more alterations, I can now get it to always return an array without overflow, but it still only partitions once.
I'm pretty sure I fixed it now. You were increasing the left and right pointers within the partition method before you wanted to (outside of the "checks"). Change your partition method as follows:
public static int partition(int[] array, int left, int right) {
if(array.length == 1)
return right;
int pivot = array[0];
int pivotIndex = 0;
int leftPointer = left; // Remove the +1
int rightPointer = right; // Remove the +1
while(pivotIndex < right) {
if(leftPointer > rightPointer) {
break;
}
//leftPointer++;
while((leftPointer < array.length - 1) && (array[leftPointer] <= pivot)) {
leftPointer++;
}
//rightPointer--;
while((rightPointer > leftPointer) && (array[rightPointer] > pivot)) {
rightPointer--;
}
if(leftPointer < rightPointer) {
int temp = array[leftPointer];
array[leftPointer] = array[rightPointer];
array[rightPointer] = temp;
}
else {
int temp = array[rightPointer];
array[rightPointer] = array[pivotIndex];
array[pivotIndex] = temp;
}
}
return rightPointer;
}
You're returning "0" from partition whenever the array length is not 1, and setting that to the pivot. The if(pivot >= 0) will always be hit in that case, or it will iterate once if you used if(pivot > 0), which I think is the problem. If that's right, then correcting your return from partition (to "left" ?) should fix the problem.
I think you should change
if(leftPointer > rightPointer) {
break;
}
to
if(leftPointer >= rightPointer) {
break;
}
inside the while loop.
Also, I think you should compare leftPointer with rightPointer after either is changed,
// move to #### to perform compare after possible change
// if(leftPointer > rightPointer) break;
//leftPointer++;
while(leftPointer < array.length - 1 && array[leftPointer] <= pivot) leftPointer++;
//rightPointer--;
while(rightPointer > leftPointer && array[rightPointer] > pivot) rightPointer--;
//####
if(leftPointer > rightPointer) break;