Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/marlonmontenegro/rubybubblesort


https://github.com/marlonmontenegro/rubybubblesort

Last synced: about 1 month ago
JSON representation

Awesome Lists containing this project

README

        

# Bubble Sort Algorithm in Ruby

The Bubble Sort exercise challenges you to implement the Bubble Sort algorithm in Ruby. Bubble Sort is a simple sorting algorithm that repeatedly steps through a list, compares adjacent elements, and swaps them if they are in the wrong order. The goal is to sort the list by "bubbling up" the largest (or smallest) elements to their correct positions.

## Exercise Description

The purpose of this exercise is to implement the Bubble Sort algorithm using Ruby. You will create a method called `bubble_sort` that takes an array as input and returns a sorted version of the array. The exercise involves iterating through the array, comparing adjacent elements, and swapping them as needed to ensure the array is sorted.

## Features

- **Sorting Logic:** The `bubble_sort` method uses nested loops to iterate through the array and swap elements as needed to achieve sorting.
- **Adjacent Element Comparison:** The algorithm compares adjacent elements and swaps them if they are in the wrong order.
- **Optimization:** After each iteration, the largest (or smallest) element is moved to its correct position, reducing the number of comparisons in subsequent iterations.
- **Output:** The method returns a sorted array.

## Example

For example, if you provide the `unsorted_array = [64, 34, 25, 12, 22, 11, 90]`, the expected output might be:
Sorted Array: [11, 12, 22, 25, 34, 64, 90]

The method correctly sorts the provided array using the Bubble Sort algorithm.