TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

Program to sort the elements of an array in descending order

Program to sort the elements of an array in descending order on fibonacci, factorial, prime, armstrong, swap, reverse, search, sort, stack, queue, array, linkedlist, tree, graph etc.

<< Back to PROGRAM

Q. Program to sort the elements of an array in descending order.

Explanation

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

Original array:

Program to sort the elements of an array in descending order

Array after sorting:

Program to sort the elements of an array in descending order

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

Algorithm

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

Solution

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]),
 
#Sort the array in descending 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 array after sorting
print("Elements of array sorted in descending order: ");
for i in range(0, len(arr)): 
    print(arr[i]),

Output:

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

C

#include 
 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 descending 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 descending 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 descending order: 
8 7 5 2 1 

JAVA

public class SortDsc {
    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 descending 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 descending 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 descending order: 
8 7 5 2 1 

C#

using System;
                    
public class SortDsc
{
    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 descending 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 descending 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 descending order: 
8 7 5 2 1 

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 descending 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 descending 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 descending order: 
8 7 5 2 1 

Next Topic#




Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf