Q. Write a program to sort an array elements in Descending order using Insertion Sort Algorithm.



Insertion Sort :-Insertion sort is a simple sorting algorithm that works out how to sort cards playing in our hands.

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.

Insertion Sort Algorithm

START
Step 1 : If it is the first element, it is already sorted. return 1;
Step 2 : Pick next element
Step 3 : Compare with all elements in the sorted sub-list
Step 4 : Shift all the elements in the sorted sub-list that is less than the 
value to be sorted
Step 5 : Insert the value
Step 6 : Repeat until list is sorted
STOP


Insertion Sort Program

  • C
  • C++
  • Java
  • Python
  • C#
  • PHP
  • // Insertion sort in C
    #include <stdio.h>
    void print_array(int arr[], int size)
    {
    for (int i = 0; i < size; i++)
    {
    printf("%d ", arr[i]);
    }
    printf("\n");
    }
    void insertion_sort(int arr[], int size)
    {
    for (int step = 1; step < size; step++)
    {
    int key = arr[step];
    int j = step - 1;
    while (key > arr[j] && j >= 0)
    {
    // For ascending order, change key> arr[j] to key < arr[j].
    arr[j + 1] = arr[j];
    --j;
    }
    arr[j + 1] = key;
    }
    }
    int main()
    {
    int arr[] = {15, 50, -27, -4, 10};
    int size = sizeof(arr) / sizeof(arr[0]);
    insertion_sort(arr, size);
    printf("Sorted array in descending order:\n");
    print_array(arr, size);
    }
    
    // Insertion sort in C++
    #include <iostream>
    using namespace std;
    void print_array(int arr[], int size)
    {
    for (int i = 0; i < size; i++)
    {
    cout << arr[i] << " ";
    }
    cout << endl;
    }
    void insertion_sort(int arr[], int size)
    {
    for (int step = 1; step < size; step++)
    {
    int key = arr[step];
    int j = step - 1;
    while (key > arr[j] && j >= 0)
    {
    // For ascending order, change key> arr[j] to key< arr[j].
    arr[j + 1] = arr[j];
    --j;
    }
    arr[j + 1] = key;
    }
    }
    int main()
    {
    int arr[] = {15, 50, -27, -4, 10};
    int size = sizeof(arr) / sizeof(arr[0]);
    insertion_sort(arr, size);
    cout << "Sorted array in descending order:\n";
    print_array(arr, size);
    }
    
    // Insertion sort in Java
    import java.util.Arrays;
    class LFC {
    void insertion_sort(int arr[]) {
    int size = arr.length;
    for (int step = 1; step < size; step++) {
    int key = arr[step];
    int j = step - 1;
    while (j >= 0 && key > arr[j]) {
    // For ascending order, change key> arr[j] to key< arr[j].
    arr[j + 1] = arr[j];
    --j;
    }
    arr[j + 1] = key;
    }
    }
    public static void main(String args[]) {
    int[] arr = { 15, 50, -27, -4, 10 };
    LFC is = new LFC();
    is.insertion_sort(arr);
    System.out.println("Sorted Array in descending Order: ");
    System.out.println(Arrays.toString(arr));
    }
    }
    
    # Insertion sort in Python
    def insertion_sort(arr):
    for step in range(1, len(arr)):
    key = arr[step]
    j = step - 1
    while j >= 0 and key > arr[j]:
    # For ascending order, change key> arr[j] to key < arr[j].
    arr[j + 1] = arr[j]
    j = j - 1
    arr[j + 1] = key
    arr = [15, 50, -27, -4, 10 ]
    insertion_sort(arr)
    print('Sorted Array in descending Order:')
    print(arr)
    
    using System;
    namespace InsertionSortDemo {
    class LFC {
    static void Main(string[] args) {
    int[] arr = new int[5] { 15, 50, -27, -4, 10  };
    int n = 5, i, j, val, flag;
    for (i = 1; i < n; i++) {
    val = arr[i];
    flag = 0;
    for (j = i - 1; j >= 0 && flag != 1; ) {
    if (val > arr[j]) {
    arr[j + 1] = arr[j];
    j--;
    arr[j + 1] = val;
    }
    else flag = 1;
    }
    }
    Console.Write("Sorted Array in descending Order: \n");   
    for (i = 0; i < n; i++) {
    Console.Write(arr[i] + " ");
    }     
    }
    }   
    }
    
    $arr = array(15, 50, -27, -4, 10);
    function insertion_sort($arr)
    {
    for ($i = 0; $i < count($arr); $i++) {
    $val = $arr[$i];
    $j = $i-1;
    while($j>=0 && $arr[$j] < $val){
    $arr[$j+1] = $arr[$j];
    $j--;
    }
    $arr[$j+1] = $val;
    }
    return $arr;
    }
    $sorted_arr = insertion_sort($arr);
    echo "Sorted Array in descending Order: \n"; 
    for($i = 0; $i < 5; $i++) 
    echo $sorted_arr[$i]." ";
    
    // Insertion sort in C
    #include <stdio.h>
    void print_array(int arr[], int size)
    {
    for (int i = 0; i < size; i++)
    {
    printf("%d ", arr[i]);
    }
    printf("\n");
    }
    void insertion_sort(int arr[], int size)
    {
    for (int step = 1; step < size; step++)
    {
    int key = arr[step];
    int j = step - 1;
    while (key > arr[j] && j >= 0)
    {
    // For ascending order, change key> arr[j] to key < arr[j].
    arr[j + 1] = arr[j];
    --j;
    }
    arr[j + 1] = key;
    }
    }
    int main()
    {
    int arr[] = {15, 50, -27, -4, 10};
    int size = sizeof(arr) / sizeof(arr[0]);
    insertion_sort(arr, size);
    printf("Sorted array in descending order:\n");
    print_array(arr, size);
    }
    

    Output

    Sorted array in descending order:
    50 15 10 -4 -27
    

    Recommended Programs

       Insertion Sort Algorithm And Program
       Program To Check Palindrome Number.