/*
 * This code is from "The Practice of Programming"
 * by Brian W. Kernighan and Rob Pike 
 * Addison Wesley Longman, 1999.
 */
#include <stdio.h>
#include <stdlib.h>

/* swap: interchange v[i] and v[j] */
void swap(int v[], int i, int j)
{
    int temp;

    temp = v[i];
    v[i] = v[j];
    v[j] = temp;
}

/* quicksort: sort v[0]..v[n-1] into ascending order */
void quicksort(int v[], int n)
{
    int i, last;

    if (n <= 1)			/* nothing to do */
	return;
    last = 0;
    for (i = 1; i < n; i++)
	if (v[i] < v[0])
	    swap(v, ++last, i);
    swap(v, 0, last);		/* restore pivot */
    quicksort(v, last);		/* recursively sort */
    quicksort(v + last + 1, n - last - 1);	/*        each part */
}

int main(int argc, char *argv[])
{
    int i, N = atoi(argv[1]), sw = atoi(argv[2]);
    int *a = malloc(N * sizeof(int));
    if (sw)
	for (i = 0; i < N; i++)
	    a[i] = 1000 * (1.0 * rand() / RAND_MAX);
    else {
	N = 0;
	while (scanf("%d", &a[N]) == 1)
	    N++;
    }
    quicksort(a, N);
    for (i = 0; i < N; i++)
	printf("%3d ", a[i]);
    printf("\n");
    return 0;
}
