Last active
October 30, 2023 11:01
-
-
Save MaksimDmitriev/bf84df0f480841e4df7438a04dc3cfae to your computer and use it in GitHub Desktop.
SelectionSort
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package ru.maksim.sample | |
| import org.junit.jupiter.api.Test | |
| class SelectionSortTest { | |
| @Test | |
| fun foo() { | |
| val arr = intArrayOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) | |
| selectionSort(arr) | |
| println(arr.contentToString()) | |
| } | |
| @Test | |
| fun foo_2() { | |
| val arr = intArrayOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) | |
| arr.shuffle() | |
| selectionSort(arr) | |
| println(arr.contentToString()) | |
| } | |
| private fun selectionSort(arr: IntArray) { | |
| for (i in 0 until arr.lastIndex) { | |
| var minIndex = i | |
| for (j in i + 1..arr.lastIndex) { | |
| if (arr[j] < arr[minIndex]) { | |
| minIndex = j | |
| } | |
| } | |
| val temp = arr[i] | |
| arr[i] = arr[minIndex] | |
| arr[minIndex] = temp | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment