Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 5129eca

Browse files
refactored InsertionSort, SelectionSort, CountingSort and RadixSort algorithms
1 parent 0591b75 commit 5129eca

File tree

13 files changed

+224
-332
lines changed

13 files changed

+224
-332
lines changed

‎README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ Content:
6464
* [Selection sort](/src/main/kotlin/sorting/SelectionSort.kt)
6565
* [Radix sort](/src/main/kotlin/sorting/RadixSort.kt)
6666
* [Tim sort](/src/main/kotlin/sorting/TimSort.kt)
67-
* [Count sort](/src/main/kotlin/sorting/CountSort.kt)
67+
* [Count sort](/src/main/kotlin/sorting/CountingSort.kt)
6868

6969
4. package <code>search</code> - search algorithms
7070
* [Binary search](/src/main/kotlin/search/BinarySearch.kt)

‎README_ru.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@
6464
* [Сортировка выбором](/src/main/kotlin/sorting/SelectionSort.kt)
6565
* [Поразрядная сортировка](/src/main/kotlin/sorting/RadixSort.kt)
6666
* [Tim sort](/src/main/kotlin/sorting/TimSort.kt)
67-
* [Сортировка подсчётом](/src/main/kotlin/sorting/CountSort.kt)
67+
* [Сортировка подсчётом](/src/main/kotlin/sorting/CountingSort.kt)
6868

6969
4. пакет <code>ru.search</code> - алгоритмы поиска
7070
* [Бинарный поиск](/src/main/kotlin/search/BinarySearch.kt)

‎src/main/kotlin/sorting/CountSort.kt

Lines changed: 0 additions & 36 deletions
This file was deleted.

‎src/main/kotlin/sorting/CountingSort.kt

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package sorting
2+
3+
/**
4+
*
5+
* Counting sort is a sorting technique based on keys between a specific range
6+
*
7+
* worst time: n
8+
* best time: n
9+
* average time: n
10+
*
11+
* amount of memory: equals to the size of the range of numbers plus 1 (for example: 1001 for numbers from 0 to 1000)
12+
*
13+
* P.S. The use of counting sort is useful only when the sorted numbers have (or can be mapped to) a range of possible
14+
* values that is small enough compared to the sorted set, for example, a million natural numbers less than 1000
15+
*
16+
*/
17+
18+
class CountingSort {
19+
20+
// sorts numbers in the range from start to end
21+
fun sort(array: Array<Int>, start: Int, end: Int) {
22+
val arraySize = array.size
23+
val countedNumbers = Array(end + 1) { 0 }
24+
25+
var index = 0
26+
while (index < arraySize) {
27+
countedNumbers[array[index]]++
28+
index++
29+
}
30+
31+
index = 0
32+
var currentNumber = start
33+
while (currentNumber < countedNumbers.size) {
34+
var frequency = countedNumbers[currentNumber]
35+
while (frequency > 0) {
36+
array[index++] = currentNumber
37+
frequency--
38+
}
39+
currentNumber++
40+
}
41+
}
42+
43+
}
Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,34 @@
11
package sorting
22

33
/**
4-
* insertion sort algorithm
4+
*
5+
* Insertion sort is a simple sorting algorithm that works similar to the way you sort playing cards in your hands.
6+
*
7+
* The array is virtually split into a sorted and an unsorted part.
8+
*
9+
* Values from the unsorted part are picked and placed at the correct position in the sorted part.
510
*
611
* worst time: n2
7-
* the best time: n
12+
* best time: n
813
* average time: n2
914
*
1015
* amount of time: 1
16+
*
1117
*/
1218

