mirror of
https://github.com/tu-darmstadt-informatik/AuD18.git
synced 2025-12-13 01:45:49 +00:00
Implement Quicksort
This commit is contained in:
parent
43688b2237
commit
c775fb378f
1
QuickSort/.gitignore
vendored
Normal file
1
QuickSort/.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
||||
bin/
|
||||
@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -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
|
||||
|
||||
}
|
||||
|
||||
@ -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
|
||||
|
||||
}
|
||||
|
||||
@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user