ADVERTISEMENT
ADVERTISEMENT

Ruby program to sort an array in ascending order using bubble sort

Ruby Example: Write a program to sort an array in ascending order using bubble sort.
Submitted by Nidhi, on January 21, 2022

Problem Solution:

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

Program/Source Code:

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

# Ruby program to sort an array in ascending order 
# using bubble Sort

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

i=0;

while (i < 5)
  j = 4;
  while (j > i) 
    if (arr[j] < arr[j - 1]) 
      t = arr[j];
      arr[j] = arr[j - 1];
      arr[j - 1] = t;
    end
    j = j - 1
  end
  i = i + 1
end

print "Sorted Array: \n";
i=0;

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

Output:

Sorted Array: 
12 49 68 69 87 

Explanation:

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

Ruby Arrays Programs »


ADVERTISEMENT
ADVERTISEMENT


Comments and Discussions!



ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.