How to Arrange 3 Numbers in Descending Order in C++ (CPP)?

Write a “C++ Program to Arrange 3 Numbers in Descending Order”

If you are new to C++ programming and want to know how to write a C++ program to arrange 3 numbers in descending order, then you landed in the right place. Keep reading 📖.

Arranging data is very important before analyzing; it helps programmers quickly analyze data or come to conclusions by watching sorted data. This article explains the best ways to sort your Array in descending order, so be with us till the end; this article is full of examples that make it relatively easy to understand. 

Before we dive deep into sorting methods, it is so essential for us to understand the concept of sorting.

 

 

What is Sorting in C++?

In C++, sorting is a concept in which we rearrange a given array or list of elements with the help of comparison operators. Sorting unsorted data helps us to analyze data quickly; with the use of sorted data, we easily find the minimum or maximum value. 

We arrange data in two manners lowest to highest (ascending order) or highest to lowest (descending order). Now understand this concept with the help of this example:

 

Unsorted data: 20, 10, 5, 15, 19

Ascending order: 5, 10, 15, 19, 20

Descending order: 20, 19, 15, 10, 5

 

 

Different Ways to Arrange 3 Numbers in Descending Order in C++

We can sort an array from lowest to highest or from highest to lowest with the help of different sorting techniques. We have other sorting techniques like a quick, heap, merge, and many more, but we will discuss the most suitable and easy sorting techniques here.

Following are the most simple and easy sorting techniques we can use for arranging 3 numbers in descending order:

  1. Using bubble sort
  2. Using insertion sort
  3. Using selection sort

 

Method 1: Using Bubble Sort to Arrange 3 Numbers

Bubble sorting is one of the most simple sorting techniques. In this sorting, we use swapping; we compare the first two values of the Array and check if the first element is smaller than the second value; if it is, we will swap those elements and move forward to the next element. If the first element is not smaller than the second, then we don’t swap them. We keep repeating this process till the end of the Array.

Now understand bubble sorting with the help of a simple example:

 

Code

// Example of Bubble sort in C++
#include <iostream>

using namespace std;




int main() {

  int data[] = { 103, 15, 91};

  

  cout << "Unsorted Array:"<<endl;

  for (int i = 0; i < 3; ++i) {

    cout << " " << data[i];

  }

  cout << "\n";




// Bubble sorting technique




  // loop to access each array element

  for (int step = 0; step < 3; step++) {

      

    // loop to compare array elements

    for (int i = step + 1; i < 3; i++) {




      // compare two adjacent elements in descending order

      // change > to < to sort in ascending order

      if (data[i] > data[step]) {




        // swapping elements if elements are not in descending order

        int temp = data[step];

        data[step] = data[i];

        data[i] = temp;

      }

    }

  }




// print sorted Array  

  cout << "Sorted Array in descending Order:\n";  

  for (int i = 0; i < 3; ++i) {

    cout << " " << data[i];

  }

  cout << "\n";   

}

 

Output

Unsorted Array:

  103 15 91

Sorted Array in descending Order:

  103 91 15

 

The main advantage of Bubble Sort is its simplicity. In bubble sort, the smallest element bubbles up to the end of the Array.

 

 

Method 2: Using Insertion Sort to Arrange 3 Numbers

In insertion sort, the elements are sorted by comparing the array value with their previous value. It starts by comparing the second value with the first value. The Array is split into two parts in this sorting, sorted and unsorted. The elements from the unsorted part are picked and placed correctly in the sorted position.

Now understand this sorting with the help of a simple example in which we arrange 3 numbers in descending order:

 

Code

// Example of Insertion sort in C++

#include <iostream>

using namespace std;




int main() {

  int data[] = { 13, 105, 91};

  

  cout << "Unsorted Array:"<<endl;

  for (int i = 0; i < 3; ++i) {

    cout << " " << data[i];

  }

  cout << "\n";




// Insertion sorting technique

int i, j, key;

    for (i = 1; i < 3; i++)

    {

        // set key value on index 1

        key = data[i];

        // set j to position 0 to 1

        j = i - 1;




 // Compare the key with each element on the left of it 

// Until an element greater than it is found

// For ascending order change data[j] < key to data[j] > key

        while (j >= 0 && data[j] < key)

        {

            data[j + 1] = data[j];

            j = j - 1;

        }

        data[j + 1] = key;

    }

    

// print sorted Array  

  cout << "Sorted Array in descending Order:\n";  

  for (int i = 0; i < 3; ++i) {

    cout << " " << data[i];

  }

  cout << "\n";

}

 

Output

Unsorted Array:

13 105 91

Sorted Array in descending Order:

105 91 13

 

 

Method 3: Using Selection Sort to Arrange 3 Numbers

In selection sorting, we select the largest value from an unsorted array in each iteration and place that value at the beginning of the unsorted list. In every iteration, the unsorted subarray size decreases by one, and the sorted subarray size increases by one. 

Let’s understand Selection sorting with the help of a simple example:

 

Code

// Example of Selection sort in C++
#include <iostream>

using namespace std;




int main() {

  int data[] = { 13, 105, 91};

  

  cout << "Unsorted Array:"<<endl;

  for (int i = 0; i < 3; ++i) {

    cout << " " << data[i];

  }

  cout << "\n";




// Selection sorting technique

for (int step = 0; step < 3 - 1; step++)

{

    int max_idx = step;

    for (int i = step + 1; i < 3; i++) 

    {




      // Select the maximum value in each loop.

      if (data[i] > data[max_idx])

        max_idx = i;

    }




    // put max at the correct position

    //swap max value

    int temp = data[max_idx];

    data[max_idx] = data[step];

    data[step] = temp;

  }    

// print sorted Array  

  cout << "Sorted Array in descending Order:\n";  

  for (int i = 0; i < 3; ++i) {

    cout << " " << data[i];

  }

  cout << "\n";

    

}

 

Output

Unsorted Array:

  13 105 91

Sorted Array in descending Order:

  105 91 13

 

 

Conclusion

To summarize the article, we have discussed arranging 3 numbers Array in descending order. We have many sorting techniques, but we discussed the most straightforward sorting techniques. In our opinion, bubble sort and insertion sort are best for sorting small arrays from highest to lowest or from lowest to highest.

Let’s have a quick recap of the topics discussed above.

  1. What is sorting?
  2. Ways to sort an array from highest to lowest.
  3. Using bubble sort for arranging 3 numbers in descending order.
  4. Using insertion sort for arranging 3 numbers in descending order.
  5. Using selection sort for arranging 3 numbers in descending order.

Finally, you significantly understand “How to write a C++ Program to Arrange 3 Numbers in Descending Order”; it’s time for your participation; comment below 👇 the most straightforward way of sorting.

 

Total
0
Shares
Leave a Reply

Your email address will not be published. Required fields are marked *

Related Posts
Total
0
Share