forked from HarshCasper/NeoAlgo
-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding Kotlin Language Bubble Sort Algorithm (HarshCasper#1353)
* implemented Bubble sort in kotlin * Updated the readme * Added Selection Sort Algorithm for Kotlin
- Loading branch information
1 parent
7cc7d88
commit 8b318e7
Showing
3 changed files
with
86 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<p align="center"> | ||
<img src="../img/neo_algo.png"><br> | ||
<img src="https://img.shields.io/github/license/tesseractcoding/neoalgo?style=flat"> | ||
<a href="http://makeapullrequest.com" target="_blank"><img src="https://img.shields.io/badge/PRs-welcome-brightgreen.svg?style=flat" alt="PRs Welcome"></a> | ||
<img alt="GitHub pull requests" src="https://img.shields.io/github/issues-pr/tesseractcoding/neoalgo"> | ||
<img alt="GitHub issues" src="https://img.shields.io/github/issues/tesseractcoding/neoalgo"> | ||
<img alt="Github All Contributors" src="https://img.shields.io/github/all-contributors/tesseractcoding/neoalgo"> | ||
</p> | ||
|
||
# Codes in the Kotlin language | ||
|
||
## Sorting Algorithms | ||
|
||
[Bubble Sort](sort/BubbleSort/src/BubbleSort.kt) |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
fun bubbleSort(list: MutableList<Int>): List<Int>{ | ||
// taking a note of the lists length | ||
val length = list.size - 1 | ||
|
||
// the first loop runs for the number of items there are in the list | ||
for (i in 0 until length) { | ||
// a swapped variable which tracks if one of the variables was swapped | ||
// in one iteration | ||
var swapped = false | ||
|
||
// inner loop going from first element till the last element | ||
// in each round we have the last element as the greatest number | ||
// so next round has one less element | ||
// i.e equal to the number of iteration we are in currently. | ||
for (j in 0 until (length - i)) { | ||
// if current element is bigger than next element | ||
if (list[j] > list[j + 1]) { | ||
// do the swap | ||
list[j + 1] = list[j].also { list[j] = list[j + 1] } | ||
// take a note of that swap | ||
swapped = true | ||
} | ||
} | ||
// check if any swaps happened in this iteration. if there aren't any | ||
// swaps leave the function cause we have a sorted list. | ||
if (!swapped){ | ||
return list | ||
} | ||
} | ||
return list | ||
} | ||
|
||
fun main() { | ||
val list = listOf(11,22,33,45,34,53,27,36) | ||
println("unsorted list is $list") | ||
val sorted = bubbleSort(list.toMutableList()); | ||
println("sorted list is $sorted") | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
fun selectionSort(list: MutableList<Int>): List<Int>{ | ||
// run the code as many times as there are elements in the list | ||
for (i in 0 until list.size){ | ||
// store the first element as the smallest one as well as store the | ||
// index so that you know which number we are dealing with | ||
var minimum = list[i]; | ||
var indexMinimum = i | ||
|
||
// check the elements in the list ahead of our currently selected | ||
// element and see if there are any numbers smaller than it | ||
for(x in i+1 until list.size){ | ||
// actual checking of the smaller value | ||
if (list[x] < minimum){ | ||
// change our minimum value | ||
minimum = list[x] | ||
// as well as store the index of the minimum value | ||
indexMinimum = x | ||
} | ||
} | ||
// put the minimum value at the very beginning and put the beginning value | ||
// at the place of the minimum value | ||
list[i] = minimum.also { list[indexMinimum] = list[i] } | ||
} | ||
|
||
// return the sorted list | ||
return list | ||
} | ||
|
||
// driver and example code for selection sort | ||
fun main() { | ||
val unsorted = listOf(20,12,10,15,2,22,35,27,16) | ||
val sorted = selectionSort(unsorted.toMutableList()) | ||
print(sorted) | ||
} |