Code Monkey home page Code Monkey logo

timsort's Introduction

timsort Build Status codecov

timsort is a Go implementation of Tim Peters's mergesort sorting algorithm.

For many input types it is 2-3 times faster than Go's built-in sorting.

The main drawback of this sort method is that it is not in-place (as any mergesort), and may put extra strain on garbage collector.

This implementation was derived from Java's TimSort object by Josh Bloch, which, in turn, was based on the original code by Tim Peters.

Installation

$ go get github.com/psilva261/timsort

Testing

Inside the source directory, type

go test

to run test harness.

Benchmarking

Inside the source directory, type

go test -test.bench=.*

to run benchmarks. Each combination of input type/size is presented to timsort, and, for comparison, to the standard Go sort (sort.Sort for ints or sort.Stable otherwise). See BENCHMARKS.md for more info and some benchmarking results.

Examples

As drop-in replacement for sort.Sort

package main

import (
	"github.com/psilva261/timsort"
	"fmt"
	"sort"
)

func main() {
	l := []string{"c", "a", "b"}
	if err := timsort.TimSort(sort.StringSlice(l)); err != nil {
			panic(err.Error())
	}
	fmt.Printf("sorted array: %+v\n", l)
}

Explicit "less" function

package main

import (
	"github.com/psilva261/timsort"
	"fmt"
)

type Record struct {
	ssn  int
	name string
}

func BySsn(a, b interface{}) bool {
	return a.(Record).ssn < b.(Record).ssn
}

func ByName(a, b interface{}) bool {
	return a.(Record).name < b.(Record).name
}

func main() {
	db := make([]interface{}, 3)
	db[0] = Record{123456789, "joe"}
	db[1] = Record{101765430, "sue"}
	db[2] = Record{345623452, "mary"}

	// sorts array by ssn (ascending)
	timsort.Sort(db, BySsn)
	fmt.Printf("sorted by ssn: %v\n", db)

	// now re-sort same array by name (ascending)
	timsort.Sort(db, ByName)
	fmt.Printf("sorted by name: %v\n", db)
}

timsort's People

Contributors

pgmmpk avatar psilva261 avatar dsamarin avatar

Watchers

 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.