<< Chapter < Page Chapter >> Page >
An introduction and example of sorting an array within the C++ programming language.

Overview

Sorting is the process through which data are arranged according to their values. There are several sorting algorithms or methods that can be used to sort data. Some include:

  1. Bubble
  2. Selection
  3. Insertion

We will not be covering the selection or insertion sort methods in this module.

"The bubble sort is an easy way to arrange data in ascending or descending order. If an array is sorted in ascending order, it means the values in the array are stored from lowest to highest. If values are sorted in descending order, they are stored from highest to lowest. Bubble sort works by comparing each element with its neighbor and swapping them it they are not in the desired order." Tony Gaddis, Judy Walters and Godfrey Muganda, Starting Out with C++ Early Objects Sixth Edition (United States of America: Pearson – Addison Wesley, 2008) 569.

There are several different methods of bubble sorting and some methods are more efficient than others. Most use a pair of nested loops or iteration control structures. One method sets a flag that indicates that the array is sorted, then does a pass and if any elements are exchanged (switched); it sets the flag to indicate that the array is not sorted. It is executed until it makes a pass and nothing is exchanged.

The bubble sort gets its name from the lighter bubbles that move or "bubble up" to the top of a glass of soda pop. We move the smaller elements of the array to the top as the larger elements move to the bottom of the array. This can be viewed from a different perspective. Using an Italian salad dressing with oil, water and herbs; once shaken you can either:

  1. envision the lighter oil rising to the top; OR
  2. envision the heaver water and herbs sinking to the bottom

Either way is correct and this version of the code simply demonstrates the sinking to the bottom the heaver or larger elements of the array.

Bubble sorting is demonstrated in the demo file provided, thus you need to study this material in conjunction with the demo program.

Demonstration program in c++

Creating a folder or sub-folder for source code files

Depending on your compiler/IDE, you should decide where to download and store source code files for processing. Prudence dictates that you create these folders as needed prior to downloading source code files. A suggested sub-folder for the Bloodshed Dev-C++ 5 compiler/IDE might be named:

  • Demo_Programs

If you have not done so, please create the folder(s) and/or sub-folder(s) as appropriate.

Download the demo program

Download and store the following file(s) to your storage device in the appropriate folder(s). Following the methods of your compiler/IDE, compile and run the program(s). Study the source code file(s) in conjunction with other learning materials. You may need to right click on the link and select "Save Target As" in order to download the file.

Download from Connexions: Demo_Sort_Array_Function.cpp

Download from Connexions: Demo_Farm_Acres_Input.txt

Definitions

sorting
Arranging data according to their values.
bubble sort
A method of swapping array members until they are in the desired sequence.

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, My first collection. OpenStax CNX. Aug 03, 2009 Download for free at http://cnx.org/content/col10870/1.1
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'My first collection' conversation and receive update notifications?

Ask