C Programming Absolute Beginner's Guide (3rd Edition)

(Romina) #1

run several times (one time for each item in the list).


An added bonus that is common to many improved bubble sort routines is the testing to see whether a
swap took place during any iteration of the inner loop. If no swap took place, the outer loop finishes
early (via a break statement). Therefore, if the loop is sorted to begin with, or if only a few passes
are needed to sort the list, the outer loop doesn’t have to finish all its planned repetitions.


Click here to view code image


// Example program #1 from Chapter 23 of Absolute Beginner's Guide
// to C, 3rd Edition
// File Chapter23ex1.c
/* This program generates 10 random numbers and then sorts them */
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
main()
{
int ctr, inner, outer, didSwap, temp;
int nums[10];
time_t t;
// If you don't include this statement, your program will always
// generate the same 10 random numbers
srand(time(&t));
// The first step is to fill the array with random numbers
// (from 1 to 100)

for (ctr = 0; ctr < 10; ctr++)
{
nums[ctr] = (rand() % 99) + 1;
}
// Now list the array as it currently is before sorting
puts("\nHere is the list before the sort:");
for (ctr = 0; ctr < 10; ctr++)
{
printf("%d\n", nums[ctr]);
}
// Sort the array
for (outer = 0; outer < 9; outer++)
{
didSwap = 0; //Becomes 1 (true) if list is not yet ordered
for (inner = outer; inner < 10; inner++)
{
if (nums[inner] < nums[outer])
{
temp = nums[inner];
nums[inner] = nums[outer];
Free download pdf