Implement Quicksort

This commit is contained in:
rec0de 2018-04-25 12:30:55 +02:00
parent 43688b2237
commit c775fb378f
5 changed files with 72 additions and 8 deletions

1
QuickSort/.gitignore vendored Normal file
View File

@ -0,0 +1 @@
bin/

View File

@ -13,5 +13,33 @@ public abstract class QuickSort {
public abstract void Quicksort(SortArray records, int left, int right);
// You may add additional methods here
protected int partition(SortingItem pivot, SortArray records, int leftBound, int rightBound) {
if(leftBound == rightBound)
return leftBound;
int leftIndex = leftBound;
int rightIndex = rightBound;
while(true){
while(leftIndex < rightBound && records.getElementAt(leftIndex).compareTo(pivot) < 0) {
leftIndex += 1;
}
while(rightIndex > leftBound && records.getElementAt(rightIndex).compareTo(pivot) > 0) {
rightIndex -= 1;
}
if(leftIndex >= rightIndex) {
return rightIndex;
}
swap(leftIndex, rightIndex, records);
}
}
protected void swap(int i1, int i2, SortArray records) {
SortingItem temp = records.getElementAt(i1);
records.setElementAt(i1, records.getElementAt(i2));
records.setElementAt(i2, temp);
}
}

View File

@ -18,11 +18,16 @@ public class QuickSortA extends QuickSort {
*/
@Override
public void Quicksort(SortArray records, int left, int right) {
// TODO
// implement the Quicksort A algorithm to sort the records
// (choose the pivot as the first (leftmost) element in the list)
if(right - left < 1)
return;
SortingItem pivot = records.getElementAt(left);
int p = partition(pivot, records, left, right);
Quicksort(records, left, p);
Quicksort(records, p+1, right);
}
// You may add additional methods here
}

View File

@ -18,12 +18,39 @@ public class QuickSortB extends QuickSort {
*/
@Override
public void Quicksort(SortArray records, int left, int right) {
// TODO
// implement the Quicksort B algorithm to sort the records
// (choose the pivot as the median value of the elements at position
// (left (first),middle,right(last)))
if(right - left < 1)
return;
SortingItem pivot = getPivot(left, right, records);
int p = partition(pivot, records, left, right);
Quicksort(records, left, p);
Quicksort(records, p+1, right);
}
private SortingItem getPivot(int left, int right, SortArray records) {
SortingItem l = records.getElementAt(left);
SortingItem r = records.getElementAt(right);
SortingItem m = records.getElementAt((int) Math.floor(left + (right - left) / 2));
SortingItem t = null;
// 'Sort' the three elements by doing two swaps if necessary, then return the middle (= median) one
if(l.compareTo(m) > 0) {
t = l;
l = m;
m = t;
}
if(r.compareTo(m) < 0) {
t = r;
r = m;
m = t;
}
return m;
}
// You may add additional methods here
}

View File

@ -5,7 +5,7 @@ package lab;
* This class represents one entry of the list that has to be sorted.
*
*/
public class SortingItem {
public class SortingItem implements Comparable<SortingItem>{
// DO NOT modify
public String BookSerialNumber;
@ -24,6 +24,9 @@ public class SortingItem {
this.Status = otherItem.Status;
}
// You may add additional methods here
@Override
public int compareTo(SortingItem arg0) {
return this.BookSerialNumber.compareTo(arg0.BookSerialNumber) == 0 ? this.ReaderID.compareTo(arg0.ReaderID): this.BookSerialNumber.compareTo(arg0.BookSerialNumber);
}
}