Skip to content

Add shellsort #90

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions DIRECTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
30 changes: 30 additions & 0 deletions src/main/kotlin/sort/ShellSort.kt
Original file line number Diff line number Diff line change
@@ -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 <T : Comparable<T>> shellSort(array: Array<T>, gaps: Array<Int> = 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)
}
}
}
}
}
31 changes: 31 additions & 0 deletions src/test/kotlin/sort/ShellSortTest.kt
Original file line number Diff line number Diff line change
@@ -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"))
}
}