GithubHelp home page GithubHelp logo

tatsuyafujisaki / kotlin-sorting-algorithms Goto Github PK

View Code? Open in Web Editor NEW
7.0 0.0 2.0 45 KB

Sorting algorithms implemented in Kotlin

Kotlin 100.00%
kotlin sorting-algorithms quicksort merge-sort heapsort bubble-sort insertion-sort selection-sort radix-sort

kotlin-sorting-algorithms's Introduction

Sorting algorithms

Name Best-case time complexity Average time complexity Worst-case time complexity Space complexity Stable?
Bubble sort O(n) comparisons, O(1) swaps O(n^2) comparisons and swaps O(n^2) comparisons and swaps O(1) Yes
Counting sort - O(n+k) O(n+k) O(n+k) Yes
Heapsort O(n log n) O(n log n) O(n log n) O(1) No
Insertion sort O(n) comparisons, O(1) swaps O(n^2) comparisons and swaps O(n^2) comparisons and swaps O(1) Yes
Merge sort O(n log n) O(n log n) O(n log n) O(n) Yes
Quicksort O(n log n) O(n log n) O(n^2) O(log n) No
Radix sort O(k*n) O(k*n) O(k*n) O(k+n) Yes
Selection sort O(n^2) comparisons, O(1) swaps O(n^2) comparisons, O(n) swaps O(n^2) comparisons, O(n) swaps O(1) No

Quicksort

Why is the average time complexity n log n?

Because ...

  • Each partitioning is O(n) because you have to visit every element.
  • Partitioning is repeated for the height of the tree.
  • Hence, the time complexity is n * height.

Height of tree

Ideal case

You choose a pivot that partitions the input in half. In the case, the height of the tree is log(n) because n โ‰ค 2^h - 1.

Worst case

You choose a pivot that is the smallest or largest element. In the case, the height of the tree is n.

How to avoid the worst-case scenario?

  • Randomly choose a pivot.
  • Choose the median as a pivot.

Quicksort versus Merge sort versus Heapsort

Name Strengths Weaknesses
Quicksort * Fastest in practice
* Parallelizable because it is a divide-and-conquer algorithm
* Slow worst-case
* Not stable
Merge sort * Fast worst-case
* Parallelizable because it is a divide-and-conquer algorithm
* Stable
Space inefficient
Heapsort * Fast worst-case
* Space efficient
* Slow in practice
* Not stable

Note

  • The space complexity O(1) means in-place sorting.
  • Stable sorting algorithms maintain the relative order of records with equal values.
  • n and 2n are both O(n).
  • Quicksort and heapsort are single words but merge sort is not.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.