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 b50e320

Browse files
Quick sort and refactor
1 parent c272280 commit b50e320

29 files changed

+181
-129
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package pl.dmichalski.algorithms._9_sorting
2+
3+
interface SortService {
4+
5+
fun sort(values: IntArray): IntArray
6+
7+
}

‎src/main/kotlin/pl/dmichalski/algorithms/_9_sorting/bubble/BubbleSortOptimizedService.kt‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
package pl.dmichalski.algorithms._9_sorting.bubble
22

3-
internal class BubbleSortOptimizedService {
3+
import pl.dmichalski.algorithms._9_sorting.SortService
4+
5+
internal class BubbleSortOptimizedService : SortService {
46

57
/**
68
* O(n2) time complexity
9+
* O(1) space complexity
710
*/
8-
fun sort(values: IntArray): IntArray? {
11+
overridefun sort(values: IntArray): IntArray {
912
val n = values.size
1013
for (i in 0 until n - 1) {
1114
var noSwaps = true

‎src/main/kotlin/pl/dmichalski/algorithms/_9_sorting/bubble/BubbleSortService.kt‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
package pl.dmichalski.algorithms._9_sorting.bubble
22

3-
internal class BubbleSortService {
3+
import pl.dmichalski.algorithms._9_sorting.SortService
4+
5+
internal class BubbleSortService : SortService {
46

57
/**
68
* O(n2) time complexity
9+
* O(1) space complexity
710
*/
8-
fun sort(values: IntArray): IntArray {
11+
overridefun sort(values: IntArray): IntArray {
912
val n = values.size
1013
for (i in 0 until n - 1) {
1114
for (j in 0 until n - i - 1) {

‎src/main/kotlin/pl/dmichalski/algorithms/_9_sorting/insertion/InsertionSortService.kt‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
package pl.dmichalski.algorithms._9_sorting.insertion
22

3-
internal class InsertionSortService {
3+
import pl.dmichalski.algorithms._9_sorting.SortService
4+
5+
internal class InsertionSortService : SortService {
46

57
/**
68
* O(n2) time complexity
79
* O(1) space complexity
810
*/
9-
fun sort(values: IntArray): IntArray {
11+
overridefun sort(values: IntArray): IntArray {
1012
for (i in 1 until values.size) {
1113
val currentVal = values[i]
1214
var j: Int = i - 1

‎src/main/kotlin/pl/dmichalski/algorithms/_9_sorting/merge/MergeSortService.kt‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
package pl.dmichalski.algorithms._9_sorting.merge
22

3-
internal class MergeSortService {
3+
import pl.dmichalski.algorithms._9_sorting.SortService
4+
5+
internal class MergeSortService : SortService {
46

57
/**
68
* O(n log n) time complexity
79
* O(n) space complexity
810
*/
9-
fun sort(values: IntArray): IntArray {
11+
overridefun sort(values: IntArray): IntArray {
1012
if (values.size <= 1) {
1113
return values
1214
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package pl.dmichalski.algorithms._9_sorting.quick
2+
3+
internal class QuickSortService {
4+
5+
/**
6+
* O(n2) time complexity
7+
* O(n log n) space complexity
8+
*/
9+
fun sort(values: IntArray, left: Int? = 0, right: Int? = values.size): IntArray {
10+
if (left!! < right!!) {
11+
val pivotIndex = pivot(values, left, right)
12+
sort(values, left, pivotIndex)
13+
sort(values, pivotIndex + 1, right)
14+
}
15+
return values
16+
}
17+
18+
private fun pivot(values: IntArray, start: Int, end: Int): Int {
19+
val pivot = values[start]
20+
var swapIndex = start
21+
22+
for (i in start + 1 until end) {
23+
if (pivot > values[i]) {
24+
swapIndex++
25+
swap(values, swapIndex, i)
26+
}
27+
}
28+
swap(values, start, swapIndex)
29+
return swapIndex
30+
}
31+
32+
private fun swap(values: IntArray, x: Int, y: Int) {
33+
val temp = values[x]
34+
values[x] = values[y]
35+
values[y] = temp
36+
}
37+
38+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package pl.dmichalski.algorithms._9_sorting.quick
2+
3+
/**
4+
* Write a function called sort, that accepts integer array
5+
* and returns sorted array using quick sort algorithm.
6+
*/
7+
object Runner {
8+
9+
@JvmStatic
10+
fun main(args: Array<String>) {
11+
val values = intArrayOf(3, 2, 5, 1, 4, 2, 22, -2)
12+
13+
val sortService = QuickSortService()
14+
15+
val result = sortService.sort(values)
16+
17+
println("Before sorting: ${values.contentToString()}")
18+
println("After sorting with quick sort: ${result.contentToString()}")
19+
}
20+
21+
}

‎src/main/kotlin/pl/dmichalski/algorithms/_9_sorting/selection/SelectionSortService.kt‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
package pl.dmichalski.algorithms._9_sorting.selection
22

3-
class SelectionSortService {
3+
import pl.dmichalski.algorithms._9_sorting.SortService
4+
5+
class SelectionSortService : SortService {
46

57
/**
68
* O(n2) time complexity
9+
* O(1) space complexity
710
*/
8-
fun sort(values: IntArray): IntArray {
11+
overridefun sort(values: IntArray): IntArray {
912
val n = values.size
1013
for (i in 0 until n) {
1114
var lowestIndex = i

‎src/test/groovy/pl/dmichalski/algorithms/_1_sum_from_1_to_n/SumCounterService1Spec.groovy‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import spock.lang.Unroll
55

66
class SumCounterService1Spec extends Specification {
77

8-
private SumCounterService1 underTest = new SumCounterService1()
8+
private finalSumCounterService1 underTest = new SumCounterService1()
99

1010
@Unroll
1111
def 'should return (expectedResult=#expectedResult) for (n=#n)'() {

‎src/test/groovy/pl/dmichalski/algorithms/_1_sum_from_1_to_n/SumCounterService2Spec.groovy‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import spock.lang.Unroll
55

66
class SumCounterService2Spec extends Specification {
77

8-
private SumCounterService2 underTest = new SumCounterService2()
8+
private finalSumCounterService2 underTest = new SumCounterService2()
99

1010
@Unroll
1111
def 'should return (expectedResult=#expectedResult) for (n=#n)'() {

0 commit comments

Comments
(0)

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