C++ Sort 5 numbers using only if-statements

eyong kevin
ITNEXT
Published in
4 min readSep 28, 2019

--

Sorting numbers in programming are no fun especially when you have to do it from scratch without using any build-in functions. That is when you understand this quote by Douglas Adams:

I am rarely happier than when spending entire day programming my computer to perform automatically a task that it would otherwise take me a good ten seconds to do by hand.

In this article, we will write code to sort 5 numbers using C++ only if-statements. We shall cover the following topics:

  • Algorithm to sort 3numbers
  • Code the algorithm using C++ only if-statements for 5 numbers
  • Conclusion

Algorithm to sort 3 numbers

I will start by explaining the algorithm to sort numbers using only if-statements. We will first consider sorting 3 numbers to make it less complex and understandable.

Consider the array of unsorted numbers arr= [3, 1,0]. We can sort it with the following steps:
step 1: compare indices 1 and 0. if( arr[1] < arr[0])
If this condition holds, then swap the numbers. We can see that 1 < 3 is true. So we swap and the array becomes [1,3,0]

step 2: Compare next indices 2 and 1. if(arr[2] < arr[1])
If this condition holds, then swap the numbers. We can see that 0 < 3 is true. So we swap and the array becomes [1,0,3]

Step 2.1: If condition at step 2 was true, then we repeat step 1. if(arr[1] < arr[0])
If this condition holds, then swap the numbers. We can see that 0 < 1 is true. So we swap and the array becomes [0,1,3].

We now have our sorted array [0,1,3]. We see that sorting 3 numbers requires a maximum of 3 if-statements. We shall extend what we have learned here to sort more(5) numbers.

The more the numbers, the more the if-statements that will be required to sort the numbers.

Code the algorithm using C++ only if-statements for 5 numbers

Here, I will expand the algorithm explained above to sort 5 numbers; However, I will explain directly from the code.

C++ Sorting 5 numbers using only if-statements

From the code above, we can identify 3 functions; swap(), sort(), and main().

main():

This is our main function. Here, we test the sorting function. Our sort function accepts an array of numbers and returns a pointer to the first element of the array

In C++, we can’t return an entire array

Swap():

This is a simple function that accepts 2 variables by reference and uses a temporary variable to swap the 2 variables. Here, I decided to code it from scratch, but we could still use the function std::swap() which is a built-in function in the C++ Standard Template Library (STL)

Sort():

To better understand these lines of code, let’s consider the array
a= [4,5,7,9,6]. You will notice that the first part(Part a) is the same algorithm we described above to sort 3 numbers. So Part a will sort the first three numbers of the array and we shall have a = [4,5,7,9,6]

Part a ended with the indices 2 and 1. Now Part b continues from there. Consider that at each stage of the if-statement, we increment the indices. So we have indices 3 and 2, then we follow the following steps:

  • Step b1: Compare indices 3 and 2. if(a[3] < a[2])
    If this condition holds, then swap the numbers. We can see that 9 < 7 is false. So, no swapping here.
  • Step b2: If Step a was true, then apply Part a( Step 1, Step 2 and or Step 2.1). What we realize here is that, each time we sort at a particular position, we need to make sure we sort again everything behind that position. So, Part a will make sure everything is sorted below the index position 3.

Part c continues from where Part b left. We now have the indices 4 and 3 to sort first. If follows the steps:

  • Step c1: Compare indices 4 and 3. if(a[4] < a[3])
    If this condition holds, then swap the numbers. We can see that 6< 9 is true, so we swap and get a = [4,5,7,6,9].
  • Step c2: If Step c1 was true, then apply Part b(Step b1, Stepb2). We will notice here that, Part b will also apply Part a. At the end, we will have our sorted array
    a = [4,5,6,7,9]

Conclusion

I must confess, the solution here is not pretty simple and I may not have explained it well; however, I did my best to make it as simple as possible. I am still looking for ways to reduce code complexity. One way is we could reduce redundant code. Also, a better algorithm could be used which produces less lines of code and also reduces its complexity. I am open to suggestions, so feel free to criticize, appreciate and share.

--

--

Bachelor degree in software engineering, currently working on Machine Learning, AI, Deep Learning and React. When I am not working, I produce beats.