×

Ruby Tutorial

Ruby Basics

Ruby Control Statements

Ruby Methods

Ruby Classes and Methods

Ruby Arrays

Ruby Sets

Ruby Strings

Ruby Classes & Objects

Ruby Hash

Ruby Tools

Ruby Functions

Ruby Built-in Functions

Misc.

Ruby Programs

Ruby program to sort an array in descending order using insertion sort

Last Updated : December 15, 2025

Problem Solution

In this program, we will create an array of integers and then we will sort the created array in descending order using insertion sort.

Program/Source Code

The source code to sort an array in descending order using insertion sort is given below. The given program is compiled and executed successfully.

# Ruby program to sort an array in descending order 
# using insertion Sort

arr = [12,69,49,87,68];

i = 1;

while (i < 5) 
  item = arr[i];
  j = i - 1;
  while (j >= 0 && arr[j] < item) 
    arr[j + 1] = arr[j];
    j = j - 1;
  end

  arr[j + 1] = item;
  i = i + 1;
end

print "Sorted Array in descending order: \n";
i=0;

while(i<5)
    print arr[i]," ";
    i=i+1;
end

Output

Sorted Array in descending order: 
87 69 68 49 12 

Explanation

In the above program, we created an array of integer elements. Then we sorted the array elements in descending order using the insertion sort mechanism. After that, we printed the sorted array.

Ruby Arrays Programs »


Advertisement
Advertisement


Comments and Discussions!

Load comments ↻


Advertisement
Advertisement
Advertisement

Copyright © 2025 www.includehelp.com. All rights reserved.