java – Sort Algorithms in Kotlin – Bubbles, Insert, Selection, Merge and Quick Sort

I decided to try Kotlin because of its indulgent nature for long time Java users. I've implemented some introductory sorting algorithms and would like to see them again, not only to make sure that I apply Kotlin correctly, but also to improve my style and eliminate any bugs.

I've used some extension functions to make the code more readable and all relevant source codes will be posted in this issue.

Note: I also called it Java, because a person familiar with Java can easily access Kotlin if she sees how easy it is to read Kotlin. Anything that is applicable in Java can almost always be applied directly to the Kotlin code.

Main files

+ - meta
| + - annotations
| | + - ComparisonSort
| | + - StableSort
| + - Complexity
+ - sort
| + - AbstractSort
| + - BubbleSort
| + - InsertionSort
| + - MergeSort
| + - QuickSort
| + - SelectionSort
+ - util
+ - ArrayHelper

Tests

+ - sort
| + - AbstractSortTest
| + - BubbleSortTest
| + - InsertionSortTest
| + - MergeSortTest
| + - QuickSortTest
| + - SelectionSortTest
+ - util
+ - ArrayHelperTest

In addition to being undocumented (I have not added comments because I think the code is already readable enough), I think my code might be a little better organized. If I forgot to include some test cases, thank you for letting me know. General comments on all potential problems, preferences are always welcome.

ArrayHelper.kt

Note that all functions here are extension functions. These methods are added to the existing methods in the Array.kt class provided in the library.

package com.github.hungrybluedev.util

import kotlin.random.Random

fun  Board.swap (i: Int, j: Int) {
val tmp = this[i]
    this[i] = this[j]
    this[j] = tmp
}

fun  Board.shuffle () {
shuffle (0, size)
}

fun  Board. Shuffle (bottom: Int, up: Int) {
for (index down + 2 up to high) {
swap (index, Random.nextInt (index))
}
}

fun <T: comparable> Table.isSorted (): Boolean {
return isSorted (0, size)
}

fun <T: comparable> Table.isSorted (
lower: Int,
Above: Int
): Boolean {
for (index down + 1 up to high) {
if this[index - 1] > this[index]) {
false returns
}
}
returns true
}

fun  Board.isSorted (
comparator: comparator,
lower: Int,
Above: Int
): Boolean {
for (index down + 1 up to high) {
if (comparator.compare (this[index - 1], this[index])> 0) {
false returns
}
}
returns true
}

AbstractSort.kt

package com.github.hungrybluedev.sort

import com.github.hungrybluedev.meta.Complexity

AbstractSort abstract class (complexity of val: complexity) {
fun <T: comparable> so (arr: Array) {
sort (arr, naturalOrder (), 0, arr.size)
}

fun <T: comparable> so (arr: Array, lower: Int, upper: Int) {
sort (arr, naturalOrder (), lower, upper)
}

fun  spell (arr: Array, comparator: comparator) {
sort (arr, comparator, 0, arr.size)
}

abstract fun  Sort(
arr: Array,
comparator: comparator,
lower: Int,
Above: Int
)
}

BubbleSort.kt

package com.github.hungrybluedev.sort

import com.github.hungrybluedev.meta.Complexity
import com.github.hungrybluedev.meta.annotatioins.ComparisonSort
import com.github.hungrybluedev.meta.annotatioins.StableSort
import com.github.hungrybluedev.util.swap

@ComparisonSort
@StableSort
BubbleSort class: AbstractSort (Complexity.QUADRATIC) {
to override the pleasure  Sort(
arr: Array,
comparator: comparator,
lower: Int,
Above: Int
) {
var sorted = 0
make {
permuted var = false
for (i down to high - 1 - sorted) {
if (comparator.compare (arr[i]arr[i + 1])> 0) {
arr.swap (i, i + 1)
permuted = true
}
}
sorted ++
} while (traded)
}
}

InsertionSort.kt

package com.github.hungrybluedev.sort

import com.github.hungrybluedev.meta.Complexity
import com.github.hungrybluedev.meta.annotatioins.ComparisonSort
import com.github.hungrybluedev.meta.annotatioins.StableSort

@StableSort
@ComparisonSort
class InsertionSort: AbstractSort (Complexity.QUADRATIC) {

to override the pleasure  Sort(
arr: Array,
comparator: comparator,
lower: Int,
Above: Int
) {
for (at the bottom + 1 until high) {
val = arr[j]
            var i = j - 1
while (i> = lower && comparator.compare (key, arr[i]) <0) {
arr[i + 1] = arr[i--]
            }
arr[i + 1] = key
}
}
}

