Merge pull request #449 from artzok/master

fix error comments
This commit is contained in:
Christian Bender 2018-06-07 22:23:34 +02:00 committed by GitHub
commit 419b91d85c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 31 additions and 34 deletions

View File

@ -43,12 +43,12 @@ class BubbleSort implements SortAlgorithm {
BubbleSort bubbleSort = new BubbleSort();
bubbleSort.sort(integers);
// Output => 1 4 6 9 12 23 54 78 231
// Output => 231, 78, 54, 23, 12, 9, 6, 4, 1
print(integers);
// String Input
String[] strings = {"c", "a", "e", "b","d"};
//Output => a b c d e
//Output => e, d, c, b, a
print(bubbleSort.sort(strings));
}

View File

@ -21,35 +21,32 @@ class CocktailShakerSort implements SortAlgorithm {
@Override
public <T extends Comparable<T>> T[] sort(T[] array) {
int last = array.length;
// Sorting
boolean swap;
do {
swap = false;
int length = array.length;
int left = 0;
int right = length - 1;
int swappedLeft, swappedRight;
while (left < right) {
// front
for (int count = 0; count <= last - 2; count++) {
if (less(array[count + 1], array[count])) {
swap = swap(array, count, count + 1);
swappedRight = 0;
for (int i = left; i < right; i++) {
if (less(array[i + 1], array[i])) {
swap(array, i, i + 1);
swappedRight = i;
}
}
//break if no swap occurred
if (!swap) {
break;
}
swap = false;
// back
for (int count = last - 2; count >= 0; count--) {
if (less(array[count + 1], array[count])) {
swap = swap(array, count, count + 1);
right = swappedRight;
swappedLeft = length - 1;
for (int j = right; j > left; j--) {
if (less(array[j], array[j - 1])) {
swap(array, j - 1, j);
swappedLeft = j;
}
}
last--;
//end
} while (swap);
left = swappedLeft;
}
return array;
}
// Driver Program