mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-12 06:37:03 +08:00
Refactored BubbleSort, CycleSort, CocktailShakerSort
This commit is contained in:
@ -1,86 +0,0 @@
|
|||||||
|
|
||||||
/**
|
|
||||||
*
|
|
||||||
* @author Mateus Bizzo (https://github.com/MattBizzo)
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
|
|
||||||
class CocktailShakerSort {
|
|
||||||
/**
|
|
||||||
* This method implements the Generic Cocktail Shaker Sort
|
|
||||||
*
|
|
||||||
* @param array
|
|
||||||
* The array to be sorted
|
|
||||||
* @param last
|
|
||||||
* The count of total number of elements in array Sorts the array in
|
|
||||||
* increasing order
|
|
||||||
**/
|
|
||||||
|
|
||||||
public static <T extends Comparable<T>> void CS(T array[], int last) {
|
|
||||||
|
|
||||||
// Sorting
|
|
||||||
boolean swap;
|
|
||||||
do {
|
|
||||||
swap = false;
|
|
||||||
|
|
||||||
//front
|
|
||||||
for (int count = 0; count <= last - 2; count++) {
|
|
||||||
int comp = array[count].compareTo(array[count + 1]);
|
|
||||||
if (comp > 0) {
|
|
||||||
T aux = array[count];
|
|
||||||
array[count] = array[count + 1];
|
|
||||||
array[count + 1] = aux;
|
|
||||||
swap = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
//break if no swap occurred
|
|
||||||
if (!swap) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
swap = false;
|
|
||||||
|
|
||||||
//back
|
|
||||||
for (int count = last - 2; count >= 0; count--) {
|
|
||||||
int comp = array[count].compareTo(array[count + 1]);
|
|
||||||
if (comp > 0) {
|
|
||||||
T aux = array[count];
|
|
||||||
array[count] = array[count + 1];
|
|
||||||
array[count + 1] = aux;
|
|
||||||
swap = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
last--;
|
|
||||||
//end
|
|
||||||
} while (swap);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Driver Program
|
|
||||||
public static void main(String[] args) {
|
|
||||||
// Integer Input
|
|
||||||
int[] arr1 = { 4, 23, 6, 78, 1, 54, 231, 9, 12 };
|
|
||||||
int last = arr1.length;
|
|
||||||
Integer[] array = new Integer[last];
|
|
||||||
for (int i = 0; i < last; i++) {
|
|
||||||
array[i] = arr1[i];
|
|
||||||
}
|
|
||||||
|
|
||||||
CS(array, last);
|
|
||||||
|
|
||||||
// Output => 1 4 6 9 12 23 54 78 231
|
|
||||||
for (int i = 0; i < last; i++) {
|
|
||||||
System.out.print(array[i] + "\t");
|
|
||||||
}
|
|
||||||
System.out.println();
|
|
||||||
|
|
||||||
// String Input
|
|
||||||
String[] array1 = { "c", "a", "e", "b", "d" };
|
|
||||||
last = array1.length;
|
|
||||||
|
|
||||||
CS(array1, last);
|
|
||||||
|
|
||||||
// Output => a b c d e
|
|
||||||
for (int i = 0; i < last; i++) {
|
|
||||||
System.out.print(array1[i] + "\t");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,7 +1,6 @@
|
|||||||
package sort;
|
package sort;
|
||||||
|
|
||||||
import static sort.SortUtils.print;
|
import static sort.SortUtils.*;
|
||||||
import static sort.SortUtils.swap;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
@ -27,10 +26,8 @@ class BubbleSort implements SortAlgorithm{
|
|||||||
do {
|
do {
|
||||||
swap = false;
|
swap = false;
|
||||||
for (int count = 0; count < last-1; count++) {
|
for (int count = 0; count < last-1; count++) {
|
||||||
int comp = array[count].compareTo(array[count + 1]);
|
if (less(array[count], array[count + 1])) {
|
||||||
if (comp > 0) {
|
swap = swap(array, count, count + 1);
|
||||||
swap(array, count, count + 1);
|
|
||||||
swap = true;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
last--;
|
last--;
|
||||||
@ -42,7 +39,7 @@ class BubbleSort implements SortAlgorithm{
|
|||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
|
|
||||||
// Integer Input
|
// Integer Input
|
||||||
Integer[] integers = {4,23,6,78,1,54,231,9,12};
|
Integer[] integers = {4, 23, 6, 78, 1, 54, 231, 9, 12};
|
||||||
BubbleSort bubbleSort = new BubbleSort();
|
BubbleSort bubbleSort = new BubbleSort();
|
||||||
bubbleSort.sort(integers);
|
bubbleSort.sort(integers);
|
||||||
|
|
||||||
|
70
Sorts/src/sort/CocktailShakerSort.java
Normal file
70
Sorts/src/sort/CocktailShakerSort.java
Normal file
@ -0,0 +1,70 @@
|
|||||||
|
package sort;
|
||||||
|
|
||||||
|
import static sort.SortUtils.*;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @author Mateus Bizzo (https://github.com/MattBizzo)
|
||||||
|
* @author Podshivalov Nikita (https://github.com/nikitap492)
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
class CocktailShakerSort implements SortAlgorithm {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This method implements the Generic Cocktail Shaker Sort
|
||||||
|
*
|
||||||
|
* @param array The array to be sorted
|
||||||
|
* Sorts the array in increasing order
|
||||||
|
**/
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public <T extends Comparable<T>> T[] sort(T[] array){
|
||||||
|
|
||||||
|
int last = array.length;
|
||||||
|
|
||||||
|
// Sorting
|
||||||
|
boolean swap;
|
||||||
|
do {
|
||||||
|
swap = false;
|
||||||
|
|
||||||
|
//front
|
||||||
|
for (int count = 0; count <= last - 2; count++) {
|
||||||
|
if (less(array[count + 1], array[count])) {
|
||||||
|
swap = swap(array, count, count + 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
last--;
|
||||||
|
//end
|
||||||
|
} while (swap);
|
||||||
|
return array;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Driver Program
|
||||||
|
public static void main(String[] args) {
|
||||||
|
// Integer Input
|
||||||
|
Integer[] integers = { 4, 23, 6, 78, 1, 54, 231, 9, 12 };
|
||||||
|
CocktailShakerSort shakerSort = new CocktailShakerSort();
|
||||||
|
|
||||||
|
// Output => 1 4 6 9 12 23 54 78 231
|
||||||
|
print(shakerSort.sort(integers));
|
||||||
|
|
||||||
|
// String Input
|
||||||
|
String[] strings = { "c", "a", "e", "b", "d" };
|
||||||
|
print(shakerSort.sort(strings));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
81
Sorts/src/sort/CycleSort.java
Normal file
81
Sorts/src/sort/CycleSort.java
Normal file
@ -0,0 +1,81 @@
|
|||||||
|
package sort;
|
||||||
|
|
||||||
|
import static sort.SortUtils.less;
|
||||||
|
import static sort.SortUtils.print;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Podshivalov Nikita (https://github.com/nikitap492)
|
||||||
|
*/
|
||||||
|
class CycleSort implements SortAlgorithm {
|
||||||
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public <T extends Comparable<T>> T[] sort(T[] arr) {
|
||||||
|
int n = arr.length;
|
||||||
|
|
||||||
|
// traverse array elements
|
||||||
|
for (int j = 0; j <= n - 2; j++) {
|
||||||
|
// initialize item as starting point
|
||||||
|
T item = arr[j];
|
||||||
|
|
||||||
|
// Find position where we put the item.
|
||||||
|
int pos = j;
|
||||||
|
for (int i = j + 1; i < n; i++)
|
||||||
|
if (less(arr[i], item)) pos++;
|
||||||
|
|
||||||
|
// If item is already in correct position
|
||||||
|
if (pos == j) continue;
|
||||||
|
|
||||||
|
// ignore all duplicate elements
|
||||||
|
while (item.compareTo(arr[pos]) == 0)
|
||||||
|
pos += 1;
|
||||||
|
|
||||||
|
// put the item to it's right position
|
||||||
|
if (pos != j) {
|
||||||
|
item = replace(arr, pos, item);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Rotate rest of the cycle
|
||||||
|
while (pos != j) {
|
||||||
|
pos = j;
|
||||||
|
|
||||||
|
// Find position where we put the element
|
||||||
|
for (int i = j + 1; i < n; i++)
|
||||||
|
if (less(arr[i], item)){
|
||||||
|
pos += 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// ignore all duplicate elements
|
||||||
|
while (item.compareTo(arr[pos]) == 0)
|
||||||
|
pos += 1;
|
||||||
|
|
||||||
|
// put the item to it's right position
|
||||||
|
if (item != arr[pos]) {
|
||||||
|
item = replace(arr, pos, item);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return arr;
|
||||||
|
}
|
||||||
|
|
||||||
|
private <T extends Comparable<T>> T replace(T[] arr, int pos, T item){
|
||||||
|
T temp = item;
|
||||||
|
item = arr[pos];
|
||||||
|
arr[pos] = temp;
|
||||||
|
return item;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
Integer arr[] = { 4, 23, 6, 78, 1, 26, 11, 23 , 0, -6, 3, 54, 231, 9, 12 };
|
||||||
|
CycleSort cycleSort = new CycleSort();
|
||||||
|
cycleSort.sort(arr);
|
||||||
|
|
||||||
|
System.out.println("After sort : ");
|
||||||
|
print(arr);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -18,10 +18,11 @@ final class SortUtils {
|
|||||||
* @param idx index of the first element
|
* @param idx index of the first element
|
||||||
* @param idy index of the second element
|
* @param idy index of the second element
|
||||||
*/
|
*/
|
||||||
static <T> void swap(T[] array, int idx, int idy){
|
static <T> boolean swap(T[] array, int idx, int idy){
|
||||||
T swap = array[idx];
|
T swap = array[idx];
|
||||||
array[idx] = array[idy];
|
array[idx] = array[idy];
|
||||||
array[idy] = swap;
|
array[idy] = swap;
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user