MergeSort.kt

package com.github.hungrybluedev.sort

import com.github.hungrybluedev.meta.Complexity
import com.github.hungrybluedev.meta.annotatioins.ComparisonSort
import com.github.hungrybluedev.meta.annotatioins.StableSort

@StableSort
@ComparisonSort
MergeSort Class: AbstractSort (Complexity.LINEARITHMIC) {

private pleasure  merge(
from: Array,
to: table,
p: Int,
q: Int,
r: Int,
comparator: comparator
    ) {
var i = p
var j = p
var k = q
while (i <q && k <r) {
at[j++] = if (comparator.compare (from[i], from[k]) <0)
of[i++]
            other
of[k++]
        }
while (i <q) {
at[j++] = of[i++]
        }
while (k <r) {
at[j++] = of[k++]
        }
}

private pleasure  internalSort (
arrA: painting,
arrB: painting,
comparator: comparator,
p: Int,
r: Int
) {
if (r - p <= 1) {
return
}
val q = p + ((r - p) shr 1)
internalSort (arrB, arrA, comparator, p, q)
internalSort (arrB, arrA, comparator, q, r)
fusion (arrB, arrA, p, q, r, comparator)
}

to override the pleasure  Sort(
arr: Array,
comparator: comparator,
lower: Int,
Above: Int
) {
val copy = arr.clone ()
internalSort (arr, copy, comparator, lower, upper)
}
}

QuickSort.kt

package com.github.hungrybluedev.sort

import com.github.hungrybluedev.meta.Complexity
import com.github.hungrybluedev.meta.annotatioins.ComparisonSort
import com.github.hungrybluedev.util.swap
import kotlin.random.Random

@ComparisonSort
class QuickSort: AbstractSort (Complexity.LINEARITHMIC) {
private pleasure  partition(
arr: Array,
lower: Int,
Above: Int
comparator: comparator
    ): Int {
val pivot = arr[lower + Random.nextInt(upper - lower + 1)]

        var p = lower - 1
var q = higher + 1
while (true) {
while (comparator.compare (arr[++p], pivot) < 0);
            while (comparator.compare(arr[--q], pivot) > 0);
if (p> = q) {
back q
}
arr.swap (p, q)
}
}

private pleasure  internalSort (
arr: Array,
comparator: comparator,
lower: Int,
Above: Int
) {
if (lower> = higher) {
return
}
val pivot = partition (arr, lower, upper, comparator)
internalSort (arr, comparator, inferior, pivot)
internalSort (arr, comparator, pivot + 1, upper)
}

to override the pleasure  Sort(
arr: Array,
comparator: comparator,
lower: Int,
Above: Int
) {
internalSort (arr, comparator, lower, upper - 1)
}
}

Complexity.kt

package com.github.hungrybluedev.meta

Enum class Complexity {
lOGARITHMIC,
LINEAR,
LINEARITHMIQUE,
QUADRATIC,
CUBIC
}

ComparisonSort.kt

com.github.hungrybluedev.meta.annotatioins package

ComparisonSort annotation class

StableSortSort.kt

com.github.hungrybluedev.meta.annotatioins package

StableSort annotation class

AbstractSortTest.kt

package com.github.hungrybluedev.sort

import com.github.hungrybluedev.meta.Complexity
import com.github.hungrybluedev.util.isSorted
import com.github.hungrybluedev.util.shuffle
import org.junit.jupiter.api.Assertions.assertArrayEquals
import org.junit.jupiter.api.Assertions.assertTrue
import org.junit.jupiter.api.Test
import kotlin.random.Random

