Write a C++ program to sort an Array in Ascending order

Write a C++ program to sort an Array in Ascending order

Assignment
14

Write a C++ program to sort an Array in Ascending order.

Example 1: Without using function

Program Code for run:

#include
<iostream>   

using
namespace std;   

int
main()

{

            int a[20], n, i, j, temp;

            clrscr();

            cout << ” How many
elements to enter in array ” << endl;

            cin >> n;

            cout << ” Enter elements
of an array ” << endl;

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

               
        cin >> a[i];

 

            cout << ” Display
elements of an array: Before Sort ” << endl;

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

               
        cout << ”
a[” << i << “]:= ” << a[i] << ” \t
“;

 

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

            {

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

                        {

                                    if(a[i] >= a[j])

                                    {

                                                temp
= a[i];

                                                a[i] = a[j];

                                                a[j] = temp;

                                    }

                        }

            }

 

            cout << ” Display
elements of an array: After Sort ” << endl;

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

               
        cout << ”
a[” << i << “]:= ” << a[i] << ” \t
“;

            return 0;

}

Output
of Program:

 How
many elements to enter in array

 5

 Enter
elements of an array

 19

 12

 15

 13

 16

 Display
elements of an array: Before Sort

 a[0] :=
19       a[1] := 12        a[2] := 15        a[3] :=
13        a[4] := 16

 Display
elements of an array: After Sort

 a[0] :=
12       a[1] := 13        a[2] := 15        a[3] :=
16        a[4] := 19

 

Example 2: Using function

Program
Code for run:

#include
<iostream>   

using
namespace std;   

 

void
sortArray(int *a, int n)

{

            int temp;

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

            {

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

                        {

                                    if(*(a+i)
>= *(a+j))

                                    {

                                                temp
= *(a+i);

                                                *(a+i)
= *(a+j);

                                                *(a+j)
= temp;

                                    }

                        }

            }

}

 

int
main()

{

            int a[20], n, i, j;

            clrscr();

            cout << ” How many
elements to enter in array ” << endl;

            cin >> n;

 

            cout << ” Enter elements
of an array ” << endl;

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

               
        cin >> a[i];

 

            cout << ” Display
elements of an array: Before Sort ” << endl;

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

               
        cout << ”
a[” << i << “]:= ” << a[i] << ” \t
“;

 

            sortArray(a,n); // call function

 

            cout << ” Display
elements of an array After Sort ” << endl;

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

               
        cout << ”
a[” << i << “]:= ” << a[i] << ” \t
“;

            return 0;

}

Output
of Program:

 How many elements to
enter in array

 5

 Enter
elements of an array

 19

 12

 15

 13

 16

 Display
elements of an array: Before Sort

 a[0] :=
19       a[1] := 12        a[2] := 15        a[3] :=
13        a[4] := 16

 Display
elements of an array: After Sort

 a[0] :=
12       a[1] := 13        a[2] := 15        a[3] :=
16        a[4] := 19

Source link

Leave a Comment

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

Scroll to Top