Skip to content

Commit 42a0613

Browse files
authored
Merge pull request #108 from Ha1fByte/KotlinBogoSort
Kotlin bogo sort
2 parents c250af7 + b3bb40c commit 42a0613

File tree

2 files changed

+48
-1
lines changed

2 files changed

+48
-1
lines changed

Kotlin/BogoSort.kt

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import kotlin.random.Random
2+
3+
fun main(args : Array<String>) {
4+
//Create Array
5+
var array = intArrayOf(4,5,3,9,1)
6+
7+
array = sort(array)
8+
9+
array.forEach {
10+
println(it)
11+
}
12+
}
13+
14+
/**
15+
* Use Bogo sort to randomly swap two inputs and then check if the list is sorted
16+
*/
17+
fun sort(input:IntArray):IntArray{
18+
19+
//keep repeating check until sorted
20+
while (!isSorted(input)){
21+
//get two randomly picked array locations
22+
val index1 = Random.nextInt(0, input.size-1)
23+
val index2 = Random.nextInt(0, input.size-1)
24+
25+
//swap the values
26+
val temp = input[index1]
27+
input[index1] = input[index2]
28+
input[index2] = temp
29+
}
30+
return input
31+
}
32+
33+
/**
34+
* Check if the array is sorted from lowest to highest
35+
*/
36+
fun isSorted(input:IntArray): Boolean{
37+
var max:Int? = null
38+
for(x in input.indices){
39+
if(max == null || input[x] >= max){
40+
max = input[x]
41+
}else{
42+
return false
43+
}
44+
}
45+
return true
46+
}
47+

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ _Sorting algorithms implemented in different languages (for hacktoberfest_ 😃_
1313
| Javascript | [`Bogo Sort`](Javascript/bogoSort.js) [`Counting Sort`](Javascript/countingSort.js) [`Heap Sort`](Javascript/HeapSort.js) [`Insertion Sort`](Javascript/Insertionsort.js) [`Merge Sort`](Javascript/MergeSort.js) <br> [`Quick Sort`](Javascript/Quicksort.js) [`Bubble Sort`](Javascript/bubbleSort.js) [`Shell Sort`](Javascript/shellSort.js ) [`Selection Sort`](Javascript/selectionSort.js) <br> [`Radix Sort`](Javascript/RadixSort.js) |
1414
| Python | [`Bogo Sort`](Python/BogoSort.py) [`Bubble Sort`](Python/BubbleSort.py) [`Bucket Sort`](Python/BucketSort.py) <br> [`Gnome Sort`](Python/GnomeSort.py) [`Insertion Sort`](Python/InsertionSort.py) [`Merge Sort`](Python/MergeSort.py) <br> [`Quick Sort`](Python/QuickSort.py) [`Radix Sort`](Python/RadixSort.py) [`Selection Sort`](Python/SelectionSort.py) <br> [`Binary Insertion Sort`](Python/BinaryInsertionSort.py) [`Heap Sort`](Python/heapSort.py) |
1515
| Ruby | [`Bubble Sort`](Ruby/bubble_sort.rb) [`Gnome Sort`](Ruby/gnome_sort.rb)
16-
| Kotlin |[`Merge Sort`](Kotlin/MergeSort.kt) [`Bubble Sort`](Kotlin/BubbleSort.kt) [`Selection Sort`](Kotlin/selectionSort.kt) <br> [`Heap Sort`](Kotlin/HeapSort.kt) [`Insertion Sort`](Kotlin/InsertionSort.kt) [`Quick Sort`](Kotlin/QuickSort.kt)
16+
| Kotlin |[`Merge Sort`](Kotlin/MergeSort.kt) [`Bubble Sort`](Kotlin/BubbleSort.kt) [`Selection Sort`](Kotlin/selectionSort.kt) <br> [`Heap Sort`](Kotlin/HeapSort.kt) [`Insertion Sort`](Kotlin/InsertionSort.kt) [`Quick Sort`](Kotlin/QuickSort.kt) [`Bogo Sort`](Kotlin/BogoSort.kt)
1717
| Elixir | [`Selection Sort`](Elixir/selectionSort.exs)
1818

1919
## Contributing 🖇️

0 commit comments

Comments
 (0)