Skip to content

Instantly share code, notes, and snippets.

@xnoreq
Last active March 26, 2022 21:07
Show Gist options
  • Save xnoreq/8ec07041f26cf6de8471013c7a526896 to your computer and use it in GitHub Desktop.
Save xnoreq/8ec07041f26cf6de8471013c7a526896 to your computer and use it in GitHub Desktop.
Quicksort in AWK
function quicksort(a, left, right, pivot, left_new, right_new) {
if (left >= right) return;
pivot = a[left + int((right - left) / 2)];
left_new = left;
right_new = right;
do {
while (a[left_new] < pivot) left_new += 1;
while (pivot < a[right_new]) right_new -= 1;
if (left_new <= right_new) {
tmp=a[left_new];
a[left_new]=a[right_new];
a[right_new]=tmp;
left_new++;
right_new--;
}
} while (left_new <= right_new);
quicksort(a, left, right_new);
quicksort(a, left_new, right);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment