All Projects → voutcn → Kxsort

voutcn / Kxsort

Fast in-place radix sort with STL-like API

Projects that are alternatives of or similar to Kxsort

Sorting visualization
The Sound of Sorting: Visualize and Audibilize 12 classic sorting algorithms in real time
Stars: ✭ 260 (+642.86%)
Mutual labels:  sorting-algorithms, sorting
sorting-visualizer
Sorting Algorithms Visualizer
Stars: ✭ 429 (+1125.71%)
Mutual labels:  sorting, sorting-algorithms
C Sharp Algorithms
📚 📈 Plug-and-play class-library project of standard Data Structures and Algorithms in C#
Stars: ✭ 4,684 (+13282.86%)
Mutual labels:  sorting-algorithms, sorting
ultra-sort
DSL for SIMD Sorting on AVX2 & AVX512
Stars: ✭ 29 (-17.14%)
Mutual labels:  sorting, sorting-algorithms
Data-Structures-and-Algorithms
Implementation of various Data Structures and algorithms - Linked List, Stacks, Queues, Binary Search Tree, AVL tree,Red Black Trees, Trie, Graph Algorithms, Sorting Algorithms, Greedy Algorithms, Dynamic Programming, Segment Trees etc.
Stars: ✭ 144 (+311.43%)
Mutual labels:  sorting, sorting-algorithms
alvito
Alvito - An Algorithm Visualization Tool for Python
Stars: ✭ 52 (+48.57%)
Mutual labels:  sorting, sorting-algorithms
AlgorithmVisualizer
A better visualization of different algorithms made with React
Stars: ✭ 123 (+251.43%)
Mutual labels:  sorting, sorting-algorithms
Data-Structures-and-Algorithms
Data Structures and Algorithms implementation in Python
Stars: ✭ 31 (-11.43%)
Mutual labels:  sorting, sorting-algorithms
Algorithms Visualiser
Algorithms Visualiser is an opensource project made using ReactJS. Visualise Algorithms on Sorting, Pathfinding, Searching, Word Search, Backtracking.
Stars: ✭ 290 (+728.57%)
Mutual labels:  sorting-algorithms, sorting
Ruby
All algorithms implemented in Ruby
Stars: ✭ 454 (+1197.14%)
Mutual labels:  sorting-algorithms
Table Dragger
Turn your old table to drag-and-drop table with columns and rows sorting like magic!
Stars: ✭ 704 (+1911.43%)
Mutual labels:  sorting
Natsort
Simple yet flexible natural sorting in Python.
Stars: ✭ 450 (+1185.71%)
Mutual labels:  sorting
Column Sortable
Package for handling column sorting in Laravel 5/6/7/8
Stars: ✭ 496 (+1317.14%)
Mutual labels:  sorting
Advanced Algorithms
100+ algorithms & data structures generically implemented in C#.
Stars: ✭ 752 (+2048.57%)
Mutual labels:  sorting-algorithms
Gpusorting
Implementation of a few sorting algorithms in OpenCL
Stars: ✭ 9 (-74.29%)
Mutual labels:  sorting-algorithms
Js Sorting Algorithm
一本关于排序算法的 GitBook 在线书籍 《十大经典排序算法》,多语言实现。
Stars: ✭ 4,507 (+12777.14%)
Mutual labels:  sorting-algorithms
Algodeck
An Open-Source Collection of 200+ Algorithmic Flash Cards to Help you Preparing your Algorithm & Data Structure Interview 💯
Stars: ✭ 4,441 (+12588.57%)
Mutual labels:  sorting-algorithms
Coding Ninjas Java Solutions
This will have solutions to all the problems that are included in Coding Ninja's 2020 Java Course. Star the repo if you like it.
Stars: ✭ 32 (-8.57%)
Mutual labels:  sorting-algorithms
Sorting
🍡 Visualize the process of sorting algorithms simply
Stars: ✭ 24 (-31.43%)
Mutual labels:  sorting-algorithms
Cassandra Lucene Index
Lucene based secondary indexes for Cassandra
Stars: ✭ 584 (+1568.57%)
Mutual labels:  sorting

kxsort

Fast, in-place radix sort.

Quick Start

#include "kxsort.h"
#include <cstdlib>
#include <vector>
#include <iostream>

int main(int argc, char **argv) {
	int N = 10000000;
	std::vector<int> a(N);
	for (int i = 0; i < N; ++i) {
		a[i] = rand() * (rand() % 2 ? 1 : -1);
	}

	kx::radix_sort(a.begin(), a.end());
	std::cout << (is_sorted(a.begin(), a.end()) ? "OK" : "Error") << std::endl;
	return 0;
}

Benchmarks

Time (sec.) for sorting 100 million random integers.

Implementation int uint32_t uint64_t
kx::radix_sort 4.630 4.621 5.304
klib radix_sort 5.074 5.233 5.926
std::sort 11.321 11.191 11.260

APIs

1. Builtin Integers

To sort all C++ builtin integers in ascending order, the API is the same as std::sort.

template <class RandomIt>
void radix_sort(RandomIt s, RandomIt e);

2. Other class or struct

template <class RandomIt, class RadixTraits>
inline void radix_sort(RandomIt s, RandomIt e, RadixTraits r_traits);

To sort items of other value types, the user should define the RadixTraits for them. kx::radix_sort sorts items byte by byte (i.e. the bucket size is 256 for each round). A RadixTraits struct must implement the following public interfaces,

struct SampleRadixTraits {
    static const int nBytes;
    int kth_byte(const YourType &x, int k);
    bool compare(const YourType &x, const YourType &y);
};

where nBytes specifies how many rounds the radix sort will go over, and kth_byte(const YourType &x, int k) is the function to extract the k-th (0-base) byte of x for radix sort, i.e. the byte to be used in the nBytes-1-k round.

The function compare should be compatible with kth_byte, which means it should return exactly the same results as the following function for any x and y, but the implementation may be faster:

bool another_compare(const YourType &x, const YourType &y) {
	for (int k = nBytes - 1; k >= 0; --k) {
		if (kth_byte(x, k) < kth_byte(y, k)) return true;
		if (kth_byte(x, k) > kth_byte(y, k)) return false;
	}
	return false;
}

You may want to look at some examples.

Methodology

An MSD radix sort is implemented in kx::radix_sort. It partitions the input array with the most significant byte, and sort each partitions recursively. When the number of items in a partition is no more than 64, an insertion sort will be applied and no more recursion is needed. The using of insertion sort is the reason why it requires a compare function in RadixTraits.

The implement here is substantially the same as the following implementations

kx::radix_sort is a bit faster than ac's klib radix_sort probably due to the using of C++ template, which may unfold the recursions.

Limitations

  • It is not a stable sorting method.
  • It is slower than std::sort if the input sequence is nearly sorted.
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].