AbstractSortTest abstract class(private yard sorter: T) {
size of the private value = when (sorter.complexity) {
Complexity.QUADRATIC -> 20_000
Complexity.LINEARITHMIC -> 1_000_000
otherwise -> 10_000
}

@Test
internal fun emptyTest () {
val arr = arrayOf()
sorter.sort (arr)
assertArrayEquals (arrayOf(), arr)
}

@Test
internal fun singleElementTest () {
val arr = arrayOf (1)
sorter.sort (arr)
assertArrayEquals (arrayOf (1), arr)
}

@Test
internal fun sortElementsTest () {
val arr = arrayOf (1, 2, 5, 7)
sorter.sort (arr)
assertArrayEquals (arrayOf (1, 2, 5, 7), arr)
}

@Test
internal fun unsortedElementsTest () {
val arr = arrayOf (7, 2, 5, 1)
sorter.sort (arr)
assertArrayEquals (arrayOf (1, 2, 5, 7), arr)
}

@Test
internal fun reverseOrderTest () {
val arr = arrayOf (9, 8, 7, 6, 5, 4, 3, 2, 1, 0)
sorter.sort (arr)
assertArrayEquals (arrayOf (0, 1, 2, 3, 4, 5, 6, 7, 8, 9), arr)
}

@Test
internal fun partialSortTest () {
val arr = arrayOf (9, 8, 7, 6, 5, 4, 3, 2, 1, 0)
sorter.sort (arr, 4, 9)
assertArrayEquals (arrayOf (9, 8, 7, 6, 1, 2, 3, 4, 5, 0), arr)
}

@Test
Internal Fun ComparatorDescendingTest () {
val arr = arrayOf (9, 3, 4, 6, 2, 1, 0, 5, 7, 8)
sorter.sort (arr, reverseOrder ())
assertArrayEquals (arrayOf (9, 8, 7, 6, 5, 4, 3, 2, 1, 0), arr)
}

@Test
Internal Fun ComparatorPartialDescendingTest () {
val arr = arrayOf (3, 0, 1, 2, 4, 8, 5, 9, 7, 6)
sorter.sort (arr, 0, 5)
sorter.sort (arr, reverseOrder (), 5, 10)
assertArrayEquals (arrayOf (0, 1, 2, 3, 4, 9, 8, 7, 6, 5), arr)
}

@Test
internal fun repeatElementsTest () {
val arr = arrayOf (0, 0, 1, 2, 3, 3, 4, 4, 4, 4, 4, 5, 6, 7, 8, 8, 9, 9, 9, 9)
val cpy = arr.clone ()
cpy.shuffle ()
sorter.sort (cpy)
assertArrayEquals (arr, cpy)
}

@Test
internal fun randomDistinctArrayTest () {
val arr = Tableau (size) {x -> x}
sorter.sort (arr)
assertTrue (arr.isSorted ())
}

@Test
internal randomRepeatedTest () {
val arr = Array (size) {Random.nextInt (size)}
sorter.sort (arr)
assertTrue (arr.isSorted ())
}

@Test
internal fun descendingTest () {
val arr = Tableau (size) {x -> size - x}
sorter.sort (arr)
assertTrue (arr.isSorted ())
}
}

BubbleSortTest.kt

package com.github.hungrybluedev.sort


BubbleSortTest internal class: AbstractSortTest(BubbleSort ())

InsertionSortTest.kt

package com.github.hungrybluedev.sort

internal class InsertionSortTest: AbstractSortTest(Insert Sort ())

MergeSortTest.kt

package com.github.hungrybluedev.sort

MergeSortTest internal class: AbstractSortTest(Merge sort ())

QuickSortTest.kt

package com.github.hungrybluedev.sort

QuickSortTest internal class: AbstractSortTest(Fast sort ())

SelectionSortTest.kt

package com.github.hungrybluedev.sort

SelectionSortTest internal class: AbstractSortTest(SelectionSort ())

ArrayHelperTest.kt

package com.github.hungrybluedev.util

import org.junit.jupiter.api.Assertions.assertFalse
import org.junit.jupiter.api.Assertions.assertTrue
import org.junit.jupiter.api.Test

internal class ArrayHelperTest {
@Test
internal fun smallAscendingTest () {
val arr = arrayOf (1, 2, 3, 4, 5)
assertTrue (arr.isSorted ())
assertTrue (arr.isSorted (0, arr.size))
assertTrue (arr.isSorted (naturalOrder (), 0, arr.size))
}

@Test
internal fun smallDescendingTest () {
val arr = arrayOf (9, 7, 6, 4, 3, 2)
assertFalse (arr.isSorted ())
assertFalse (arr.isSorted (0, arr.size))
assertTrue (arr.isSorted (reverseOrder (), 0, arr.size))
}
}

Slim Quick Keto of anesthetic lotion on the emission position.


  1. miji kait

    miji kait
    New member


    joined:
    Today & # 39; hui
    posts:
    1
    I like receipts:
    0
    Trophy points:
    0
    Sex:
    Male

    Slim Quick Keto When the lotion becomes effective, the doctor administers a 1988, but it has just been introduced in the United States. The American non-surgical aesthetic community estimates that the wide variety of lipodissoloma treatments conducted in the United States and Europe is between 50,000 and 100,000. Are there known drawbacks to the procedure? Like other feature techniques, PCDC can not guarantee 100% performance, even if it is fast and less evasive. But, relying on the wide variety of techniques employed, it is obvious that many are interested in taking it in the name of thinness and comfort. The cost of PCDC therapy can range from $ 900 to $ 2,500, but many are still willing to pay for it as it works quickly to dissolve unwanted areas in targeted locations. Many who have tried it have also claimed to have seen it.

