mirror of
https://github.com/tu-darmstadt-informatik/AuD18.git
synced 2025-12-13 09:55:49 +00:00
Add some documentation and optimize WriteOps
This commit is contained in:
parent
18fe8a8a93
commit
e86a96c63a
@ -12,7 +12,14 @@ public abstract class QuickSort {
|
|||||||
// DO NOT modify this method
|
// DO NOT modify this method
|
||||||
public abstract void Quicksort(SortArray records, int left, int right);
|
public abstract void Quicksort(SortArray records, int left, int right);
|
||||||
|
|
||||||
// You may add additional methods here
|
/**
|
||||||
|
* Partitions a list with no pre-supplied values
|
||||||
|
* @param pivot Pivot element to compare against
|
||||||
|
* @param records The SortArray to work on
|
||||||
|
* @param leftBound Left bounding index of the subset to partition
|
||||||
|
* @param rightBound Right bounding index of the subset to partition
|
||||||
|
* @return Index of the split position
|
||||||
|
*/
|
||||||
protected int partition(SortingItem pivot, SortArray records, int leftBound, int rightBound) {
|
protected int partition(SortingItem pivot, SortArray records, int leftBound, int rightBound) {
|
||||||
|
|
||||||
if(leftBound == rightBound)
|
if(leftBound == rightBound)
|
||||||
@ -24,6 +31,15 @@ public abstract class QuickSort {
|
|||||||
return partition(pivot, records, leftBound, rightBound, leftValue, rightValue);
|
return partition(pivot, records, leftBound, rightBound, leftValue, rightValue);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Partitions a list with pre-supplied leftValue. Used by QuickSortA as pivot = leftValue is already known
|
||||||
|
* @param pivot Pivot element to compare against
|
||||||
|
* @param records The SortArray to work on
|
||||||
|
* @param leftBound Left bounding index of the subset to partition
|
||||||
|
* @param rightBound Right bounding index of the subset to partition
|
||||||
|
* @param leftValue SortingItem at position leftBound
|
||||||
|
* @return Index of the split position
|
||||||
|
*/
|
||||||
protected int partition(SortingItem pivot, SortArray records, int leftBound, int rightBound, SortingItem leftValue) {
|
protected int partition(SortingItem pivot, SortArray records, int leftBound, int rightBound, SortingItem leftValue) {
|
||||||
|
|
||||||
if(leftBound == rightBound)
|
if(leftBound == rightBound)
|
||||||
@ -34,6 +50,16 @@ public abstract class QuickSort {
|
|||||||
return partition(pivot, records, leftBound, rightBound, leftValue, rightValue);
|
return partition(pivot, records, leftBound, rightBound, leftValue, rightValue);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Partitions a list with pre-supplied leftValue and rightValue. Used by QuickSortB as leftValue and rightValue are already known
|
||||||
|
* @param pivot Pivot element to compare against
|
||||||
|
* @param records The SortArray to work on
|
||||||
|
* @param leftBound Left bounding index of the subset to partition
|
||||||
|
* @param rightBound Right bounding index of the subset to partition
|
||||||
|
* @param leftValue SortingItem at position leftBound
|
||||||
|
* @param rightValue SortingItem at position rightBound
|
||||||
|
* @return Index of the split position
|
||||||
|
*/
|
||||||
protected int partition(SortingItem pivot, SortArray records, int leftBound, int rightBound, SortingItem leftValue, SortingItem rightValue) {
|
protected int partition(SortingItem pivot, SortArray records, int leftBound, int rightBound, SortingItem leftValue, SortingItem rightValue) {
|
||||||
|
|
||||||
if(leftBound == rightBound)
|
if(leftBound == rightBound)
|
||||||
@ -41,21 +67,30 @@ public abstract class QuickSort {
|
|||||||
|
|
||||||
int leftIndex = leftBound;
|
int leftIndex = leftBound;
|
||||||
int rightIndex = rightBound;
|
int rightIndex = rightBound;
|
||||||
|
int pivotIndex = leftBound;
|
||||||
SortingItem temp;
|
SortingItem temp;
|
||||||
|
|
||||||
while(true){
|
while(true){
|
||||||
while(leftIndex < rightBound && leftValue.compareTo(pivot) < 0) {
|
while(leftIndex < rightBound && leftValue.compareTo(pivot) <= 0) {
|
||||||
|
pivotIndex = leftValue.compareTo(pivot) == 0 ? leftIndex : pivotIndex; // 'Search for' the pivot index while traversing list to avoid passing the index as an argument
|
||||||
leftIndex += 1;
|
leftIndex += 1;
|
||||||
leftValue = records.getElementAt(leftIndex);
|
leftValue = records.getElementAt(leftIndex);
|
||||||
}
|
}
|
||||||
while(rightIndex > leftBound && rightValue.compareTo(pivot) > 0) {
|
while(rightIndex > leftBound && rightValue.compareTo(pivot) >= 0) {
|
||||||
|
pivotIndex = rightValue.compareTo(pivot) == 0 ? rightIndex : pivotIndex; // 'Search for' the pivot index while traversing list to avoid passing the index as an argument
|
||||||
rightIndex -= 1;
|
rightIndex -= 1;
|
||||||
rightValue = records.getElementAt(rightIndex);
|
rightValue = records.getElementAt(rightIndex);
|
||||||
}
|
}
|
||||||
if(leftIndex >= rightIndex) {
|
if(leftIndex >= rightIndex) {
|
||||||
|
// Swap the pivot if necessary (values equal to pivot are not swapped while partitioning)
|
||||||
|
if((pivotIndex > rightIndex && pivot.compareTo(rightValue) < 0) || (pivotIndex < rightIndex && pivot.compareTo(rightValue) > 0)) {
|
||||||
|
records.setElementAt(pivotIndex, rightValue);
|
||||||
|
records.setElementAt(rightIndex, pivot);
|
||||||
|
}
|
||||||
return rightIndex;
|
return rightIndex;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Swap right and left elements and local copies
|
||||||
records.setElementAt(leftIndex, rightValue);
|
records.setElementAt(leftIndex, rightValue);
|
||||||
records.setElementAt(rightIndex, leftValue);
|
records.setElementAt(rightIndex, leftValue);
|
||||||
temp = rightValue;
|
temp = rightValue;
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user