Skip to content

Commit 01b8cbc

Browse files
committed
Added selection sort
1 parent 4141e99 commit 01b8cbc

File tree

14 files changed

+85
-45
lines changed

14 files changed

+85
-45
lines changed

README.md

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,19 +22,17 @@ by checking out this the project from git repository.
2222
ice preview of markdown files (files containing puzzles description)
2323

2424
## Puzzle structure
25-
Each puzzle is located in separate package and it contains 3 files
26-
([screen](./misc/images/SampleTask.png)):
25+
Each puzzle is located in separate package and it contains 3 files ([screen](./misc/image/SampleTask.png)):
2726
* `Puzzle.md` - description of the puzzle
2827
* `Puzzle.kt` - contains empty method/class where puzzle should be solved and set of tests to run
2928
* `PuzzleSolution.kt - file that contains one or more puzzle solutions (encapsulated in Kotlin `object` just to avoid conflicts with
3029
other coding puzzles)
3130

3231
## Running tests
3332

34-
Open `Puzzle.kt` file for a any puzzle. Locate class with `Test` suffix (`PuzzleTest`). Click on the green green arrow close to line
35-
number to run one or more tests in the class
36-
([screen](./misc/images/RunTest.png)). After running the test you can rerun last
37-
configuration using `Run command` (`⌘ + R`).
33+
Open `Puzzle.kt` file for a any puzzle. Locate class with `Test` suffix (`PuzzleTest`). Click on the green green arrow close to line number
34+
to run one or more tests in the class ([screen](./misc/image/RunTest.png)). After running the test you can rerun last configuration using
35+
`Run command` (`⌘ + R`).
3836

3937
Larger puzzles will have larger test base, so tests have to be uncommented one by one while solving various steps of the given puzzle.
4038

@@ -65,6 +63,7 @@ same tasks multiple times be patient and persistent over time.
6563
**List**
6664
* [List chunking](app/src/test/java/com/igorwojda/codingpuzzle/listchunk/ListChunk.md)
6765
* [Bubble sort](app/src/test/java/com/igorwojda/datastructure/list/bubblesort/BubbleSort.md)
66+
* [Selection sort](app/src/test/java/com/igorwojda/datastructure/list/selectionsort/SelectionSort.md)
6867

6968
**Queue**
7069
* [Int queue](app/src/test/java/com/igorwojda/datastructure/queue/int/IntQueue.md)
@@ -127,6 +126,6 @@ If you think something is incorrect, you have found a new puzzle, have a better
127126
please create PR or open a new issue.
128127

129128
# Follow me
130-
![avatar.png](misc/images/avatar.png)
129+
![avatar.png](misc/image/avatar.png)
131130

132131
[Twitter](https://twitter.com/igorwojda) | [Medium](https://medium.com/@igorwojda) | [Linkedin](https://www.linkedin.com/in/igorwojda/)

app/src/test/java/com/igorwojda/datastructure/list/bubblesort/BubbleSort.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
Sort list of numbers from lowest number to greatest number using [bubble sort](https://en.wikipedia.org/wiki/Bubble_sort).
88

9+
Algorithm:
910
Starting from the beginning of the list, compare every adjacent pair, swap their position if they are not in the right order (the latter one
1011
is smaller than the former one). After each iteration, one less element (the last one) is needed to be compared until there are no more
1112
elements left to be compared.

app/src/test/java/com/igorwojda/datastructure/list/bubblesort/BubbleSortSolution.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ private object BubbleSortSolution {
44
private fun bubbleSort(list: List<Int>): List<Number> {
55
val sorted = list.toMutableList()
66

7-
sorted.forEachIndexed { i, _ ->
7+
(0..sorted.lastIndex).forEach { i ->
88
var swapped = false
99
(0 until (sorted.size - i - 1)).forEach { j ->
1010
val element = sorted[j]

app/src/test/java/com/igorwojda/datastructure/list/bubblesort/BubbleSort.kt renamed to app/src/test/java/com/igorwojda/datastructure/list/bubblesort/selectionSort.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package com.igorwojda.datastructure.list.bubblesort
33
import org.amshove.kluent.shouldEqual
44
import org.junit.Test
55

6-
private fun bubbleSort(list: List<Int>): List<Number> {
6+
private fun selectionSort(list: List<Int>): List<Number> {
77
return list
88
}
99

@@ -15,6 +15,6 @@ class BubbleSortTest {
1515

1616
@Test
1717
fun `bubble sort test`() {
18-
bubbleSort(LIST) shouldEqual SORTED_LIST
18+
selectionSort(LIST) shouldEqual SORTED_LIST
1919
}
2020
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.igorwojda.datastructure.list.selectionsort
2+
3+
import org.amshove.kluent.shouldEqual
4+
import org.junit.Test
5+
6+
private fun selectionSort(list: List<Int>): List<Number> {
7+
return list
8+
}
9+
10+
class SelectionSortTest {
11+
companion object {
12+
val LIST = listOf(5, 1, 4, 2, 8)
13+
val SORTED_LIST = listOf(1, 2, 4, 5, 8)
14+
}
15+
16+
@Test
17+
fun `selection sort test`() {
18+
selectionSort(LIST) shouldEqual SORTED_LIST
19+
}
20+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Selection sort
2+
3+
## Instructions
4+
5+
[Puzzle files](.)
6+
7+
Sort list of numbers from lowest number to greatest number using [selection sort](https://en.wikipedia.org/wiki/Selection_sort).
8+
9+
Algorithm: Sort an list by repeatedly finding the minimum element (ascending order) from unsorted part and putting it at the beginning of
10+
the list.
11+
12+
## Examples
13+
14+
Example 1
15+
16+
Sort `[5, 1, 4, 2, 8]`
17+
18+
19+
20+
## Hints
21+
22+
<details>
23+
<summary>Hint 1</summary>
24+
The algorithm uses two sublists in a given array.
25+
26+
1. The sublist which is already sorted
27+
2. Remaining sublist which is unsorted
28+
</details>
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.igorwojda.datastructure.list.bubblesort
2+
3+
private object SelectionSortSolution {
4+
private fun selectionSort(list: List<Int>): List<Number> {
5+
val sorted = list.toMutableList()
6+
7+
(0..sorted.lastIndex).forEach { i ->
8+
var indexOfMin = i
9+
10+
((i + 1)..sorted.lastIndex).forEach { j ->
11+
if(sorted[j] < sorted[indexOfMin]) {
12+
indexOfMin = j
13+
}
14+
}
15+
16+
if(indexOfMin != i) {
17+
val temp = sorted[i]
18+
sorted[i] = sorted[indexOfMin]
19+
sorted[indexOfMin] = temp
20+
}
21+
}
22+
23+
return sorted
24+
}
25+
}

app/src/test/java/com/igorwojda/datastructure/tree/traversal/TreeTraversal.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ using `Breath-First` and `Depth-First` traversals.
1717

1818
Example:
1919

20-
<img src="./misc/breadth_first_traversal.svg">
20+
![breadth_first_traversal.svg](misc/breadth_first_traversal.svg)
2121

2222
### 2. Depth First traversal (DF traversal)
2323

@@ -26,5 +26,4 @@ Example:
2626
* **Test**: `depth first traverse`
2727

2828
Example:
29-
30-
<img src="./misc/depth_first_traversal.svg">
29+
![depth_first_traversal.svg](misc/depth_first_traversal.svg)

app/src/test/java/com/igorwojda/sort/bubblesort/BubbleSort.kt

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

app/src/test/java/com/igorwojda/sort/bubblesort/BubbleSort.md

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

0 commit comments

Comments
 (0)