Easy Tutorial: Computer Programming for DUMMIES (sorting)

Hey guys! Here is another basic programming tutorial. This tutorial is about sorting arrays. We can sort numbers in ascending or descending order. The algorithm I will show you is pretty simple. I will be coding in C++, but the algorithm will still apply to other languages. This is part eight of my tutorials. If you want to catch up on my previous tutorials, here they are:

Part 1: Hello World!

Part 2: Variables

Part 3: Functions

Part 4: if(), else, else if()

Part 5: Loops

Part 6: Arrays

Part 7: Basic Input/Output

Bubble sort

This is probably the simplest sort algorithm. It compares two numbers in an array at a time, starting with the first two. If the first number is bigger than the second, then they swap positions. It then compares the second number to the third number, and so on... Once the last two numbers are compared, the largest value will be at the end. The sort then starts all over, but ignores the last number. Here is an example:

  1. 8 3 6 1 -- 8 is bigger than 3, swap
  2. 3 8 6 1 -- 8 is bigger than 6, swap
  3. 3 6 8 1 -- 8 is bigger than 1, swap
  4. 3 6 1 - 8 -- 8 is now in the right position
  5. 3 6 1 - 8 -- 3 is smaller than 6, don't swap
  6. 3 6 1 - 8 -- 6 is bigger than 1, swap
  7. 3 1 - 6 8 -- 6 is now in the right position
  8. 3 1 - 6 8 -- 3 is bigger than 1, swap
  9. 1 3 6 8 -- They are in the correct order.

Here is a gif from Wikipedia:

Here is how you implement the bubble sort in C++:
arr[]: the array being sorted
arrSize: (integer) the number of elements in the array

for(int x = 0; x < arrSize; x++)
{
    for(int y = 0; y < arrSize-x-1; y++)
    {
        if(arr[y] > arr[y + 1])
        {
            int temp = arr[y];
            arr[y] = arr[y + 1];
            arr[y + 1] = temp;
        }
    }
}

I realize that nested for() loops may be a bit confusing at first, so let's walk through this. The first for loop represents every occasion the sort goes from the beginning to the end of the array (remember, it starts over until all of the elements are sorted -- this may mean that it swaps elements until there are only two elements left to swap). Each iteration of this loop in my example would be numbers 1-4, 5-7, and 8-9 (3 iterations). The second for() loop represents comparing each to the next. Look at "y < arrSize-x-1." The "-x" is there because after each iteration, the last comparison is unnecessary. "x" increments by one every iteration, so subtracting "x" will keep one extra comparison from happening each iteration. Also, to swap values, you need a temporary variable to hold one value so that it doesn't get lost. That is what "temp" is for. If you want to sort in descending order, you just have to change the greater than sign to a less than sign on line five.

Here is a program I wrote just to apply this sort to a working example:

#include<iostream>
using namespace std;

void bubble_ascending(int arr[], int arrSize)
{
    for(int x = 0; x < arrSize; x++)
    {
        for(int y = 0; y < arrSize-x-1; y++)
        {
            if(arr[y] > arr[y + 1])
            {
                int temp = arr[y];
                arr[y] = arr[y + 1];
                arr[y + 1] = temp;
            }
        }
    }
}

void bubble_descending(int arr[], int arrSize)
{
    for(int x = 0; x < arrSize; x++)
    {
        for(int y = 0; y < arrSize-x-1; y++)
        {
            if(arr[y] < arr[y + 1])
            {
                int temp = arr[y];
                arr[y] = arr[y + 1];
                arr[y + 1] = temp;
            }
        }
    }
}       

int main()
{
    int arr[10];
    cout << "Enter 10 integers: ";
    for(int x = 0; x < 10; x++)
        cin >> arr[x];

    bubble_ascending(arr, 10);
    cout << "Ascending order: ";
    for(int x = 0; x < 10; x++)
        cout << arr[x] << " ";
    cout << endl;
    
    bubble_descending(arr, 10);
    cout << "Descending order: ";
    for(int x = 0; x < 10; x++)
        cout << arr[x] << " ";
    cout << endl;
    
    return 0;
}

Here is the output (user input in bold):


[cmw4026@omega test]$ g++ sort.cpp
[cmw4026@omega test]$ ./a.out
Enter 10 integers: 6 3 56 98 12 56 309 21 555 -8
Ascending order: -8 3 6 12 21 56 56 98 309 555
Descending order: 555 309 98 56 56 21 12 6 3 -8
[cmw4026@omega test]$


The parameters for each function is simply the array to be sorted, then the number of elements in the array.

I hope this was useful! Bubble sort is not the fastest sort algorithm, but is is probably the easiest. It is fine because speed will not matter unless you are sorting a HUGE data set! Leave a comment if you have any suggestions, or want a faster algorithm.

Sort:  

Elecrowarrior here thanks for the invite. I like to program in c and c++. Usually my Arduino robots are my thing although I'm still a novice. Thank you. P.s. followed

Coin Marketplace

STEEM 0.20
TRX 0.12
JST 0.028
BTC 64182.86
ETH 3531.12
USDT 1.00
SBD 2.53