<commit_before>---
layout: post
title: quick sort algorithm in C
description: quick sort algorithm in C
modified: 2015-08-05
tags: [algorithm, english]
image:
  feature: abstract-15.png
---

### The performance of quicksort

There are a lot of implementations of quick sort algorithm. Even the time complexity of the algorithm is **nlog(n)**, the real performance is highly depending on the implementation. 

### Fastest quick sort implmentation I've ever used

The following source code presents the fastest quick sort implementation that I have ever used in common cases. Morover, the code is intuitive and easy to understand.

{% highlight c %}
#include <stdio.h>

void swap(int array[], int a, int b){
	int tmp = array[a];
	array[a] = array[b];
	array[b] = tmp;
}

int partition(int array[], int low, int high){
	int last = array[high];
	int index = low;
	for (int i = low; i < high; i++){
		if (array[i] <= last){
			swap(array, i, index);
			index++;
		}
	}
	swap(array, index, high);
	return index;
}
/* quick sort function*/
void qsort(int array[], int low, int high){
	if (high <= low)
		return;
	int pivot = partition(array, low, high);
	qsort(array, low, pivot - 1);
	qsort(array, pivot + 1, high);<commit_msg>Update 2015-08-05-quicksort-in-c.md<commit_after>---
layout: post
title: quick sort algorithm in C
description: quick sort algorithm in C
modified: 2015-08-05
tags: [algorithm, english]
image:
  feature: abstract-15.png
---

### The performance of quicksort

There are a lot of implementations of quick sort algorithm. Even the time complexity of the algorithm is **nlog(n)**, the real performance is highly depending on the implementation. 

### Fastest quick sort in C 

The following source code presents the fastest quick sort implementation that <u>I have ever used</u>. Morover, the code is intuitive and easy to understand.

{% highlight c %}
#include <stdio.h>

void swap(int array[], int a, int b){
	int tmp = array[a];
	array[a] = array[b];
	array[b] = tmp;
}

int partition(int array[], int low, int high){
	int last = array[high];
	int index = low;
	for (int i = low; i < high; i++){
		if (array[i] <= last){
			swap(array, i, index);
			index++;
		}
	}
	swap(array, index, high);
	return index;
}
/* quick sort function*/
void qsort(int array[], int low, int high){
	if (high <= low)
		return;
	int pivot = partition(array, low, high);
	qsort(array, low, pivot - 1);
	qsort(array, pivot + 1, high);
{'fraction_non_alphanumeric': 0.12911184210526316, 'fraction_numerical': 0.013157894736842105, 'mean_word_length': 4.016494845360825, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3085759', 'n_tokens_mistral': 823, 'n_tokens_neox': 769, 'n_words': 327}