diff --git a/.idea/misc.xml b/.idea/misc.xml index 4b49563..bc8d0a3 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,7 +1,7 @@ - + \ No newline at end of file diff --git a/DIRECTORY.md b/DIRECTORY.md index 17951f0..25168be 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -37,6 +37,7 @@ * [Mergesort](https://github.com/TheAlgorithms/Kotlin/blob/master/src/main/kotlin/sort/MergeSort.kt) * [Quicksort](https://github.com/TheAlgorithms/Kotlin/blob/master/src/main/kotlin/sort/QuickSort.kt) * [Selectionsort](https://github.com/TheAlgorithms/Kotlin/blob/master/src/main/kotlin/sort/SelectionSort.kt) + * [Shellsort](https://github.com/TheAlgorithms/Kotlin/blob/master/src/main/kotlin/sort/ShellSort.kt) * Test * Dynamic Programming * [Palindromepartitioningtest](https://github.com/TheAlgorithms/Kotlin/blob/master/src/test/kotlin/dynamic_programming/PalindromePartitioningTest.kt) @@ -72,3 +73,4 @@ * [Mergesorttest](https://github.com/TheAlgorithms/Kotlin/blob/master/src/test/kotlin/sort/MergeSortTest.kt) * [Quicksorttest](https://github.com/TheAlgorithms/Kotlin/blob/master/src/test/kotlin/sort/QuickSortTest.kt) * [Selectionsorttest](https://github.com/TheAlgorithms/Kotlin/blob/master/src/test/kotlin/sort/SelectionSortTest.kt) + * [ShellSsorttest](https://github.com/TheAlgorithms/Kotlin/blob/master/src/test/kotlin/sort/ShellSortTest.kt) \ No newline at end of file diff --git a/src/main/kotlin/sort/ShellSort.kt b/src/main/kotlin/sort/ShellSort.kt new file mode 100644 index 0000000..16cce73 --- /dev/null +++ b/src/main/kotlin/sort/ShellSort.kt @@ -0,0 +1,30 @@ +package sort + +/** + * This method implements the Shell Sort + * + * @param array The array to be sorted + * @param gaps The gap sequence to be used. Default is [701, 301, 132, 57, 23, 10, 4, 1] (Marcin Ciura's gap sequence) + * Sorts the array by decreasing the gap over which it compares elements after each iteration + * + * Worst-case performance O(n^2) (worst known worst case gap sequence) + * O(n log^2 n) (best known worst case gap sequence) + * Best-case performance O(n log n) (most gap sequences) + * O(n log^2 n) (best known worst-case gap sequence) + * Average performance depends on gap sequence + * Worst-case space complexity O(1) + **/ +fun > shellSort(array: Array, gaps: Array = arrayOf(701, 301, 132, 57, 23, 10, 4, 1)) { + for (gap in gaps) { + if (gap >= array.size) + continue + + for (i in gap until array.size) { + for (j in i downTo gap step gap) { + if (array[j] < array[j - gap]) { + swapElements(array, j, j - gap) + } + } + } + } +} diff --git a/src/test/kotlin/sort/ShellSortTest.kt b/src/test/kotlin/sort/ShellSortTest.kt new file mode 100644 index 0000000..0bc577c --- /dev/null +++ b/src/test/kotlin/sort/ShellSortTest.kt @@ -0,0 +1,31 @@ +package sort + +import org.junit.Assert.assertArrayEquals +import org.junit.Test + +class ShellSortTest { + + @Test + fun testShellSort1() { + val array = arrayOf(4, 3, 2, 8, 1) + shellSort(array) + + assertArrayEquals(array, arrayOf(1, 2, 3, 4, 8)) + } + + @Test + fun testShellSort2() { + val array = arrayOf(20, 5, 16, -1, 6) + shellSort(array) + + assertArrayEquals(array, arrayOf(-1, 5, 6, 16, 20)) + } + + @Test + fun testShellSort3() { + val array = arrayOf("A", "D", "E", "C", "B") + shellSort(array) + + assertArrayEquals(array, arrayOf("A", "B", "C", "D", "E")) + } +} \ No newline at end of file