Git Product home page Git Product logo

pqsort's Introduction

pqsort.h

Generic C99 partial quicksort.

This is the sort routine largely as described by Bentley & McIlroy's "Engineering a Sort Function", along with some modifications as found in FreeBSD's libc.

"Partial" means pqsort can be used to place only specific items in their final sorted positions, making it useful for pagination, ranking and partitioning.

offset=100, limit=100 will place array[100] - array[199] in the correct position, ensure array[0] - array[99] contains only values smaller than that in array[100], and array[200] - array[nitems - 1] only values larger than array[199].

Since it's a preprocessor macro, comparison functions can be inlined. This can be quite a win if comparisons are cheap compared with function calls, e.g. when sorting integers. It can be easily adapted to handle void *'s and support a runtime-specified comparison function.'

A modified version of this routine was used by Newzbin.com's custom search engine, quickly sorting upwards of 40 million items for result sets between 50 and 20,000.

Example

#include <stdio.h>
#include "pqsort.h"

static inline int IntCmp(const int a, const int b) {
	return (a - b);
}

define_pqsort(my_int, int, IntCmp);

#define DUMP()  printf("{ "); \
	for (int i = 0; i < nvalues; i++)  \
		printf("%d, ", values[i]); \
	printf("}\n");

int main(void) {
	int values[] = { 42, 98, 56, 23, 45, 63, 56, 80, 102, 2 };
	int nvalues = sizeof(values) / sizeof(values[0]);

	// partition the list in two
	my_int_pqsort(values, nvalues, 5, 0);
	DUMP(); // => { 23, 2, 42, 45, 56, 80, 102, 98, 63, 56, }

	// sort the first half
	my_int_pqsort(values, nvalues, 0, 5);
	DUMP(); // => { 2, 23, 42, 45, 56, 102, 98, 63, 80, 56, }

	// sort the rest
	my_int_pqsort(values + 5, nvalues - 5, 0, nvalues);
	DUMP(); // => { 2, 23, 42, 45, 56, 56, 63, 80, 98, 102, }

	return 0;
}

pqsort's People

Contributors

freaky 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.