#include <stdio.h> void interchange(int a[], int i, int j) { int temp = a[i]; a[i] = a[j]; a[j] = temp; } int partition(int a[], int m, int p) { int v = a[m]; int i = m; int j = p; do { while (a[i] <= v && i < j) { i++; } while (a[j] > v && j > i) { j--; } if (i < j) { interchange(a, i, j); } } while (i < j); a[m] = a[j]; a[j] = v; return j; } void quick_sort(int a[], int p, int q) { if (p < q) { int j = partition(a, p, q); quick_sort(a, p, j - 1); quick_sort(a, j + 1, q); } } int main() { int a[] = {3, 6, 8, 10, 1, 2, 1}; int n = sizeof(a) / sizeof(a[0]); quick_sort(a, 0, n - 1); for (int i = 0; i < n; i++) { printf("%d ", a[i]); } printf("\n"); return 0; }
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter