Chapter 6, Advanced Topics



Chapter 6, Advanced Topics

Topics:

1. Sorting Arrays

2. Sequentially Searching Arrays

3. Binary Search of Arrays.

Sorting Arrays (The Bubble Sort):

Sorting Data (i.e., placing the data into some particular order such as ascending or descending) is one of the most important computing applications. Many other commands designed to rapidly search data will depend upon the data being in a sorted order, also it is easier for users to something off of a big list, if it is sorted.

The first sorting algorithm we will learn is the bubble sort or the sinking sort. It is the easiest to write, but it is also the least efficient. It is called a bubble sort because the smaller values gradually “bubble” their way to the top of the list and it is called a sinking sort for exactly the opposite reason, because the larger values gradually sink to the bottom.

The technique is to make several passes through the array. On each pass, successive pairs of elements are compared. If a pair is in decreasing order, there values are swapped in the array.

Example:

/* This program sorts an array’s values into ascending order */

#include

#define SIZE 10

main ()

{

int intArray[SIZE] = {2, 6, 4, 8, 10, 12, 89, 68, 45, 37};

int intIndex, intPass, intHold;

printf (“Data items in original order\n”);

for (intIndex = 0; intIndex ................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download