[ Back to the overview Matrix ]

Test case : Bubble sort using ANSI/ISO C

Lines used: 35
#include <stdio.h>

#define NELEM(a) ((int)(sizeof a / sizeof *a))

int main(void)
{
    int arr[25];
    int count = 0;
    int i, notdone;

    while (count < NELEM(arr))
    {
        int tmp;
        printf(">");
        fflush(stdout);
        scanf("%d", &tmp);
        while (getchar() != '\n') ;  /* chomp newline */
        if (tmp == 0)
          break;
        if (tmp < 1 || tmp > 32000)
          continue;
        arr[count++] = tmp;
    }

    do {
        notdone = 0;
        for (i=0; i < count-1; ++i) {
            if (arr[i] > arr[i+1]) {
                int t = arr[i];
                arr[i] = arr[i+1];
                arr[i+1] = t;
                notdone = 1;
            }
        }
    } while (notdone);

    for (i=0; i < count; ++i)
      printf("%d\n", arr[i]);

    return 0;
}
Contributed by Arthur J. O`Dwyer at andrew.cmu.edu