Giter VIP home page Giter VIP logo

nim-sorting's Introduction

nim-sorting

A collection of sorting algorithms in Nim

Install

nimble install https://github.com/rustomax/nim-sorting

Usage

# Example of sorting students by their GPA

import sorting/quickSort

type
    Student = object
      name: string
      gpa: float

proc `<`*(a, b: Student): bool {.inline.} =
    return a.gpa < b.gpa

var students: array[4, Student] = [Student(name: "Bob", gpa: 4.2),
                                   Student(name: "Diana", gpa: 4.9),
                                   Student(name: "Julie", gpa: 3.7),
                                   Student(name: "Max", gpa: 2.6)]

students.quickSort()

assert students == [Student(name: "Max", gpa: 2.6),
                    Student(name: "Julie", gpa: 3.7),
                    Student(name: "Bob", gpa: 4.2),
                    Student(name: "Diana", gpa: 4.9)]

Run tests suite

nimble test

Run benchmark suite

nimble benchmark

Sample performance comparison

Benchmarking sorting algorithms with 1000 integers
  Bubble     :  0.001628 sec
  Selection  :  0.001293 sec
  Insertion  :  0.001357 sec
  Merge      :  0.000345 sec
  Heap       :  0.000211 sec
  Comb       :  0.000084 sec
  Quicksort  :  0.000072 sec

Benchmarking sorting algorithms with 10000 integers
  Bubble     :  0.207325 sec
  Selection  :  0.098015 sec
  Insertion  :  0.066177 sec
  Merge      :  0.002245 sec
  Heap       :  0.001876 sec
  Comb       :  0.001506 sec
  Quicksort  :  0.001338 sec

Benchmarking sorting algorithms with 100000 integers
  Bubble     : 22.345801 sec
  Selection  :  9.118128 sec
  Insertion  :  6.093889 sec
  Merge      :  0.027296 sec
  Heap       :  0.027507 sec
  Comb       :  0.020545 sec
  Quicksort  :  0.011738 sec

Playing with compile-time switches

CPP: cpp
RELEASE: -d:release
DANGER: -d:danger
LTO: -d:lto
NRT: --newruntime

Benchmarking Quicksort with 100000 integers

C RELEASE NRT      : 0.011169 sec -> 100%
C RELEASE LTO      : 0.010939 sec
C RELEASE          : 0.010693 sec
CPP RELEASE        : 0.008718 sec

C DANGER NRT       : 0.006382 sec
CPP DANGER NRT     : 0.006336 sec
CPP DANGER         : 0.006275 sec
C DANGER           : 0.006166 sec
CPP DANGER NRT LTO : 0.006151 sec -> 45% improvement vs C RELEASE NRT

nim-sorting's People

Contributors

rustomax avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

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.