Program to Sort an array

Program to Sort an array In Ascending and Descending order

This tutorial is based to sort an array either in ascending or descending order based on the requirement of the asked problem and condition to apply for. Program is written in C++ language.

Problem Description 

Write a Program to sort a one-dimensional array with taking the size and some random numbers from the user as input, and print the sorted elements of the array in ascending and descending order both.

 Sample input:

6

95 65 75 55 15 85

Sample output:

Elements in ascending order : 15 55 65 75 85 95

Elements in Descending order : 95 85 75 65 55 15

🐱‍🏍Logic and procedure to Sort array in Ascending order

1. Declare the required sets of variables.

2. Declare a variable n, which hold the size of the array.

3. Create the array of the size of the elements, taking to the maximum capacity it can hold.

4. Apply the loop, iterate it and take the array elements as input.

5. Now apply the required looping sets, in this case a nested loop is applied with the iterators i and j, where each element is compared with the next elements in the array.

                                i.e., for( int i = 0; i < n; i++ )

                                       {

                                                for(int j = i+1; j < n; j++)

                                         }

6. If during comparison, an element is found greater than the next element then elements are swapped using the declared temp variable and then they hold new position.

                                  i.e., if ( arr[i] > arr[j] ) then swap else continue

7. Same procedure is followed until all the elements are compared and sorted in ascending order.

8. Hence, at last the array is printed in the ascending order.

👨‍💻Required Program to sort array in ascending order

Code :

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include <iostream>
using namespace std;
int main() {
    int n,temp;

    cin>>n;

    int arr[n];

    for(int i=0;i<n;i++)

    {

        cin>>arr[i];

    }

    for(int i=0;i<n;i++)

    {

     for(int j=i+1;j<n;j++)

     {

        if(arr[i]>arr[j])

        {

            temp=arr[i];

            arr[i]=arr[j];

            arr[j]=temp;

        }

     }

    }

    cout<<"Array in ascending order : ";

    for(int i=0;i<n;i++)

    {

        cout<<arr[i]<<" ";

    }

              return 0;

}

Sample output of executed code

🐱‍🏍Logic and procedure to Sort array in Descending order

1. Declare the required sets of variables.

2. Declare a variable n, which hold the size of the array.

3. Create the array of the size of the elements, taking to the maximum capacity it can hold.

4. Apply the loop, iterate it and take the array elements as input.

5. Now apply the required looping sets, in this case a nested loop is applied with the iterators i and j, where each element is compared with the next elements in the array.

                                             i.e., for( int i = 0; i < n; i++ )

                                       {

                                                for(int j = i+1; j < n; j++)

                                         }

6. If during comparison, an element is found smaller than the next element then elements are swapped using the declared variable temp and then they hold new position.

                                  i.e., if ( arr[i] < arr[j] ) then swap else continue

7 Same procedure is followed until all the elements are compared and sorted in descending order.

8. Hence, at last the array is printed in the descending order.

👨‍💻Required Program to sort array in descending order

Code :

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include <iostream>
using namespace std;
int main() {
    int n,temp;

    cin>>n;

    int arr[n];

    for(int i=0;i<n;i++)

    {

        cin>>arr[i];

    }

    for(int i=0;i<n;i++)

    {

     for(int j=i+1;j<n;j++)

     {

        if(arr[i]<arr[j])

        {

            temp=arr[i];

            arr[i]=arr[j];

            arr[j]=temp;

        }

     }

    }

    cout<<"Array in descending order : ";

    for(int i=0;i<n;i++)

    {

        cout<<arr[i]<<" ";

    }

              return 0;

}


Sample output of executed code

Now we will we writing the code to combine both Ascending and Descending Order in one Program

Code :

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#include <iostream>
using namespace std;
int main() {
    int n,temp;

    cin>>n;

    int arr[n];

    for(int i=0;i<n;i++)

    {

        cin>>arr[i];

    }

    for(int i=0;i<n;i++)

    {

     for(int j=i+1;j<n;j++)

     {

        if(arr[i]>arr[j])

        {

            temp=arr[i];

            arr[i]=arr[j];

            arr[j]=temp;

        }

     }

    }

    cout<<"Array in ascending order : ";

    for(int i=0;i<n;i++)

    {

        cout<<arr[i]<<" ";

    }

    cout<<endl;

    for(int i=0;i<n;i++)

    {

     for(int j=i+1;j<n;j++)

     {

        if(arr[i]<arr[j])

        {

            temp=arr[i];

            arr[i]=arr[j];

            arr[j]=temp;

        }

     }

    }

    cout<<"Array in descending order : ";

    for(int i=0;i<n;i++)

    {

        cout<<arr[i]<<" ";

    }

              return 0;

}


Sample output of executed code

Program is executed at an online compiler from JDOODLE, you can visit below provided link to execute and run the same set of codes - https://www.jdoodle.com/online-compiler-c++/

If you liked this post, then share this with your friends and if anything is confusing or incorrect then let me know in the comment section.

Thanks form my side, this is Mayank, keep learning and exploring and stay safe !!

Next Post Previous Post
No Comment
Add Comment
comment url

Best Buy Products