Q:

Program to sort the elements of an array in ascending order

belongs to collection: Array Programs

0

Explanation

In this program, we need to sort the given array in ascending order such that elements will be arranged from smallest to largest. This can be achieved through two loops. The outer loop will select an element, and inner loop allows us to compare selected element with rest of the elements.

Original array:

Array after sorting:

Elements will be sort in such a way that smallest element will appear on extreme left which in this case is 1. The largest element will appear on extreme right which in this case is 8.

Algorithm

  1. Declare and initialize an array.
  2. Loop through the array and select an element.
  3. The inner loop will be used to compare the selected element from the outer loop with the rest of the elements of the array.
  4. If any element is less than the selected element then swap the values.
  5. Continue this process till entire array is sorted in ascending order.

Input:

arr = [5, 2, 8, 7, 1]  

Output:

Elements of original array: 5 2 8 7 1
Elements of array sorted in ascending order: 1 2 5 7 8

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Python

#Initialize array   
arr = [5, 2, 8, 7, 1];   
temp = 0;  
   
#Displaying elements of original array  
print("Elements of original array: ");  
for i in range(0, len(arr)):  
    print(arr[i], end=" ");  
   
#Sort the array in ascending order  
for i in range(0, len(arr)):  
    for j in range(i+1, len(arr)):  
        if(arr[i] > arr[j]):  
            temp = arr[i];  
            arr[i] = arr[j];  
            arr[j] = temp;  
   
print();  
   
#Displaying elements of the array after sorting  
  
print("Elements of array sorted in ascending order: ");  
for i in range(0, len(arr)):  
    print(arr[i], end=" ");  

 

Output:

Elements of original array: 
5 2 8 7 1 
Elements of array sorted in ascending order: 
1 2 5 7 8 

 

C

#include <stdio.h>  
   
int main()  
{  
    //Initialize array   
    int arr[] = {5, 2, 8, 7, 1};   
    int temp = 0;  
      
    //Calculate length of array arr  
    int length = sizeof(arr)/sizeof(arr[0]);  
      
    //Displaying elements of original array  
    printf("Elements of original array: \n");  
    for (int i = 0; i < length; i++) {   
        printf("%d ", arr[i]);   
    }    
      
    //Sort the array in ascending order  
    for (int i = 0; i < length; i++) {   
        for (int j = i+1; j < length; j++) {   
           if(arr[i] > arr[j]) {  
               temp = arr[i];  
               arr[i] = arr[j];  
               arr[j] = temp;  
           }   
        }   
    }  
      
    printf("\n");  
      
    //Displaying elements of array after sorting  
    printf("Elements of array sorted in ascending order: \n");  
    for (int i = 0; i < length; i++) {   
        printf("%d ", arr[i]);  
    }  
    return 0;  
}  

 

Output:

Elements of original array: 
5 2 8 7 1 
Elements of array sorted in ascending order: 
1 2 5 7 8 

 

JAVA

public class SortAsc {  
    public static void main(String[] args) {      
          
        //Initialize array   
        int [] arr = new int [] {5, 2, 8, 7, 1};   
        int temp = 0;  
          
        //Displaying elements of original array  
        System.out.println("Elements of original array: ");  
        for (int i = 0; i < arr.length; i++) {   
            System.out.print(arr[i] + " ");  
        }  
          
        //Sort the array in ascending order  
        for (int i = 0; i < arr.length; i++) {   
            for (int j = i+1; j < arr.length; j++) {   
               if(arr[i] > arr[j]) {  
                   temp = arr[i];  
                   arr[i] = arr[j];  
                   arr[j] = temp;  
               }   
            }   
        }  
        
        System.out.println();  
          
        //Displaying elements of array after sorting  
        System.out.println("Elements of array sorted in ascending order: ");  
        for (int i = 0; i < arr.length; i++) {   
            System.out.print(arr[i] + " ");  
        }  
    }  
}  

 

Output:

Elements of original array: 
5 2 8 7 1 
Elements of array sorted in ascending order: 
1 2 5 7 8 

 

C#

using System;  
                      
public class SortAsc  
{  
    public static void Main()  
    {  
        //Initialize array   
        int [] arr = new int [] {5, 2, 8, 7, 1};   
        int temp = 0;  
          
        //Displaying elements of original array  
        Console.WriteLine("Elements of original array: ");  
        for (int i = 0; i < arr.Length; i++) {   
            Console.Write(arr[i] + " ");  
        }  
          
        //Sort the array in ascending order  
        for (int i = 0; i < arr.Length; i++) {   
            for (int j = i+1; j < arr.Length; j++) {   
               if(arr[i] > arr[j]) {  
                   temp = arr[i];  
                   arr[i] = arr[j];  
                   arr[j] = temp;  
               }   
            }   
        }  
        
        Console.WriteLine();  
          
        //Displaying elements of array after sorting  
        Console.WriteLine("Elements of array sorted in ascending order: ");  
        for (int i = 0; i < arr.Length; i++) {   
            Console.Write(arr[i] + " ");  
        }  
    }  
}  

 

Output:

Elements of original array: 
5 2 8 7 1 
Elements of array sorted in ascending order: 
1 2 5 7 8 

 

PHP

<!DOCTYPE html>  
<html>  
<body>  
<?php  
//Initialize array   
$arr = array(5, 2, 8, 7, 1);   
$temp = 0;  
   
//Displaying elements of original array  
print("Elements of original array: <br>");  
for ($i = 0; $i < count($arr); $i++) {   
    print($arr[$i] . " ");  
}  
   
//Sort the array in ascending order  
for ($i = 0; $i < count($arr); $i++) {   
    for ($j = $i+1; $j < count($arr); $j++) {   
       if($arr[$i] > $arr[$j]) {  
           $temp = $arr[$i];  
           $arr[$i] = $arr[$j];  
           $arr[$j] = $temp;  
       }   
    }   
}  
   
print("<br>");  
   
//Displaying elements of array after sorting  
print("Elements of array sorted in ascending order: <br>");  
for ($i = 0; $i < count($arr); $i++) {   
    print($arr[$i] . " ");  
}  
?>  
</body>  
</html>  

 

Output:

Elements of original array: 
5 2 8 7 1 
Elements of array sorted in ascending order: 
1 2 5 7 8 

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Program to sort the elements of an array in descen... >>
<< Program to right rotate the elements of an array...