#include <stdio.h>
#include <stdlib.h>
void quickSort(int *ad, int iLen)
{
int i,j,v,flag, pos;
v = ad[0];
i = 0;
j = iLen - 1;
flag = 1;
pos = 0;
while (i < j)
{
if (flag > 0)
{
if (ad[j] >= v)
j --;
else
{
ad[i] = ad[j];
ad[j] = v;
pos = j;
flag = 0;
}
}
else
{
if(ad[i] <= v)
i ++;
else
{
ad[j] = ad[i];
ad[i] = v;
pos = i;
flag = 1;
}
}
}
if (pos > 1)
quickSort(ad, pos);
if ((iLen - pos - 1) > 1)
quickSort(ad + pos + 1, iLen - pos - 1);
}
int main(int argc, char *argv[]) {
int a[] = {1,8,3,6,2,9,1,1,4,5};
int iLen = 10;
int i = 0;
quickSort(a,iLen);
while (i < iLen)
{
printf("%d ", a[i]);
i ++;
}
return 0;
}
// quick_sort.go
package main
import (
"fmt"
)
func findKth(a []int, K int) int {
iLen := len(a)
var i, j, v, flag, pos int
i = 0
j = iLen - 1
flag = 1
v = a[0]
for i < j {
if flag > 0 {
if a[j] >= v {
j--
} else {
a[i], a[j] = a[j], a[i]
pos = j
flag = 0
}
} else {
if a[i] <= v {
i++
} else {
a[i], a[j] = a[j], a[i]
pos = i
flag = 1
}
}
}
if pos > 1 {
findKth(a[:pos], K)
}
if (iLen - pos - 1) > 1 {
findKth(a[pos+1:], K)
}
return 0
}
func main() {
var a = []int{1332802, 1177178, 1514891, 871248, 753214, 123866, 1615405, 328656, 1540395, 968891, 1884022, 252932, 1034406, 1455178, 821713, 486232, 860175, 1896237, 852300, 566715, 1285209, 1845742, 883142, 259266, 520911, 1844960, 218188, 1528217, 332380, 261485, 1111670, 16920, 1249664, 1199799, 1959818, 1546744, 1904944, 51047, 1176397, 190970, 48715, 349690, 673887, 1648782, 1010556, 1165786, 937247, 986578, 798663}
findKth(a, 24)
for i := 0; i < len(a); i++ {
fmt.Printf("%d:%d \n", i, a[i])
}
}