|
@@ -3,8 +3,8 @@ package com.garypaduana.groovytools.algorithm
|
|
|
/**
|
|
/**
|
|
|
* Created by gary on 2/5/17.
|
|
* Created by gary on 2/5/17.
|
|
|
*/
|
|
*/
|
|
|
-class Sort{
|
|
|
|
|
- static class SortingAlgorithms{
|
|
|
|
|
|
|
+class Sort {
|
|
|
|
|
+ static class SortingAlgorithms {
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
* The algorithm starts at the beginning of the data set.
|
|
* The algorithm starts at the beginning of the data set.
|
|
@@ -15,10 +15,10 @@ class Sort{
|
|
|
* two elements, repeating until no swaps have occurred on
|
|
* two elements, repeating until no swaps have occurred on
|
|
|
* the last pass.
|
|
* the last pass.
|
|
|
*/
|
|
*/
|
|
|
- def static bubbleSort(def array){
|
|
|
|
|
- for(int k = array.size() - 1; k > 0; k--){
|
|
|
|
|
- for(int i = 0; i < k; i++){
|
|
|
|
|
- if(array[i] > array[i + 1]){
|
|
|
|
|
|
|
+ def static bubbleSort(def array) {
|
|
|
|
|
+ for (int k = array.size() - 1; k > 0; k--) {
|
|
|
|
|
+ for (int i = 0; i < k; i++) {
|
|
|
|
|
+ if (array[i] > array[i + 1]) {
|
|
|
swap(array, i, i + 1)
|
|
swap(array, i, i + 1)
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
@@ -32,12 +32,12 @@ class Sort{
|
|
|
* for the remainder of the list. It does no more than n
|
|
* for the remainder of the list. It does no more than n
|
|
|
* swaps, and thus is useful where swapping is very expensive.
|
|
* swaps, and thus is useful where swapping is very expensive.
|
|
|
*/
|
|
*/
|
|
|
- def static selectionSort(def array){
|
|
|
|
|
- for(int k = 0; k < array.size(); k++){
|
|
|
|
|
|
|
+ def static selectionSort(def array) {
|
|
|
|
|
+ for (int k = 0; k < array.size(); k++) {
|
|
|
def min = array[k]
|
|
def min = array[k]
|
|
|
def minIndex = k
|
|
def minIndex = k
|
|
|
- for(int i = k; i < array.size(); i++){
|
|
|
|
|
- if(array[i] < min){
|
|
|
|
|
|
|
+ for (int i = k; i < array.size(); i++) {
|
|
|
|
|
+ if (array[i] < min) {
|
|
|
min = array[i]
|
|
min = array[i]
|
|
|
minIndex = i
|
|
minIndex = i
|
|
|
}
|
|
}
|
|
@@ -57,8 +57,8 @@ class Sort{
|
|
|
* recursively sorted. This yields average time complexity of
|
|
* recursively sorted. This yields average time complexity of
|
|
|
* O(n log n), with low overhead, and thus this is a popular algorithm.
|
|
* O(n log n), with low overhead, and thus this is a popular algorithm.
|
|
|
*/
|
|
*/
|
|
|
- def static quickSort(def array, def low, def high){
|
|
|
|
|
- if(low < high){
|
|
|
|
|
|
|
+ def static quickSort(def array, def low, def high) {
|
|
|
|
|
+ if (low < high) {
|
|
|
def p = partition(array, low, high)
|
|
def p = partition(array, low, high)
|
|
|
quickSort(array, low, p - 1)
|
|
quickSort(array, low, p - 1)
|
|
|
quickSort(array, p + 1, high)
|
|
quickSort(array, p + 1, high)
|
|
@@ -66,9 +66,9 @@ class Sort{
|
|
|
return array
|
|
return array
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- def static partition(def array, def low, def high){
|
|
|
|
|
|
|
+ def static partition(def array, def low, def high) {
|
|
|
def pivotIndex = (int) ((high - low) / 2 + low)
|
|
def pivotIndex = (int) ((high - low) / 2 + low)
|
|
|
- if(pivotIndex == low){
|
|
|
|
|
|
|
+ if (pivotIndex == low) {
|
|
|
pivotIndex++
|
|
pivotIndex++
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -77,8 +77,8 @@ class Sort{
|
|
|
swap(array, pivotIndex, high)
|
|
swap(array, pivotIndex, high)
|
|
|
def storeIndex = low
|
|
def storeIndex = low
|
|
|
|
|
|
|
|
- for(int i = low; i < high; i++){
|
|
|
|
|
- if(array[i] <= pivotValue){
|
|
|
|
|
|
|
+ for (int i = low; i < high; i++) {
|
|
|
|
|
+ if (array[i] <= pivotValue) {
|
|
|
swap(array, i, storeIndex)
|
|
swap(array, i, storeIndex)
|
|
|
storeIndex++
|
|
storeIndex++
|
|
|
}
|
|
}
|
|
@@ -90,7 +90,7 @@ class Sort{
|
|
|
/**
|
|
/**
|
|
|
* Swaps values found at positions i and j within array.
|
|
* Swaps values found at positions i and j within array.
|
|
|
*/
|
|
*/
|
|
|
- def static swap(def array, int i, int j){
|
|
|
|
|
|
|
+ def static swap(def array, int i, int j) {
|
|
|
def temp = array[i]
|
|
def temp = array[i]
|
|
|
array[i] = array[j]
|
|
array[i] = array[j]
|
|
|
array[j] = temp
|
|
array[j] = temp
|