Reactions in Keto quick results

Discussion in & # 39; SEO & Traffic Discussion & # 39; started by johnlimler, January 14, 2019 at 05:52.

Keywords:

  • quick results

  1. johnlimler,
    January 14, 2019 at 05:52

    # 1


    johnlimler

    johnlimler
    New member


    joined:
    Today & # 39; hui
    posts:
    2
    I like receipts:
    0
    Trophy points:
    1
    Sex:
    Female
    Occupation:
    maximum increase
    Location:
    United States
    Home page:
    https://maximumenhancement.com/rapid-results-keto-diet/

    As you track down the reactions, you do not need stress as it consists of regular repairs. In case of health risk, you can consult your doctor about normal use. Keto Quick Results is tried deductively by the group of designers. It is therefore as contrasted as others more accessible on the market. The element that has no reactions will help you achieve the ideal result you need. It is proposed to look into this element to obtain advantageous results. Click on its official website for more information:https://maximumenhancement.com/rapid-results-keto-diet/

    # 1

Quick Interest – Rapidinterest.com

I am neither an administrator nor owner of this project

>>> Link for the recording <<<

Description of the program
RapidInterest stands out for its style and attitude, distinguishing it from other BTC Arbitrage trading / mining companies. Our corporate vision is the foundation of our success. We define "success" by the continued ability to execute margin trading strategies and increase our productivity in order to gain a competitive advantage over the BTC stock market. The talent and enthusiasm of our employees is essential to achieving and maintaining good results in the ongoing work in the marketplace. We have assembled a group of highly motivated and experienced BTC analysts who value teamwork, honesty, entrepreneurial talent and personal excellence.

QUOTE

Investment plans

IPB image

2.5% daily performance forever

>>> Link for the recording <<<

Payment Processor
Perfect money
Payeer
฿Bitcoin
Ethereum

Reflective Committee
5 to 7%

Minimum deposit
$ 25

functionality
Encrypted SSL
Rapid and cooperative support
Protected DDoS
Dedicated hosting
Unique design

>>> Link for the recording <<<

(Investment programs are always risky so do not invest if you can not afford to lose)

Slim Quick Keto | Black Hat Seo & Affiliate Marketing Forum


  1. Mazglubex Iza

    Mazglubex Iza
    New member


    joined:
    Today & # 39; hui
    posts:
    1
    I like receipts:
    0
    Trophy points:
    0
    Sex:
    Male

    Slim Quick Keto
    on our hip do you handle we'll take side facet aspect lower back and you can once again try to hold this manipulation by hand for your hip k are you ready to start it is very correlated we're going positioned our arms up to the aspect of the toes collectively SlimQuick Keto twist twist twist twist so we'll take it diagonally to another and keep using this core allows us to double what happens here is that you become a shimmy will become an extraordinary shimmy twist k we will do it in place our fingers and wrists around two, two, two, trying to make each other a part of our truly high quality frame, some lucky shots that hold our breath, we had a new guy who arrives at the hip, hip

    [​IMG]"data-url =" https://campaign4africa.org/wp-content/uploads/2019/01/Slim-Quick-Keto.jpg

[WTS] KVChosting.net quality SSD VPS deals with high availability and low prices.

This is a discussion on KVChosting.net quality SSD VPS deals with high availability and low prices. in the Webmaster Marketplace forums, part of the enterprise category; KVC Hosting was launched in 2010 for the sole purpose of creating a host society that is affordable for everyone. …

.

Windows App – Quick Access Popup 9.4.1.1 Multilingual Donor | NulledTeam UnderGround

Quick Access Popup 9.4.1.1 Donors Multilingual | Size of the file: 3.24 MB

QAP (Quick Access Popup) allows you to move like a breeze between your frequently used folders, folders and documents. Save thousands of clicks when browsing your favorite folders in Windows Explorer or in file dialogs. Instantly launch your most used apps, documents or websites with this simple pop-up menu and mouse / keyboard shortcut manager. Gather linked bookmarks in SUBMENUS. Assign favorites or submenus to keyboard or mouse shortcuts. Or GROUP favorites and launch them with one click.
The largest shopping center

To launch the context menu, click on the QAP icon in the system tray, press the middle mouse button or press Windows + W (these triggers can be modified in Options). In the SETTINGS window, customize the QAP menu. Check the shortcuts assigned to favorites or submenus in the HOTKEYS window.

