Skip to content

Instantly share code, notes, and snippets.

@zhanglintc
Last active March 9, 2021 07:01
Show Gist options
  • Save zhanglintc/2e718bcb12897f8303e8a1c63f4df8f9 to your computer and use it in GitHub Desktop.
Save zhanglintc/2e718bcb12897f8303e8a1c63f4df8f9 to your computer and use it in GitHub Desktop.
qsort
private void qsort(int[] a, int bl, int br) {
if (bl >= br) return;
int m = a[bl], l = bl, r = br;
while (l < r) {
while (l < r && a[r] >= m) r--;
if (l < r) a[l++] = a[r];
while (l < r && a[l] <= m) l++;
if (l < r) a[r--] = a[l];
}
a[l] = m;
qsort(a, bl, l - 1);
qsort(a, l + 1, br);
}
def qsort(a):
def _qsort(a, bl, br):
if not a or bl >= br: return
m = a[bl]; l = bl; r = br
while l < r:
while l < r and a[r] > m: r -= 1
if l < r: a[l] = a[r]; l += 1
while l < r and a[l] < m: l += 1
if l < r: a[r] = a[l]; r -= 1
a[l] = m
_qsort(a, bl, l - 1)
_qsort(a, l + 1, br)
_qsort(a, bl = 0, br = len(a) - 1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment