|
| 1 | +--- |
| 2 | +title: "Understand Bubble Sort Algorithm" |
| 3 | +meta_title: "" |
| 4 | +description: "Short guide designed to help new to understand Bubble Sort Algorithm, by an implementation in Ruby. It includes a step-by-step guide, code examples, and an analysis of time complexity." |
| 5 | +date: 2024-09-18T15:06:22 |
| 6 | +image: '/images/algorithms.png' |
| 7 | +categories: ["Software"] |
| 8 | +author: "Diego Santos" |
| 9 | +tags: ["Algorithms", "Data structures", "Ruby"] |
| 10 | +draft: false |
| 11 | +--- |
| 12 | + |
| 13 | +Bubble Sort is a simple sorting algorithm that repeatedly steps through the list, compares adjacent elements, and swaps them if they are in the wrong order. This process continues until the list is sorted. |
| 14 | + |
| 15 | +### Understanding the Algorithm |
| 16 | +1. **Iterate through the list:** Start from the first element and compare it with the next one. |
| 17 | +2. **Compare and swap:** If the current element is greater than the next one, swap them. |
| 18 | +3. **Repeat:** Move to the next element and repeat steps 1 and 2 until you reach the end of the list. |
| 19 | +4. **Outer loop:** After one pass, the largest element will be at the end of the list. Repeat the entire process (outer loop) until the list is sorted. |
| 20 | + |
| 21 | +### Ruby Implementation |
| 22 | +Here's a Ruby implementation of Bubble Sort: |
| 23 | + |
| 24 | +```ruby |
| 25 | +def bubble_sort(array_to_sort) |
| 26 | + array_length = array_to_sort.length |
| 27 | + |
| 28 | + (1..array_length - 1).each do |
| 29 | + (1..array_length - 1).each.with_index do |i, _index| |
| 30 | + first_element = array_to_sort[i - 1] |
| 31 | + second_element = array_to_sort[i] |
| 32 | + |
| 33 | + if array_to_sort[i - 1] > array_to_sort[i] |
| 34 | + array_to_sort[i] = first_element |
| 35 | + array_to_sort[i - 1] = second_element |
| 36 | + end |
| 37 | + end |
| 38 | + end |
| 39 | + # Return the sorted array |
| 40 | + array_to_sort |
| 41 | +end |
| 42 | + |
| 43 | +# Example usage |
| 44 | +unsorted_array = [64, 34, 25, 12, 22, 11, 90] |
| 45 | +sorted_array = bubble_sort(unsorted_array) |
| 46 | +puts sorted_array |
| 47 | +``` |
| 48 | +You could see this example going to this [repository](https://github.com/Sandotech/bubble_sort) |
| 49 | + |
| 50 | +### Explanation: |
| 51 | +- **Outer loop:** Iterates `array_length - 1` times, where `array_length` is the length of the array. |
| 52 | +- **Inner loop:** Iterates from each element in thea array in each outer loop iteration. |
| 53 | +- **Comparison and swap:** Compares adjacent elements and swaps them if they are in the wrong order. |
| 54 | +- **Early termination:** If no swaps occur in an outer loop iteration, the list is already sorted, and the algorithm can terminate early. |
| 55 | + |
| 56 | +### Time Complexity |
| 57 | +Bubble Sort has a time complexity of O(n^2) in the worst-case scenario, which occurs when the array is already sorted in reverse order. In the best-case scenario (array is already sorted), the time complexity is O(n). |
| 58 | + |
| 59 | +### Conclusion |
| 60 | +Bubble Sort is a simple but inefficient sorting algorithm. While it's easy to understand and implement, it's not suitable for large datasets due to its quadratic time complexity. For more efficient sorting, consider algorithms like Merge Sort, Quick Sort, or Heap Sort. |
0 commit comments