Sorting Array Element In Ascending And Descending Order

In this page, we will array sorting array element in C++ as ascending and descending order,

In the program, the size of the array-variable is given 10, so it can store a maximum of 10 elements at a time which will be input by the user.

let’s start,

C++ Sorting Array Element as Ascending Order

Here is the program,

#include<iostream>
using namespace std;

int main()
 {
  int arr[10],size,i;

  cout<<"Enter the size of Array: "; cin>>size;

  cout<<"Enter the element in the Array\n";
    for(i=0;i<size;i++) 
      {
         cin>>arr[i];
      }

 // display entered element 
  cout<<"Before Sorting Entered Element Are\n";
    for(i=0;i<size;i++)
          cout<<arr[i]<<"\t";

 // sorting process begin
int temp;  // declare third variable for swapping

for(i=0;i<size;i++)   //outer-loop
{
   for(int j=0;j<=size;j++)  //inner-loop
    {
       if(arr[i]<arr[j]) // represent second element in the array list
        {
            temp = arr[i];    // first array element assign to variable temp
            arr[i] = arr[j]; // second element assigning to first element
            arr[j] = temp; // variable temp (means first element) assigning to second element
        }
     }
}

 // display sorting element
cout<<"\nAfter Sorting Entered Element Are\n";

for(int i=0;i<size;i++)
   cout<<arr[i]<<" ";

return 0;
 }

OUTPUT

Enter the size of Array:5

Enter the element in the Array
8   
7  
2
1     
4

Before Sorting Entered Element Are  
8       7       2       1       4  

After Sorting Entered Element Are  
1       2       4       7       8 

Explanation

In the program, the size of the array-variable is given 10, so it can store a maximum of 10 elements at a time which will be input by the user.

Suppose the user has given the size of the array 5, so 5 elements will be stored in this way,

arr[0] = 8
arr[1] = 7
arr[2] = 2
arr[3] = 1
arr[4] = 4

Now after this, the sorting process will start.

We have used two loops to sort the array elements into ascending order. Where the first array-elements will be accessed from the first-loop, while all the elements of the array from the second-loop will be compared to its first element.
here first-loop called outer-loop and second inner-loop, in the program array element will be sort as follows,

interested in more, these loop will work as follows where variable belong to outer-loop and variable j belong to inner-loop,

i =0, j = 0 size = 5
8 7 2 1 4

for(i = 0; 0 < 5; 0++) // outer loop
{
   for(int j = 0; 0 <= 5; 0++) // inner loop
   {
      if(arr[0] < arr[0])          // 8<8 8<7 8<2 8<1 8<4 all time condition becoming false 
       {                                     // so after inner-loop execution will go to outer-loop
          temp = arr[0]; 
          arr[0] = arr[1]; 
          arr[1] = temp; 
       }
    }
}

here we swap element 2 and 1 so array,

   1      7      8      2      4 
arr[0] arr[1] arr[2] arr[3] arr[4]

now again inner-loop will execute but remember here only variable j value will be increase i=3, j = 1

for(int j = 1; 1 <= 5; 1++) //inner-loop
{
  if(arr[3] < arr[1]) // 2<7 true
   {
      temp = arr[3]; // temp = 2
      arr[3] = arr[1]; // arr[2]=7
      arr[1] = temp; // 2
   }
}

here we swap element 7 and 2 now array,

   1      2      8      7     4 
arr[0] arr[1] arr[2] arr[3] arr[4]

and again inner-loop will execute,and this time value of variable j will be j =2 and variable i value will be same

for(int j = 2; 2 <= 5; 2++) //inner-loop
{
  if(arr[3] < arr[2]) // 7<8 true
   { 
     temp = arr[3]; // temp = 7
     arr[3] = arr[2]; // arr[2]=8
     arr[2] = temp; // 7
   }
}

here we swap element 8 and 7 so array now will be,

   1      2      7     8      4 
arr[0] arr[1] arr[2] arr[3] arr[4]

and again inner-loop will going to be execute but this in if-statement condition becoming false such as,

arr[3] < arr[3] 8<8

so nothing will be implement and execution will go to outer-loop

now value i =4, j = 0 , n = 5

for(i = 4; 4 < 5; 4++) //outer-loop
{
  for(int j = 0; 0 <= 5; 0++) //inner-loop
   {
      but here in if-statement first two condition false 
      arr[4] < arr[0] 4<1 //false
      arr[4] < arr[1] 4<2 // false 

but when variable j value become 2 then condition will be true, 

      if(arr[4] < arr[2]) // 4<7 true
       {
         temp = arr[4]; // temp = 4
         arr[4] = arr[2]; // arr[2]=7
         arr[2] = temp; // 4
       }
   }
}

here we swap element 7 and 4, now array

 1      2     4      8      7 
arr[0] arr[1] arr[2] arr[3] arr[4]

and again inner-loop will execute,and this time i=4, j = 3 so

for(int j = 4; 4 <= 5; 4++) //inner-loop
{
  if(arr[4] < arr[3]) // 7<8 true
   {
      temp = arr[4]; // temp = 7
      arr[4] = arr[3]; // arr[2]=8
      arr[3] = temp; // 7
   }
}

in last, we swap element 8 and 7

  1      2     4      7       8 
arr[0] arr[1] arr[2] arr[3] arr[4]

thus this loop will implement and we get sorted element,

C++ Sorting Array Element as Descending Order

ascending to descending order we only change operator < to > in if statement and other statements will be same as above program,

for(i=0;i<n;i++)
{
 for(int j=0;j<n;j++)
 {
  if(arr[i]>arr[j]) // arr[i+1] represent second element in the array list
  {
    temp = arr[i];    // first array element assign to variable temp
    arr[i] = arr[j]; // second element assigning to first element
    arr[j] = temp; // variable temp (means first element) assigning to second element
  }
 }
}

related exercise:

Like it?

Leave a Reply

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

Exit mobile version