Program to sort elements of an array in ascending order.

Here, we have a basic program example to sort an array in ascending order using different languages. This program is created in c language, c++, Java, and Python.

Code to sort an array in ascending order in C language

#include <stdio.h>
void main()
{
    int arr1[100];
    int n, i, j, tmp;

       printf("Input the number of elements to be stored in the array :");
       scanf("%d",&n);

       printf("Input %d elements in the array :\n",n);
       for(i=0;i<n;i++)
            {
	      printf("element - %d : ",i);
	      scanf("%d",&arr1[i]);
	    }
   for(i=0; i<n; i++)
    {
        for(j=i+1; j<n; j++)
        {
            if(arr1[j] <arr1[i])
            {
                tmp = arr1[i];
                arr1[i] = arr1[j];
                arr1[j] = tmp;
            }
        }
    }
    printf("\nElements of array in sorted ascending order:\n");
    for(i=0; i<n; i++)
    {
        printf("%d  ", arr1[i]);
    }
	        printf("\n\n");
}

Code to sort an array in ascending order in C++ language

#include <iostream>
using namespace std;

int main()
{
   int arr1[100];
    int n, i, j, tmp;

       cout<<"Input the number of elements to be stored in the array : ";
       cin>>n;

       cout<<"Input "<<n<<" elements in the array : \n";
       for(i=0;i<n;i++)
            {
	      cout<<"element - "<<i<<" : ";
	      cin>>arr1[i];
	    }
    for(i=0; i<n; i++)
    {
        for(j=i+1; j<n; j++)
        {
            if(arr1[j] <arr1[i])
            {
                tmp = arr1[i];
                arr1[i] = arr1[j];
                arr1[j] = tmp;
            }
        }
    }
    cout<<"\nElements of array in sorted ascending order:\n";
    for(i=0; i<n; i++)
    {
        cout<<arr1[i]<<" ";
    }
}

Code to sort an array in ascending order in Python language

arr1=[]
n=int(input("Input the number of elements to be stored in the array : "))
print("Input ", n , " elements in the array :")
for i in range(0,n):
    l=int(input())
    arr1.append(l)
for i in range(0,n):
    for j in range(i+1,n):
        if(arr1[i] > arr1[j]):
            tmp=arr1[i]
            arr1[i]=arr1[j]
            arr1[j]=tmp
print("Elements of array in sorted ascending order:")
for i in range(0,n):
    print(arr1[i]," ")

Code to sort an array in ascending order in Java language

import java.util.*;
public class arrays
{
  public static void main(String[] args)
   {
     int n, tmp;
     Scanner sc = new Scanner(System.in);
     System.out.print("Input the number of elements to be stored in the array : ");
     n=sc.nextInt();

    int[] arr1 = new int[n];
    int[] arr2 = new int[n];

    System.out.println("Input "+n+" elements in the array :\n");
    for(int i=0; i<n; i++)
      {
       arr1[i]=sc.nextInt();
       }
     for(int i=0; i<n; i++)
    {
        for(int j=i+1; j<n; j++)
        {
            if(arr1[j] <arr1[i])
            {
                tmp = arr1[i];
                arr1[i] = arr1[j];
                arr1[j] = tmp;
            }
        }
    }
   System.out.println("\nElements of array in sorted ascending order:\n");
    for(int i=0; i<n; i++)
    {
        System.out.println(arr1[i]+" ");
    }
	    
   }
}