C Quicksort

Thu 31 December 2009

C Quicksort eksempel. Sorterer en tabell med heltall.

{codecitation class="brush: c; gutter: true;"}

void qsort(int a[], int lo, int hi) {
{
int h, l, p, t;

if (lo < hi) {
l = lo;
h = hi;
p = a[hi];

do {
while ((l < h) && (a[l] <= p))
l = l+1;
while ((h > l) && (a[h] >= p))
h = h-1;
if (l < h) {
t = a[l];
a[l] = a[h];
a[h] = t;
}
} while (l < h);

t = a[l];
a[l] = a[hi];
a[hi] = t;

qsort( a, lo, l-1 );
qsort( a, l+1, hi );
}
}

Tagged as : c cpp csharp

Comments

Tagged as : c cpp csharp