├── .editorconfig ├── .gitattributes ├── .gitignore ├── .travis.yml ├── LICENSE ├── README.md ├── build.gradle ├── src └── com │ └── growingwiththeweb │ └── sorting │ ├── BubbleSort.java │ ├── BubbleSortOptimised.java │ ├── BucketSort.java │ ├── CocktailSort.java │ ├── CombSort.java │ ├── CountingSort.java │ ├── GnomeSort.java │ ├── Heapsort.java │ ├── InsertionSort.java │ ├── MergeSort.java │ ├── MergeSortBottomUp.java │ ├── MergeSortNatural.java │ ├── OddEvenSort.java │ ├── Quicksort.java │ ├── RadixSort.java │ └── SelectionSort.java └── test └── com └── growingwiththeweb └── sorting ├── BaseSortTest.java ├── BubbleSortOptimisedTest.java ├── BubbleSortTest.java ├── BucketSortTest.java ├── CocktailSortTest.java ├── CombSortTest.java ├── CountingSortTest.java ├── GnomeSortTest.java ├── HeapsortTest.java ├── InsertionSortTest.java ├── MergeSortBottomUpTest.java ├── MergeSortNaturalTest.java ├── MergeSortTest.java ├── OddEvenSortTest.java ├── QuicksortRandomTest.java ├── QuicksortTest.java ├── RadixSortTest.java └── SelectionSortTest.java /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | charset = utf-8 5 | end_of_line = lf 6 | indent_style = space 7 | indent_size = 4 8 | insert_final_newline = true 9 | trim_trailing_whitespace = true 10 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | * text=auto 2 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .gradle/ 2 | build/ 3 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: java 2 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2012 Daniel Imms, http://www.growingwiththeweb.com 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of 6 | this software and associated documentation files (the "Software"), to deal in 7 | the Software without restriction, including without limitation the rights to 8 | use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 9 | the Software, and to permit persons to whom the Software is furnished to do so, 10 | subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 17 | FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 18 | COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 19 | IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 20 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # java-sorting 2 | 3 | [![Build Status](https://travis-ci.org/gwtw/java-sorting.svg?branch=master)](https://travis-ci.org/gwtw/java-sorting) 4 | 5 | A collection of sorting algorithms written in Java for [Growing with the Web][1]. 6 | 7 | 8 | 9 | ## Dependencies 10 | 11 | Java (JRE, JDK) and [Gradle](https://gradle.org/) are required to build and test this project. 12 | 13 | 14 | 15 | ## Build 16 | 17 | ```bash 18 | gradle build 19 | ``` 20 | 21 | 22 | 23 | ## Test 24 | 25 | ```bash 26 | gradle test 27 | ``` 28 | 29 | 30 | 31 | ## See also 32 | 33 | - [Complexity table for sorting algorithms](https://github.com/gwtw/js-sorting/blob/master/lib/README.md) 34 | 35 | 36 | 37 | [1]: http://www.growingwiththeweb.com 38 | -------------------------------------------------------------------------------- /build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'java' 2 | 3 | repositories { 4 | mavenCentral() 5 | } 6 | 7 | dependencies { 8 | testCompile 'junit:junit:[4,)' 9 | } 10 | 11 | sourceSets { 12 | main { 13 | java { 14 | srcDirs = ["src/"] 15 | } 16 | } 17 | test { 18 | java { 19 | srcDirs = ["test/"] 20 | } 21 | } 22 | } 23 | 24 | test { 25 | testLogging { 26 | // Show that tests are run in the command-line output 27 | events 'passed' 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /src/com/growingwiththeweb/sorting/BubbleSort.java: -------------------------------------------------------------------------------- 1 | package com.growingwiththeweb.sorting; 2 | 3 | public class BubbleSort { 4 | public static void sort(Integer[] array) { 5 | for (int i = 0; i < array.length - 1; i++) { 6 | for (int j = 1; j < array.length - i; j++) { 7 | if (array[j - 1] > array[j]) { 8 | swap(array, j, j - 1); 9 | } 10 | } 11 | } 12 | } 13 | 14 | private static void swap(Integer[] array, int a, int b) { 15 | Integer temp = array[a]; 16 | array[a] = array[b]; 17 | array[b] = temp; 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /src/com/growingwiththeweb/sorting/BubbleSortOptimised.java: -------------------------------------------------------------------------------- 1 | package com.growingwiththeweb.sorting; 2 | 3 | public class BubbleSortOptimised { 4 | public static void sort(Integer[] array) { 5 | int unsortedBelow = array.length; 6 | while (unsortedBelow != 0) { 7 | int lastSwap = 0; 8 | for (int i = 1; i < unsortedBelow; i++) { 9 | if (array[i - 1] > array[i]) { 10 | swap(array, i, i - 1); 11 | lastSwap = i; 12 | } 13 | } 14 | unsortedBelow = lastSwap; 15 | } 16 | } 17 | 18 | private static void swap(Integer[] array, int a, int b) { 19 | Integer temp = array[a]; 20 | array[a] = array[b]; 21 | array[b] = temp; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /src/com/growingwiththeweb/sorting/BucketSort.java: -------------------------------------------------------------------------------- 1 | package com.growingwiththeweb.sorting; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | 6 | public class BucketSort { 7 | private static final int DEFAULT_BUCKET_SIZE = 5; 8 | 9 | public static void sort(Integer[] array) { 10 | sort(array, DEFAULT_BUCKET_SIZE); 11 | } 12 | 13 | public static void sort(Integer[] array, int bucketSize) { 14 | if (array.length == 0) { 15 | return; 16 | } 17 | 18 | // Determine minimum and maximum values 19 | Integer minValue = array[0]; 20 | Integer maxValue = array[0]; 21 | for (int i = 1; i < array.length; i++) { 22 | if (array[i] < minValue) { 23 | minValue = array[i]; 24 | } else if (array[i] > maxValue) { 25 | maxValue = array[i]; 26 | } 27 | } 28 | 29 | // Initialise buckets 30 | int bucketCount = (maxValue - minValue) / bucketSize + 1; 31 | List> buckets = new ArrayList>(bucketCount); 32 | for (int i = 0; i < bucketCount; i++) { 33 | buckets.add(new ArrayList()); 34 | } 35 | 36 | // Distribute input array values into buckets 37 | for (int i = 0; i < array.length; i++) { 38 | buckets.get((array[i] - minValue) / bucketSize).add(array[i]); 39 | } 40 | 41 | // Sort buckets and place back into input array 42 | int currentIndex = 0; 43 | for (int i = 0; i < buckets.size(); i++) { 44 | Integer[] bucketArray = new Integer[buckets.get(i).size()]; 45 | bucketArray = buckets.get(i).toArray(bucketArray); 46 | InsertionSort.sort(bucketArray); 47 | for (int j = 0; j < bucketArray.length; j++) { 48 | array[currentIndex++] = bucketArray[j]; 49 | } 50 | } 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /src/com/growingwiththeweb/sorting/CocktailSort.java: -------------------------------------------------------------------------------- 1 | package com.growingwiththeweb.sorting; 2 | 3 | public class CocktailSort { 4 | public static void sort(Integer[] array) { 5 | int start = -1; 6 | int end = array.length - 2; 7 | boolean swapped; 8 | 9 | do { 10 | swapped = false; 11 | for (int i = ++start; i <= end; i++) { 12 | if (array[i] > array[i + 1]) { 13 | swap(array, i, i + 1); 14 | swapped = true; 15 | } 16 | } 17 | 18 | if (!swapped) { 19 | break; 20 | } 21 | 22 | swapped = false; 23 | for (int i = --end; i >= start; i--) { 24 | if (array[i] > array[i + 1]) { 25 | swap(array, i, i + 1); 26 | swapped = true; 27 | } 28 | } 29 | } while (swapped); 30 | } 31 | 32 | private static void swap(Integer[] array, int a, int b) { 33 | Integer temp = array[a]; 34 | array[a] = array[b]; 35 | array[b] = temp; 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /src/com/growingwiththeweb/sorting/CombSort.java: -------------------------------------------------------------------------------- 1 | package com.growingwiththeweb.sorting; 2 | 3 | public class CombSort { 4 | public static void sort(Integer[] array) { 5 | int gap = array.length; 6 | float shrinkFactor = 1.3f; 7 | boolean swapped = false; 8 | 9 | while (gap > 1 || swapped) { 10 | if (gap > 1) { 11 | gap = (int)(gap / shrinkFactor); 12 | } 13 | 14 | swapped = false; 15 | 16 | for (int i = 0; gap + i < array.length; i++) { 17 | if (array[i] > array[i + gap]) { 18 | swap(array, i, i + gap); 19 | swapped = true; 20 | } 21 | } 22 | } 23 | } 24 | 25 | private static void swap(Integer[] array, int a, int b) { 26 | Integer temp = array[a]; 27 | array[a] = array[b]; 28 | array[b] = temp; 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /src/com/growingwiththeweb/sorting/CountingSort.java: -------------------------------------------------------------------------------- 1 | package com.growingwiththeweb.sorting; 2 | 3 | public class CountingSort { 4 | public static void sort(Integer[] array) { 5 | if (array.length == 0) { 6 | return; 7 | } 8 | 9 | // Determine minimum and maximum values 10 | Integer minValue = array[0]; 11 | Integer maxValue = array[0]; 12 | for (int i = 1; i < array.length; i++) { 13 | if (array[i] < minValue) { 14 | minValue = array[i]; 15 | } else if (array[i] > maxValue) { 16 | maxValue = array[i]; 17 | } 18 | } 19 | 20 | sort(array, minValue, maxValue); 21 | } 22 | 23 | public static void sort(Integer[] array, int minValue, int maxValue) { 24 | int[] buckets = new int[maxValue - minValue + 1]; 25 | 26 | for (int i = 0; i < array.length; i++) { 27 | buckets[array[i] - minValue]++; 28 | } 29 | 30 | int sortedIndex = 0; 31 | for (int i = 0; i < buckets.length; i++) { 32 | while (buckets[i] > 0) { 33 | array[sortedIndex++] = i + minValue; 34 | buckets[i]--; 35 | } 36 | } 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /src/com/growingwiththeweb/sorting/GnomeSort.java: -------------------------------------------------------------------------------- 1 | package com.growingwiththeweb.sorting; 2 | 3 | public class GnomeSort { 4 | public static > void sort(T[] array) { 5 | int i = 0; 6 | while (i < array.length) { 7 | if (i == 0 || array[i - 1].compareTo(array[i]) <= 0) { 8 | i++; 9 | } else { 10 | swap(array, i, i - 1); 11 | i--; 12 | } 13 | } 14 | } 15 | 16 | private static > void swap( 17 | T[] array, int a, int b) { 18 | T temp = array[a]; 19 | array[a] = array[b]; 20 | array[b] = temp; 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /src/com/growingwiththeweb/sorting/Heapsort.java: -------------------------------------------------------------------------------- 1 | package com.growingwiththeweb.sorting; 2 | 3 | public class Heapsort { 4 | public static > void sort(T[] array) { 5 | int heapSize = array.length; 6 | buildHeap(array, heapSize); 7 | while (heapSize > 1) { 8 | swap(array, 0, heapSize - 1); 9 | heapSize--; 10 | heapify(array, heapSize, 0); 11 | } 12 | } 13 | 14 | private static > void buildHeap( 15 | T[] array, int heapSize) { 16 | for (int i = (int)(array.length / 2); i >= 0; i--) { 17 | heapify(array, heapSize, i); 18 | } 19 | } 20 | 21 | private static > void heapify( 22 | T[] array, int heapSize, int i) { 23 | int left = i * 2 + 1; 24 | int right = i * 2 + 2; 25 | int largest; 26 | if (left < heapSize && array[left].compareTo(array[i]) > 0) 27 | largest = left; 28 | else 29 | largest = i; 30 | if (right < heapSize && array[right].compareTo(array[largest]) > 0) 31 | largest = right; 32 | if (largest != i) { 33 | swap(array, i, largest); 34 | heapify(array, heapSize, largest); 35 | } 36 | } 37 | 38 | private static > void swap( 39 | T[] array, int a, int b) { 40 | T temp = array[a]; 41 | array[a] = array[b]; 42 | array[b] = temp; 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /src/com/growingwiththeweb/sorting/InsertionSort.java: -------------------------------------------------------------------------------- 1 | package com.growingwiththeweb.sorting; 2 | 3 | public class InsertionSort { 4 | public static > void sort(T[] array) { 5 | for (int i = 1; i < array.length; i++) { 6 | T item = array[i]; 7 | int indexHole = i; 8 | while (indexHole > 0 && array[indexHole - 1].compareTo(item) > 0) { 9 | array[indexHole] = array[--indexHole]; 10 | } 11 | array[indexHole] = item; 12 | } 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /src/com/growingwiththeweb/sorting/MergeSort.java: -------------------------------------------------------------------------------- 1 | package com.growingwiththeweb.sorting; 2 | 3 | public class MergeSort { 4 | public static Integer[] sort(Integer[] array) { 5 | if (array.length <= 1) 6 | return array; 7 | 8 | int middle = array.length / 2; 9 | Integer[] left = new Integer[middle]; 10 | Integer[] right = new Integer[array.length - middle]; 11 | 12 | for (int i = 0; i < left.length; i++) { 13 | left[i] = array[i]; 14 | } 15 | for (int i = 0; i < right.length; i++) { 16 | right[i] = array[i + left.length]; 17 | } 18 | 19 | left = sort(left); 20 | right = sort(right); 21 | 22 | return merge(left, right); 23 | } 24 | 25 | public static Integer[] merge(Integer[] left, Integer[] right) { 26 | Integer[] result = new Integer[left.length + right.length]; 27 | int leftIndex = 0; 28 | int rightIndex = 0; 29 | int resultIndex = 0; 30 | while (leftIndex < left.length || rightIndex < right.length) { 31 | if (leftIndex < left.length && rightIndex < right.length) { 32 | if (left[leftIndex] <= right[rightIndex]) { 33 | result[resultIndex++] = left[leftIndex++]; 34 | } else { 35 | result[resultIndex++] = right[rightIndex++]; 36 | } 37 | } else if (leftIndex < left.length) { 38 | result[resultIndex++] = left[leftIndex++]; 39 | } else if (rightIndex < right.length) { 40 | result[resultIndex++] = right[rightIndex++]; 41 | } 42 | } 43 | return result; 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /src/com/growingwiththeweb/sorting/MergeSortBottomUp.java: -------------------------------------------------------------------------------- 1 | package com.growingwiththeweb.sorting; 2 | 3 | public class MergeSortBottomUp { 4 | public static void sort(Integer[] array) { 5 | Integer[] workArray = new Integer[array.length]; 6 | int chunkSize = 1; 7 | while (chunkSize < array.length) { 8 | int i = 0; 9 | while (i < array.length - chunkSize) { 10 | merge(array, i, chunkSize, workArray); 11 | i += chunkSize * 2; 12 | } 13 | chunkSize *= 2; 14 | } 15 | } 16 | 17 | public static void merge(Integer[] array, int leftPosition, int chunkSize, Integer[] workArray) { 18 | int rightPosition = leftPosition + chunkSize; 19 | int endPosition = Math.min(leftPosition + chunkSize * 2 - 1, array.length - 1); 20 | int leftIndex = leftPosition; 21 | int rightIndex = rightPosition; 22 | 23 | for (int i = 0; i <= endPosition - leftPosition; i++) { 24 | if (leftIndex < rightPosition && 25 | (rightIndex > endPosition || 26 | array[leftIndex] <= array[rightIndex])) { 27 | workArray[i] = array[leftIndex++]; 28 | } else { 29 | workArray[i] = array[rightIndex++]; 30 | } 31 | } 32 | 33 | for (int i = leftPosition; i <= endPosition; i++) { 34 | array[i] = workArray[i - leftPosition]; 35 | } 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /src/com/growingwiththeweb/sorting/MergeSortNatural.java: -------------------------------------------------------------------------------- 1 | package com.growingwiththeweb.sorting; 2 | 3 | import java.util.LinkedList; 4 | import java.util.Queue; 5 | 6 | public class MergeSortNatural { 7 | public static > Queue sort(Queue input) { 8 | Queue output = new LinkedList(); 9 | Queue tempArray1 = new LinkedList(); 10 | Queue tempArray2 = new LinkedList(); 11 | while (input.size() > 0) { 12 | while (input.size() > 0) { 13 | merge(input, output, tempArray1); 14 | merge(input, output, tempArray2); 15 | } 16 | while (tempArray1.size() > 0 || tempArray2.size() > 0) { 17 | merge(tempArray1, tempArray2, output); 18 | merge(tempArray1, tempArray2, input); 19 | } 20 | } 21 | return output; 22 | } 23 | 24 | public static > void merge(Queue left, Queue right, Queue output) { 25 | T prevLeft = null; 26 | T prevRight = null; 27 | while ((left.size() > 0 && (prevLeft == null || prevLeft.compareTo(left.peek()) <= 0)) || 28 | (right.size() > 0 && (prevRight == null || prevRight.compareTo(right.peek()) <= 0))) { 29 | 30 | if (right.size() == 0 || (left.size() > 0 && left.peek().compareTo(right.peek()) <= 0)) { 31 | prevLeft = left.poll(); 32 | output.add(prevLeft); 33 | } else { 34 | prevRight = right.poll(); 35 | output.add(prevRight); 36 | } 37 | } 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /src/com/growingwiththeweb/sorting/OddEvenSort.java: -------------------------------------------------------------------------------- 1 | package com.growingwiththeweb.sorting; 2 | 3 | public class OddEvenSort { 4 | public static > void sort(T[] array) { 5 | boolean sorted = false; 6 | while (!sorted) { 7 | sorted = innerSort(array, 1); 8 | sorted = innerSort(array, 0) && sorted; 9 | } 10 | } 11 | 12 | private static > boolean innerSort(T[] array, Integer i) { 13 | boolean sorted = true; 14 | for (; i < array.length - 1; i += 2) 15 | { 16 | if (array[i].compareTo(array[i + 1]) > 0) 17 | { 18 | swap(array, i, i + 1); 19 | sorted = false; 20 | } 21 | } 22 | return sorted; 23 | } 24 | 25 | private static > void swap( 26 | T[] array, int a, int b) { 27 | T temp = array[a]; 28 | array[a] = array[b]; 29 | array[b] = temp; 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /src/com/growingwiththeweb/sorting/Quicksort.java: -------------------------------------------------------------------------------- 1 | package com.growingwiththeweb.sorting; 2 | 3 | import java.util.Random; 4 | 5 | public class Quicksort { 6 | public static > void sort(T[] array) { 7 | sort(array, 0, array.length - 1); 8 | } 9 | 10 | private static > void sort( 11 | T[] array, int left, int right) { 12 | if (left < right) { 13 | int pivot = partition(array, left, right); 14 | sort(array, left, pivot - 1); 15 | sort(array, pivot + 1, right); 16 | } 17 | } 18 | 19 | private static > int partition( 20 | T[] array, int left, int right) { 21 | T pivot = array[right]; 22 | int mid = left; 23 | for (int i = mid; i < right; i++) { 24 | if (array[i].compareTo(pivot) <= 0) { 25 | swap(array, i, mid++); 26 | } 27 | } 28 | swap(array, right, mid); 29 | return mid; 30 | } 31 | 32 | private static > void swap( 33 | T[] array, int a, int b) { 34 | if (a != b) { 35 | T temp = array[a]; 36 | array[a] = array[b]; 37 | array[b] = temp; 38 | } 39 | } 40 | 41 | 42 | 43 | // Random pivot implementation below 44 | 45 | private static Random random = new Random(); 46 | 47 | public static > void randomSort(T[] array) { 48 | randomSort(array, 0, array.length - 1); 49 | } 50 | 51 | private static > void randomSort( 52 | T[] array, int left, int right) { 53 | if (left < right) { 54 | int pivot = randomPartition(array, left, right); 55 | randomSort(array, left, pivot - 1); 56 | randomSort(array, pivot + 1, right); 57 | } 58 | } 59 | 60 | private static > int randomPartition( 61 | T[] array, int left, int right) { 62 | int pivot = left + random.nextInt(right - left); 63 | swap(array, right, pivot); 64 | return partition(array, left, right); 65 | } 66 | } 67 | -------------------------------------------------------------------------------- /src/com/growingwiththeweb/sorting/RadixSort.java: -------------------------------------------------------------------------------- 1 | package com.growingwiththeweb.sorting; 2 | 3 | public class RadixSort { 4 | public static void sort(Integer[] array) { 5 | RadixSort.sort(array, 10); 6 | } 7 | 8 | public static void sort(Integer[] array, int radix) { 9 | if (array.length == 0) { 10 | return; 11 | } 12 | 13 | // Determine minimum and maximum values 14 | int minValue = array[0]; 15 | int maxValue = array[0]; 16 | for (int i = 1; i < array.length; i++) { 17 | if (array[i] < minValue) { 18 | minValue = array[i]; 19 | } else if (array[i] > maxValue) { 20 | maxValue = array[i]; 21 | } 22 | } 23 | 24 | // Perform counting sort on each exponent/digit, starting at the least 25 | // significant digit 26 | int exponent = 1; 27 | while ((maxValue - minValue) / exponent >= 1) { 28 | RadixSort.countingSortByDigit(array, radix, exponent, minValue); 29 | exponent *= radix; 30 | } 31 | } 32 | 33 | private static void countingSortByDigit( 34 | Integer[] array, int radix, int exponent, int minValue) { 35 | int bucketIndex; 36 | int[] buckets = new int[radix]; 37 | int[] output = new int[array.length]; 38 | 39 | // Initialize bucket 40 | for (int i = 0; i < radix; i++) { 41 | buckets[i] = 0; 42 | } 43 | 44 | // Count frequencies 45 | for (int i = 0; i < array.length; i++) { 46 | bucketIndex = (int)(((array[i] - minValue) / exponent) % radix); 47 | buckets[bucketIndex]++; 48 | } 49 | 50 | // Compute cumulates 51 | for (int i = 1; i < radix; i++) { 52 | buckets[i] += buckets[i - 1]; 53 | } 54 | 55 | // Move records 56 | for (int i = array.length - 1; i >= 0; i--) { 57 | bucketIndex = (int)(((array[i] - minValue) / exponent) % radix); 58 | output[--buckets[bucketIndex]] = array[i]; 59 | } 60 | 61 | // Copy back 62 | for (int i = 0; i < array.length; i++) { 63 | array[i] = output[i]; 64 | } 65 | } 66 | } 67 | -------------------------------------------------------------------------------- /src/com/growingwiththeweb/sorting/SelectionSort.java: -------------------------------------------------------------------------------- 1 | package com.growingwiththeweb.sorting; 2 | 3 | public class SelectionSort { 4 | public static void sort(Integer[] array) { 5 | for (int i = 0; i < array.length - 1; i++) { 6 | int minIndex = i; 7 | for (int j = i + 1; j < array.length; j++) { 8 | if (array[j] < array[minIndex]) { 9 | minIndex = j; 10 | } 11 | } 12 | if (minIndex != i) { 13 | swap(array, i, minIndex); 14 | } 15 | } 16 | } 17 | 18 | private static void swap(Integer[] array, int a, int b) { 19 | Integer temp = array[a]; 20 | array[a] = array[b]; 21 | array[b] = temp; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /test/com/growingwiththeweb/sorting/BaseSortTest.java: -------------------------------------------------------------------------------- 1 | package com.growingwiththeweb.sorting; 2 | 3 | import static org.junit.Assert.assertEquals; 4 | import static org.junit.Assert.assertFalse; 5 | import static org.junit.Assert.assertTrue; 6 | import static org.junit.Assert.assertSame; 7 | 8 | import org.junit.After; 9 | import org.junit.Before; 10 | import org.junit.Test; 11 | import org.junit.Ignore; 12 | import org.junit.runner.RunWith; 13 | import org.junit.runners.JUnit4; 14 | 15 | import java.util.concurrent.Callable; 16 | 17 | public abstract class BaseSortTest { 18 | protected abstract void sort(Integer[] array); 19 | 20 | protected boolean arraysMatch(Integer[] a, Integer[] b) { 21 | if (a.length != b.length) { 22 | return false; 23 | } 24 | for (int i = 0; i < a.length; i++) { 25 | if (!a[i].equals(b[i])) { 26 | System.out.println("arraysMatch failure"); 27 | System.out.println("a = " + a.toString()); 28 | for (int j = 0; j < a.length; j++) { 29 | System.out.print(a[j] + ", "); 30 | } 31 | System.out.println(""); 32 | System.out.println("b = " + b.toString()); 33 | for (int j = 0; j < b.length; j++) { 34 | System.out.print(b[j] + ", "); 35 | } 36 | System.out.println(""); 37 | return false; 38 | } 39 | } 40 | return true; 41 | } 42 | 43 | protected void testSort(Integer[] input, Integer[] expected) { 44 | sort(input); 45 | assertTrue(arraysMatch(input, expected)); 46 | } 47 | 48 | @Test 49 | public void testSortEmptyArray() { 50 | testSort(new Integer[] {}, 51 | new Integer[] {}); 52 | } 53 | 54 | @Test 55 | public void testSortSmallSortedArray() { 56 | testSort(new Integer[] {1,2,3,4,5}, 57 | new Integer[] {1,2,3,4,5}); 58 | } 59 | 60 | @Test 61 | public void testSortSmallReverseSortedArray() { 62 | testSort(new Integer[] {5,4,3,2,1}, 63 | new Integer[] {1,2,3,4,5}); 64 | } 65 | 66 | @Test 67 | public void testSortSmallSortedArrayWithOnlyNegativeValues() { 68 | testSort(new Integer[] {-5,-4,-3,-2,-1}, 69 | new Integer[] {-5,-4,-3,-2,-1}); 70 | } 71 | 72 | @Test 73 | public void testSortSmallReverseSortedArrayWithOnlyNegativeValues() { 74 | testSort(new Integer[] {-1,-2,-3,-4,-5}, 75 | new Integer[] {-5,-4,-3,-2,-1}); 76 | } 77 | 78 | @Test 79 | public void testSortSmallSortedArrayWithTwoValuesSwapped() { 80 | testSort(new Integer[] {1,2,5,4,3}, 81 | new Integer[] {1,2,3,4,5}); 82 | } 83 | 84 | @Test 85 | public void testSortLargeSortedArray() { 86 | testSort(new Integer[] {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20}, 87 | new Integer[] {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20}); 88 | } 89 | 90 | @Test 91 | public void testSortLargeReverseSortedArray() { 92 | testSort(new Integer[] {20,19,18,17,16,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1,0}, 93 | new Integer[] {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20}); 94 | } 95 | 96 | @Test 97 | public void testSortLargeArrayWithTwoValuesSwapped() { 98 | testSort(new Integer[] {0,1,2,8,4,5,6,7,3,9,10,11,12,13,14,15,16,17,18,19,20}, 99 | new Integer[] {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20}); 100 | } 101 | 102 | @Test 103 | public void testSortLargeSortedArrayWithOnlyNegativeValues() { 104 | testSort(new Integer[] {-20,-19,-18,-17,-16,-15,-14,-13,-12,-11,-10,-9,-8,-7,-6,-5,-4,-3,-2,-1}, 105 | new Integer[] {-20,-19,-18,-17,-16,-15,-14,-13,-12,-11,-10,-9,-8,-7,-6,-5,-4,-3,-2,-1}); 106 | } 107 | 108 | @Test 109 | public void testSortLargeReverseSortedArrayWithOnlyNegativeValues() { 110 | testSort(new Integer[] {-1,-2,-3,-4,-5,-6,-7,-8,-9,-10,-11,-12,-13,-14,-15,-16,-17,-18,-19,-20}, 111 | new Integer[] {-20,-19,-18,-17,-16,-15,-14,-13,-12,-11,-10,-9,-8,-7,-6,-5,-4,-3,-2,-1}); 112 | } 113 | 114 | @Test 115 | public void testSortJumbledArrayWithSmallRangeOfValues() { 116 | testSort(new Integer[] {5,-3,2,0,-5,1,4,-4,-2,3,-1}, 117 | new Integer[] {-5,-4,-3,-2,-1,0,1,2,3,4,5}); 118 | } 119 | 120 | @Test 121 | public void testSortJumbledArrayWithLargeRangeOfValues() { 122 | testSort(new Integer[] {102,10,-50,2938,108,-4011,-38,523,16}, 123 | new Integer[] {-4011,-50,-38,10,16,102,108,523,2938}); 124 | } 125 | 126 | @Test 127 | public void testSortArrayWithDuplicateValues() { 128 | testSort(new Integer[] {-2,-7,1,9,-7,1,-2,10,-7,-7}, 129 | new Integer[] {-7,-7,-7,-7,-2,-2,1,1,9,10}); 130 | } 131 | } 132 | -------------------------------------------------------------------------------- /test/com/growingwiththeweb/sorting/BubbleSortOptimisedTest.java: -------------------------------------------------------------------------------- 1 | package com.growingwiththeweb.sorting; 2 | 3 | public class BubbleSortOptimisedTest extends BaseSortTest { 4 | protected void sort(Integer[] array) { 5 | BubbleSortOptimised.sort(array); 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /test/com/growingwiththeweb/sorting/BubbleSortTest.java: -------------------------------------------------------------------------------- 1 | package com.growingwiththeweb.sorting; 2 | 3 | public class BubbleSortTest extends BaseSortTest { 4 | protected void sort(Integer[] array) { 5 | BubbleSort.sort(array); 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /test/com/growingwiththeweb/sorting/BucketSortTest.java: -------------------------------------------------------------------------------- 1 | package com.growingwiththeweb.sorting; 2 | 3 | public class BucketSortTest extends BaseSortTest { 4 | protected void sort(Integer[] array) { 5 | BucketSort.sort(array); 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /test/com/growingwiththeweb/sorting/CocktailSortTest.java: -------------------------------------------------------------------------------- 1 | package com.growingwiththeweb.sorting; 2 | 3 | public class CocktailSortTest extends BaseSortTest { 4 | protected void sort(Integer[] array) { 5 | CocktailSort.sort(array); 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /test/com/growingwiththeweb/sorting/CombSortTest.java: -------------------------------------------------------------------------------- 1 | package com.growingwiththeweb.sorting; 2 | 3 | public class CombSortTest extends BaseSortTest { 4 | protected void sort(Integer[] array) { 5 | CombSort.sort(array); 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /test/com/growingwiththeweb/sorting/CountingSortTest.java: -------------------------------------------------------------------------------- 1 | package com.growingwiththeweb.sorting; 2 | 3 | public class CountingSortTest extends BaseSortTest { 4 | protected void sort(Integer[] array) { 5 | CountingSort.sort(array); 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /test/com/growingwiththeweb/sorting/GnomeSortTest.java: -------------------------------------------------------------------------------- 1 | package com.growingwiththeweb.sorting; 2 | 3 | public class GnomeSortTest extends BaseSortTest { 4 | protected void sort(Integer[] array) { 5 | GnomeSort.sort(array); 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /test/com/growingwiththeweb/sorting/HeapsortTest.java: -------------------------------------------------------------------------------- 1 | package com.growingwiththeweb.sorting; 2 | 3 | public class HeapsortTest extends BaseSortTest { 4 | protected void sort(Integer[] array) { 5 | Heapsort.sort(array); 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /test/com/growingwiththeweb/sorting/InsertionSortTest.java: -------------------------------------------------------------------------------- 1 | package com.growingwiththeweb.sorting; 2 | 3 | public class InsertionSortTest extends BaseSortTest { 4 | protected void sort(Integer[] array) { 5 | InsertionSort.sort(array); 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /test/com/growingwiththeweb/sorting/MergeSortBottomUpTest.java: -------------------------------------------------------------------------------- 1 | package com.growingwiththeweb.sorting; 2 | 3 | public class MergeSortBottomUpTest extends BaseSortTest { 4 | protected void sort(Integer[] array) { 5 | MergeSortBottomUp.sort(array); 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /test/com/growingwiththeweb/sorting/MergeSortNaturalTest.java: -------------------------------------------------------------------------------- 1 | package com.growingwiththeweb.sorting; 2 | 3 | import java.util.LinkedList; 4 | import java.util.Queue; 5 | 6 | import org.junit.Ignore; 7 | 8 | @Ignore 9 | public class MergeSortNaturalTest extends BaseSortTest { 10 | protected void sort(Integer[] array) { 11 | Queue input = new LinkedList(); 12 | for (int i = 0; i < array.length; i++) { 13 | input.offer(array[i]); 14 | } 15 | Queue output = MergeSortNatural.sort(input); 16 | for (int i = 0; i < array.length; i++) { 17 | array[i] = output.poll(); 18 | } 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /test/com/growingwiththeweb/sorting/MergeSortTest.java: -------------------------------------------------------------------------------- 1 | package com.growingwiththeweb.sorting; 2 | 3 | public class MergeSortTest extends BaseSortTest { 4 | protected void sort(Integer[] array) { 5 | Integer[] output = MergeSort.sort(array); 6 | for (int i = 0; i < output.length; i++) { 7 | array[i] = output[i]; 8 | } 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /test/com/growingwiththeweb/sorting/OddEvenSortTest.java: -------------------------------------------------------------------------------- 1 | package com.growingwiththeweb.sorting; 2 | 3 | public class OddEvenSortTest extends BaseSortTest { 4 | protected void sort(Integer[] array) { 5 | OddEvenSort.sort(array); 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /test/com/growingwiththeweb/sorting/QuicksortRandomTest.java: -------------------------------------------------------------------------------- 1 | package com.growingwiththeweb.sorting; 2 | 3 | public class QuicksortRandomTest extends BaseSortTest { 4 | protected void sort(Integer[] array) { 5 | Quicksort.randomSort(array); 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /test/com/growingwiththeweb/sorting/QuicksortTest.java: -------------------------------------------------------------------------------- 1 | package com.growingwiththeweb.sorting; 2 | 3 | public class QuicksortTest extends BaseSortTest { 4 | protected void sort(Integer[] array) { 5 | Quicksort.sort(array); 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /test/com/growingwiththeweb/sorting/RadixSortTest.java: -------------------------------------------------------------------------------- 1 | package com.growingwiththeweb.sorting; 2 | 3 | public class RadixSortTest extends BaseSortTest { 4 | protected void sort(Integer[] array) { 5 | RadixSort.sort(array); 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /test/com/growingwiththeweb/sorting/SelectionSortTest.java: -------------------------------------------------------------------------------- 1 | package com.growingwiththeweb.sorting; 2 | 3 | public class SelectionSortTest extends BaseSortTest { 4 | protected void sort(Integer[] array) { 5 | SelectionSort.sort(array); 6 | } 7 | } 8 | --------------------------------------------------------------------------------