To quickly add a new favorite folder to your context menu: 1) go to a frequently used folder, 2) click on the QAP keyboard shortcut and choose ADD THIS FILE, 3) give the folder a short name and save it.

In the MY QAP ESSENTIALS menu, choose the SWITCH menu to switch to any other explorer or application window that is running. Use the READ A FOLDER menu to reopen one of the folders already open in a browser window (very useful in file dialogs). Choose the RECENT FOLDERS menu to view an updated list of recent Windows folders. Access files or URLs from your clipboard using the CLIPBOARD menu. Click the DRIVES menu to display the list of drives with the current disk space.

In the OPTIONS dialog box, choose your preferred language, the size of the menu icons, the colors of the window. Select the number of recent folders to view, add numeric hotkeys to the folder menu, or shortcut reminders. Pin the context menu to a fixed position or remember the position of the Settings window.

All this for free in a very intuitive application. Quick Access Popup is available on Windows 7 and later. Works with Windows Explorer, Directory Opus, Total Commander and 15 other CATION file managers, WEB PAGES and much more!

What's up

HOME PAGE

Download from UploadGig

Download from Rapidgator

The largest shopping center

How to create a gif in a smart phone?

Do not ignore if you are looking for an app to create a gif from a camera, convert images or videos to gif or even edit gif on An ♪♪♪♪♪.

Today, imgplay or gif becomes an effective way for young people to be creative, to keep the moments fun and interesting. Not just cheap gifs, easy to share on the internet.

Now you can use our app right away to create the best quality in your style.

✨ Exceptional feature of Gif Maker application:

💗 Simple interface, easy to use
💗 Low capacity, easy to download and install
💗 No advertising
Completely free
💗 Create quick gif with HD quality
💗 Compatible with all devices ♪♪♪♪♪

Instructions to use the GIF Maker application to create animations and edit gif:

Step 1: Select create gif and convert the video to gif from your device library or record a new video.
Step 2: Select the part of the video for which you want to create an animation.
Step 3: Press "convert" to convert this video to gif.

Go to google play now, find "the gif maker app" and download the Vtool development application or click this link to experience it.

.

[WTS] Aspnix.com – Ultra Fast Windows VPS Cloud

AT ASPnix, you are the family. And we know how to treat our family. Our dedication to our customers makes our priority a top priority, as it brings you the latest technological advances, a 24/7 customer service network and community forums to discuss technology, feedback and experience. discussions in general. We care about what you have to say and want to hear it. Welcome to the family.

We are happy to announce our new Virtual Cloud Server Packages this will give our customers more flexibility in building the exact virtual server required. No more predefined plans that are not flexible, no need to pay for hundreds of GB space while all you needed was more RAM. Pay only what you need!

Our new cloud platform is optimized by Samsung Data Center professional class SSDs for maximum speed, over 1,000,000 IOPS performance!
Powerful Intel Xeon processors up to 16 cores and DDR4 memory up to 32 GB! Our new platform begins to $ 20 per month for a Windows VPS!

Those who love Linux can now enjoy it on our Cloud Hyper-V platform as well!
CentOS, Fedora Server, Red Hat, Ubuntu Server and Debian are all now supported! Start with a Linux VPS today for only $ 10 per month.

Customers who want to create their own hosting company or just want to make it easier to manage their Linux server can buy our cPanel Control Panel add-on.

For more information on our new visit of VPS plans – https://www.aspnix.com/servers/virtual-cloud-servers/

It's the perfect time to buy a VPS and start hosting your own website, database, point-of-sale system and much more! Around the world, customers are entrusting us with their Virtual Cloud Server operations and our growing network! So, why not try our new virtual server plans and see how a VPS can help you today!

If you have any questions, please check our product knowledge base if you still can not find your answer, please contact our support team.

Thank you for choosing ASPnix solutions!

.

Google Drive File Stream removes quick access from Google Drive to Windows

I'm simultaneously running GDFS (Google Drive File Stream) and GBS (Backup and Sync) simultaneously on Windows 10. I'd like to have them both in the Quick Access list located to the left of the window. ;Windows explorer. But it always seems to come back to having GDFS and not GBS in the list.

To replicate:

  1. Have GDFS and GBS (Google Backup and Sync) installed and running.
  2. Right-click the Google Drive (GBS) folder anywhere in Windows Explorer, and then lock the quick access.
  3. Note that Google Drive appears in the Quick Access list.
  4. (Optionally, unlink GDFS from Quick Access, it makes no difference.)
  5. Restart Windows.
  6. Note that GDFS is (back) in Quick Access and Google Drive is not.

Someone did he get that at work? Is there a hack by which I can force change?