bubblesort
Copy the following code into a file called bubblesort.c.
#include<stdio.h> #include<stdlib.h> #include <math.h> #include <time.h> #define DEFAULT_SIZE 100 #define NL printf("\n") /****************************************************************************** Housekeeping methods... *****************************************************************************/ inline void SWAP(int *a, int j, int k) { if (j!=k) {a[j] ^= a[k]; a[k] ^= a[j]; a[j] ^= a[k];} } void printArrayP(int * a, int N, int p) { int i = 0; while (i < N) { if ( i % 10 == 0 ) printf("\n%7i ]", i); if (i==p) printf(" |%5i", a[i]); else printf("%7i", a[i]); i++; } NL; } void printArray(int * a, int N) { int i = 0; while (i < N) { if ( i % 10 == 0 ) printf("\n%7i ]", i); printf("%7i", a[i]); i++; } NL; } /****************************************************************************** Sort the array a with size N using a BUBBLE SORT algorithm *****************************************************************************/ void bubbleSort( int a[], int N ) { int i,j; for (i=N-1; i>0; i--) { #ifdef SHOW printf("Bubbling...\n"); #endif for (j=0; j<i; j++) { if (a[j] > a[j+1]) SWAP(a,j,j+1); #ifdef SHOW printArrayP(a,N,i);printf("\n"); #endif } } } /** * Bubblesort - Uncluttered version without all the SHOW stuff */ void bubbleSort_( int a[], int N ) { int i,j; for (i=N-1; i>0; i--) for (j=0; j<i; j++) if (a[j] > a[j+1]) SWAP(a,j,j+1); } main(int argc, char ** argv) { int N; /* how many numbers? */ int modulo; int * nums; /* where are they ? */ int i; /* loop counter */ // Find out how many numbers are needed and dynamically allocate an array printf("To specify array size and seed the random number generator:\n"); printf("%s <number> <seed>\t e.g. %s 10 47912\n",argv[0],argv[0]); N = (argc > 1) ? atoi(argv[1]) : DEFAULT_SIZE; srand((argc > 2) ? atoi(argv[2]) : clock()); modulo = (int)(pow(10, (floor(log10(N))+1))); printf("Generating %i random numbers modulo %i\n", N, modulo); nums = (int*) malloc(sizeof(int)*N); for(i=0; i<N; i++) nums[i] = rand() % modulo; // Print out the randomly generated sequence for(i=0; i<N; i++) printf(" %i ", nums[i]); printf("\n"); bubbleSort(nums,N); // Print out the sorted sequence for(i=0; i<N; i++) printf(" %i ", nums[i]); printf("\n"); }
This program performs a bubble sort. You saw this earlier because it was used to sort the array in binarysearch1. Note the following points:
- We have used conditional compilation so that you can switch on/off the display of the array at each stage of the sorting process. If you look at the sorting function you will see the relevant #ifdef / #endif directives.
- The main program creates an array of random numbers and uses bubble sort to sort them into numerical order.
- The program has a number of helper functions that are used to swap values and print arrays etc. Without these it would be very difficult to demonstrate what is going on.
Compile the program and run it with a variety of different array sizes. NB: Start small if you switch the SHOW option on - it will print the array at every stage of the bubble sort. We would suggest an array of size five to get you started.
gcc bubblesort.c -DSHOW -o bubblesort