Skip to main content
Engineering LibreTexts

21.2: Sorting an Array

  • Page ID
    10535
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \) \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)\(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\) \(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\)\(\newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    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:

    Bubble

    Selection

    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."

    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.

    Figure \(\PageIndex{1}\)

    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:

    envision the lighter oil rising to the top; OR

    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.

    Footnotes

    Tony Gaddis, Judy Walters and Godfrey Muganda, Starting Out with C++ Early Objects Sixth Edition(United States of America: Pearson – Addison Wesley, 2008) 569.


    This page titled 21.2: Sorting an Array is shared under a CC BY license and was authored, remixed, and/or curated by Kenneth Leroy Busbee (OpenStax CNX) .

    • Was this article helpful?