Q. Write a program to sort an array elements in descending order using selection sort.



Selection Sort :- The selection Sort is basically the selection of the position of the element from the beginning with the other elements. Elements are compared and exchanged based on position and then the selection position is moved to the next position until it reaches the end.

Descending order :- Numbers are said to be in descending order when they are arranged from largest to smallest number. Such as 21, 17, 13, 9 and 3 are arranged in descending order.

Selection Sort Algorithm

START
   Step 1 : Set MAX to location 0
   Step 2 : Search the Maximum element in the list
   Step 3 : Swap with value at location MAX
   Step 4 : Increment MAX to point to next element
   Step 5 : Repeat until list is sorted
STOP


Selection Sort Program

  • C
  • C++
  • Java
  • Python
  • C#
  • PHP
  • #include <stdio.h>
    int main(){
     
       int i, j, temp, arr[5]={6,7,2,1,4};
     
       // Logic of selection sort algorithm
       for(i=0;i<5;i++){
          for(j=i+1;j<5;j++){
             if(arr[i]< arr[j]){
                temp=arr[i];
                arr[i]=arr[j];
                arr[j]=temp;
             }
          }
       }
    
       printf("Sorted elements: ");
       for(i=0;i<5;i++)
          printf(" %d",arr[i]);
    
       return 0;
    }
    
    // Selection sort in C++
    
    #include <iostream>
    using namespace std;
    
    
    void selection_Sort(int arr[], int size)
    {   int temp;
        for(int i=0;i< size;i++){
          for(int j=i+1;j< size;j++){
             if(arr[i]< arr[j]){
                temp=arr[i];
                arr[i]=arr[j];
                arr[j]=temp;
             }
          }
       }
    }
    void print_Array(int arr[], int size)
    {
      for (int i = 0; i < size; ++i)
      {
        cout << "  " << arr[i];
      }
      cout << "\n";
    }
    int main()
    {
      int number[] = {6, 7, 2, 1, 4};
      int size = sizeof(number) / sizeof(number[0]);
      selection_Sort(number, size);
      cout << "Sorted Array :\n";
      print_Array(number, size);
    }
    
    // Selection sort in Java
    
    public class LFC {
       public static void main(String args[]){
          int arr[] = {6, 7, 2, 1, 4};
          int size = arr.length;
    
          for (int i = 0 ;i< size-1; i++){
             int min = i;
    
             for (int j = i+1; j< size; j++){
                if (arr[j] > arr[min]){
                min = j;
                }
             }
             int temp = arr[min];
             arr[min] = arr[i];
             arr[i] = temp;
          }
    
          for (int i = 0 ;i< size; i++){
             System.out.print(" "+arr[i]);
          }
       }  
    }
    
    
    
    
    Arr = [ 6, 7, 2, 1, 4 ];
    for i in range(len(Arr)):
        min_= i
        for j in range(i+1, len(Arr)):
            if Arr[min_] < Arr[j]:
                min_ = j
        #swap
        Arr[i], Arr[min_] = Arr[min_], Arr[i]
    # main
    
    print(Arr)
    
    using System;
    
    public class LFC {
       static void Main(string[] args) {
          int[] arr = new int[5] { 6, 7, 2, 1, 4 };
          int n = 5; 
          
          int temp, smallest;
          for (int i = 0; i < n - 1; i++) {
             smallest = i;
             for (int j = i + 1; j < n; j++) {
                if (arr[j] > arr[smallest]) {
                   smallest = j;
                }
             }
             temp = arr[smallest];
             arr[smallest] = arr[i];
             arr[i] = temp;
          }
         
          Console.Write("Sorted array is: ");  
          for (int i = 0; i < n; i++) {
             Console.Write(arr[i] + " ");
          }
       }
    }
    
    
    function selection_sort(&$arr, $n)  
    { 
        for($i = 0; $i < $n ; $i++) 
        { 
            $low = $i; 
            for($j = $i + 1; $j < $n ; $j++) 
            { 
                if ($arr[$j] > $arr[$low]) 
                { 
                    $low = $j; 
                } 
            } 
              
            // swap the minimum value to $ith node 
            if ($arr[$i] < $arr[$low]) 
            { 
                $tmp = $arr[$i]; 
                $arr[$i] = $arr[$low]; 
                $arr[$low] = $tmp; 
            } 
        } 
    } 
      
    // Driver Code 
    $arr = array(6, 7, 2, 1, 4); 
    $len = count($arr); 
    selection_sort($arr, $len); 
    echo "Sorted array : \n";  
      
    for ($i = 0; $i < $len; $i++)  
        echo $arr[$i] . " ";  
        
    
    
    #include <stdio.h>
    int main(){
     
       int i, j, temp, arr[5]={6,7,2,1,4};
     
       // Logic of selection sort algorithm
       for(i=0;i<5;i++){
          for(j=i+1;j<5;j++){
             if(arr[i]< arr[j]){
                temp=arr[i];
                arr[i]=arr[j];
                arr[j]=temp;
             }
          }
       }
    
       printf("Sorted elements: ");
       for(i=0;i<5;i++)
          printf(" %d",arr[i]);
    
       return 0;
    }
    

    Output

    Sorted Array: 7 6 4 2 1
    

    Recommended Programs

       Selection Sort Program in Ascending Order
       Insertion Sort Program & Algorithm