Skip to content

Commit f8398e1

Browse files
Refactor
1 parent 5b9f2de commit f8398e1

File tree

3 files changed

+18
-25
lines changed

3 files changed

+18
-25
lines changed

pom.xml

+2-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
<groovy-all.version>2.4.11</groovy-all.version>
1717
<maven-surefire-plugin.version>2.22.2</maven-surefire-plugin.version>
1818
<gmavenplus-plugin.version>1.10.0</gmavenplus-plugin.version>
19-
<kotlin-maven-plugin.jvmTarget>11</kotlin-maven-plugin.jvmTarget>
19+
<kotlin-maven-plugin.jvmTarget>1.8</kotlin-maven-plugin.jvmTarget>
2020
</properties>
2121

2222
<dependencies>
@@ -69,6 +69,7 @@
6969
</execution>
7070
</executions>
7171
</plugin>
72+
7273
<plugin>
7374
<groupId>org.jetbrains.kotlin</groupId>
7475
<artifactId>kotlin-maven-plugin</artifactId>

src/main/kotlin/pl/dmichalski/algorithms/_9_sorting/quick/QuickSortService.kt

+11-4
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,25 @@
11
package pl.dmichalski.algorithms._9_sorting.quick
22

3+
import pl.dmichalski.algorithms._9_sorting.SortService
4+
35
/**
46
* Quick sort algorithm.
57
*
68
* O(n2) time complexity
79
* O(n log n) space complexity
810
*/
9-
internal class QuickSortService {
11+
internal class QuickSortService : SortService {
12+
13+
override fun sort(values: IntArray): IntArray {
14+
sortArray(values)
15+
return values
16+
}
1017

11-
fun sort(values: IntArray, left: Int? = 0, right: Int? = values.size): IntArray {
18+
private fun sortArray(values: IntArray, left: Int? = 0, right: Int? = values.size): IntArray {
1219
if (left!! < right!!) {
1320
val pivotIndex = pivot(values, left, right)
14-
sort(values, left, pivotIndex)
15-
sort(values, pivotIndex + 1, right)
21+
sortArray(values, left, pivotIndex)
22+
sortArray(values, pivotIndex + 1, right)
1623
}
1724
return values
1825
}
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,12 @@
11
package pl.dmichalski.algorithms._9_sorting.quick
22

3-
import spock.lang.Specification
4-
import spock.lang.Unroll
3+
import pl.dmichalski.algorithms._9_sorting.BaseSortServiceSpec
54

6-
class QuickSortServiceSpec extends Specification {
5+
class QuickSortServiceSpec extends BaseSortServiceSpec {
76

8-
private final QuickSortService underTest = new QuickSortService()
7+
void setup() {
8+
super.setUnderTest(new QuickSortService())
99

10-
@Unroll
11-
def 'should return [expectedResult=#expectedResult] for [values=#values]'() {
12-
when:
13-
def result = underTest.sort(values as int[], 0, values.size())
14-
15-
then:
16-
result == expectedResult as int[]
17-
18-
where:
19-
values | expectedResult
20-
[] | []
21-
[1] | [1]
22-
[1, 2] | [1, 2]
23-
[3, 2, 12, 5] | [2, 3, 5, 12]
24-
[1, 2, 10, 12, 4, 5, 1] | [1, 1, 2, 4, 5, 10, 12]
25-
[2, 3, 4, 5, 8, 12, 25, 150, 1000, 50] | [2, 3, 4, 5, 8, 12, 25, 50, 150, 1000]
2610
}
11+
2712
}

0 commit comments

Comments
 (0)