13-
fun <T : Comparable<T>> Array<T>.insertionSort() {
14-
val array = this
15-
for (i in 1 until size) {
16-
val current = array[i]
17-
var j = i - 1
18-
while (j >= 0 && array[j] > current) {
19-
array[j + 1] = array[j]
20-
j--
21-
}
22-
array[j + 1] = current
23-
}
24-
}
19+
class InsertionSort {
2520

26-
fun <T : Comparable<T>> MutableList<T>.insertionSort() {
27-
val list = this
28-
for (i in 1 until size) {
29-
val current = list[i]
30-
var j = i - 1
31-
while (j >= 0 && list[j] > current) {
32-
list[j + 1] = list[j]
33-
j--
21+
fun <T : Comparable<T>> sort(array: Array<T>) {
22+
val arraySize = array.size
23+
for (i in 1 until arraySize) {
24+
val current = array[i]
25+
var j = i - 1
26+
while (j >= 0 && array[j] > current) {
27+
array[j + 1] = array[j]
28+
j--
29+
}
30+
array[j + 1] = current
3431
}
35-
list[j + 1] = current
3632
}
33+
3734
}

‎src/main/kotlin/sorting/MergeSort.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ package sorting
1616
class MergeSort {
1717

1818
/**
19+
* worst time: n * log(n)
1920
* best time: n * log(n)
2021
* average time: n * log(n)
21-
* worst time: n * log(n)
2222
*
2323
* amount of memory: n
2424
*/
@@ -76,9 +76,9 @@ class MergeSort {
7676
}
7777

7878
/**
79+
* worst time: n * log(n)
7980
* best time: n * log(n)
8081
* average time: n * log(n)
81-
* worst time: n * log(n)
8282
*
8383
* amount of memory: n * log(n)
8484
*/

‎src/main/kotlin/sorting/RadixSort.kt

Lines changed: 26 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,65 +1,44 @@
11
package sorting
22

33
/**
4-
* radix sort algorithm applies only to integers
4+
*
5+
* Radix Sort is a linear sorting algorithm that sorts elements by processing them digit by digit.
6+
*
7+
* It is an efficient sorting algorithm for integers or strings with fixed-size keys.
58
*
69
* worst time: <number of bits of an integer> * n
710
*
811
* amount of memory: 2 * n
912
*/
1013

11-
fun Array<Int>.radixSort() {
12-
val array = this
13-
val array1 = Array(size) { 0 }
14-
val array2 = Array(size) { 0 }
15-
16-
val size = Int.SIZE_BITS
17-
18-
for (radix in 0 until size) {
19-
var size1 = 0
20-
var size2 = 0
21-
for (index in array.indices) {
22-
if (array[index].and(1.shl(radix)) == 0) {
23-
array1[size1++] = array[index]
24-
} else {
25-
array2[size2++] = array[index]
26-
}
27-
}
28-
29-
for (index in 0 until size1) {
30-
array[index] = array1[index]
31-
}
32-
33-
for (index in 0 until size2) {
34-
array[size1 + index] = array2[index]
35-
}
36-
}
37-
}
14+
class RadixSort {
3815

39-
fun MutableList<Int>.radixSort() {
40-
val list = this
41-
val array1 = MutableList(size) { 0 }
42-
val array2 = MutableList(size) { 0 }
16+
fun sort(array:Array<Int>) {
17+
val arraySize = array.size
18+
val arrayOfZeroBits = Array(arraySize) { 0 }
19+
val arrayOfOneBits = Array(arraySize) { 0 }
4320

44-
val size = Int.SIZE_BITS
21+
val size = Int.SIZE_BITS
4522

46-
for (radix in 0 until size) {
47-
var size1 = 0
48-
var size2 = 0
49-
for (index in list.indices) {
50-
if (list[index].and(1.shl(radix)) == 0) {
51-
array1[size1++] = list[index]
52-
} else {
53-
array2[size2++] = list[index]
23+
for (radix in 0 until size) {
24+
var size1 = 0
25+
var size2 = 0
26+
for (index in array.indices) {
27+
if (array[index].and(1 shl radix) == 0) {
28+
arrayOfZeroBits[size1++] = array[index]
29+
} else {
30+
arrayOfOneBits[size2++] = array[index]
31+
}
5432
}
55-
}
5633

57-
for (index in 0 until size1) {
58-
list[index] = array1[index]
59-
}
34+
for (index in 0 until size1) {
35+
array[index] = arrayOfZeroBits[index]
36+
}
6037

61-
for (index in 0 until size2) {
62-
list[size1 + index] = array2[index]
38+
for (index in 0 until size2) {
39+
array[size1 + index] = arrayOfOneBits[index]
40+
}
6341
}
6442
}
43+
6544
}
Lines changed: 21 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,38 @@
11
package sorting
22

33
/**
4-
* selection sort algorithm
4+
*
5+
* Selection sort is a simple and efficient sorting algorithm that works by repeatedly selecting
6+
*
7+
* the smallest (or largest) element from the unsorted portion of the list and
8+
*
9+
* moving it to the sorted portion of the list.
510
*
611
* worst time: n2
7-
* the best time: n2
12+
* best time: n2
813
* average time: n2
914
*
1015
* amount of memory: 1
16+
*
1117
*/
1218

13-
fun <T : Comparable<T>> Array<T>.selectionSort() {
14-
val array = this
15-
for (i in 0 until size - 1) {
16-
var min = i
17-
for (j in i + 1 until size) {
18-
if (array[min] > array[j]) {
19-
min = j
19+
class SelectionSort {
20+
21+
fun <T : Comparable<T>> sort(array: Array<T>) {
22+
val arraySize = array.size
23+
for (i in 0 until arraySize - 1) {
24+
var min = i
25+
for (j in i + 1 until arraySize) {
26+
if (array[min] > array[j]) {
27+
min = j
28+
}
2029
}
21-
}
22-
if (min != i) {
23-
array[min] = array[i].apply {
30+
if (min != i) {
31+
val tmp = array[i]
2432
array[i] = array[min]
33+
array[min] = tmp
2534
}
2635
}
2736
}
28-
}
2937

30-
fun <T : Comparable<T>> MutableList<T>.selectionSort() {
31-
val list = this
32-
for (i in 0 until size - 1) {
33-
var min = i
34-
for (j in i + 1 until size) {
35-
if (list[min] > list[j]) {
36-
min = j
37-
}
38-
}
39-
if (min != i) {
40-
list[min] = list[i].apply {
41-
list[i] = list[min]
42-
}
43-
}
44-
}
4538
}

‎src/test/kotlin/sorting/CountSortTest.kt

Lines changed: 0 additions & 38 deletions
This file was deleted.

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /