Giter VIP home page Giter VIP logo

xsort's Introduction

GoDoc

About

This package is a collection of extra primitives related to sorting. Currently it has only two functions:

These functions allows to quickly re-sort a previously sorted slice, which has few unsorted elements appended. Appended works pretty well if the tail is small, but limitations of the in-place algorithm makes it ineffective is the tail is comparable by size with the total size of the slice. While AppendedWithBuf is effective with

latencies

As one may see resorting 100 items in a slice of length 1048576 using AppendedWithBuf is almost 100 times more effective than just full resorting using sort.Slice or sort.Sort. But if the amount of unsorted items is too high then it might be less effective. So for convenience to automatically avoid spending more time than simple through Sort/Slice these functions has heuristic fallback logic for big tails, so the actual performance for a slice of size 1048576 looks like here:

latencies

But using of these functions does not make sense if tailLenght always equals to len(s).

P.S.: Also for comparison here is performance for a slice of size 256:

latencies

Quick start

In-place:

package main

import (
	"fmt"
	"sort"

	"github.com/go-ng/xsort"
)

func main() {
	s := sort.IntSlice{-4, -2, 1, 3, 4, 5, 9}

	s = append(s, 2, -3)
	xsort.Appended(s, 2)

	fmt.Println(s) // output: [-4 -3 -2 1 2 3 4 5 9]
}

(sandbox)

With buffer (faster):

package main

import (
	"fmt"
	"sort"

	"github.com/go-ng/xsort"
)

func main() {
	s := sort.IntSlice{-4, -2, 1, 3, 4, 5, 9}

	a := []int{2, -3}
	s = append(s, a...)
	xsort.AppendedWithBuf(s, a)

	fmt.Println(s) // output: [-4 -3 -2 1 2 3 4 5 9]
}

(sandbox)

xsort's People

Contributors

xaionaro avatar

Stargazers

 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.