изменение интерфейса SortAlgo на абстрактный класс, добавление тестов для bubble sort

This commit is contained in:
Dmitry
2021-10-18 12:21:31 +06:00
parent cff66cc24f
commit cabe43a189
61 changed files with 49 additions and 13 deletions
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
+1
View File
@@ -1,5 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="GradleMigrationSettings" migrationVersion="1" />
<component name="GradleSettings">
<option name="linkedExternalProjectsSettings">
<GradleProjectSettings>
+2
View File
@@ -11,4 +11,6 @@ repositories {
dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib"
implementation 'junit:junit:4.13.1'
implementation 'org.junit.jupiter:junit-jupiter:5.7.0'
}
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -1,2 +1,2 @@
7
4
3
0
Binary file not shown.
+2 -8
View File
@@ -1,14 +1,8 @@
package sorting
class BubbleSort<T : Comparable<T>> : SortAlgo<T> {
class BubbleSort<T : Comparable<T>> : SortAlgo<T>() {
override fun sort(array: Array<T>) : Array<T> {
val newArray = array.copyOf()
bubbleSort(newArray)
return newArray
}
private fun bubbleSort(array: Array<T>) {
override fun sortAlgo(array: Array<T>) {
for (i in 0 until array.size - 1) {
for (j in 0 until array.size - 1 - i) {
if (array[j] > array[j + 1]) {
+9 -3
View File
@@ -1,5 +1,11 @@
package sorting
interface SortAlgo<T : Comparable<T>> {
fun sort(array: Array<T>) : Array<T>
}
abstract class SortAlgo<T : Comparable<T>> {
fun sort(array: Array<T>) : Array<T> {
val copy = array.copyOf()
sortAlgo(copy)
return copy
}
abstract fun sortAlgo(array: Array<T>)
}
+33
View File
@@ -0,0 +1,33 @@
package sorting
import org.junit.Test
import org.junit.Assert.assertEquals
import org.junit.jupiter.api.Assertions.*
import kotlin.random.Random
internal class BubbleSortTest {
private val bubbleSort = BubbleSort<Int>()
@Test
fun test_reversed_array() {
val sortedArray = arrayOf(1, 20, 30, 40)
val reversedArray = sortedArray.reversedArray()
assertEquals(bubbleSort.sort(reversedArray).toList(), sortedArray.toList())
}
@Test
fun test_random_array() {
val array = arrayOf(Random.nextInt(100), Random.nextInt(100), Random.nextInt(100), Random.nextInt(100))
val sortedArray = array.sorted()
assertEquals(bubbleSort.sort(array).toList(), sortedArray.toList())
}
@Test
fun test_shuffled_array() {
val sortedArray = arrayOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
val shuffledArray = sortedArray.copyOf()
shuffledArray.shuffle()
assertEquals(bubbleSort.sort(shuffledArray).toList(), sortedArray.toList())
}
}