#include <iostream.h> extern "C" { #include <stdlib.h> // for atoi() } const int MAXNUM=10; void exchange(int *a, int *b) { int t=*a; *a=*b; *b=t; } void print_list(int list[], int list_size) { for (int j=0; j<list_size; j++) cout << "list[" << j << "]=" << list[j] << endl; cout << endl; } void bubble_sort(int list[], int list_size) { int sorted = FALSE; while (!sorted) { sorted = TRUE; for (int j=0; j<list_size - 1; j++) { if (list[j] > list[j+1]) { sorted = FALSE; exchange(&list[j], &list[j+1]); } } } } main(int argc, char **argv) { int a[MAXNUM], i; const int size = argc - 1; if ((argc-1 < 2) || (argc-1 >MAXNUM)) { cerr << "Usage: " << argv[0] << " number1 number2 [... number" << MAXNUM <<"]" << endl; exit(1); } i=0; while(--argc) a[i++] = atoi(*++argv); print_list(a, size); // print unsorted bubble_sort(a, size); // sort array print_list(a, size); // print list }
© 1997 Gottfried Rudorfer, C++-AG, Lehrveranstaltungen, Abteilung für Angewandte Informatik, Wirtschaftsuniversität Wien, 3/19/1998 |