One of the simpler ways of sorting a group of items in an array is bubble sort. We are re-creating this method in our project.
You will find two functions in our code:
- bubble_sort Function that sorts a given array of numbers.
- bubble_sort_by Function that sorts a given array of items. It accepts blocks that compares items in the array by using yield method and return value of the block.
Both of them optimized with two optimizations:
- Decreasing number of iteration for each round.
- Swap check: break if not swapped.
- Ruby
To get a local copy up and running follow these steps:
- Ruby installed on local machine
- A browser (for usage or repl.it)
Preview with repl.it in Live Demo Link
OR:
- Fork/Clone this project to your local machine
- Open bubble_sort.rb in your local enviroment and run this code:
## Testing bubble_sort
p t_array = [4, 3, 78, 2, 0, 2]
p bubble_sort(t_array)
## Testing bubble_sort_by
p bubble_sort_by(%w[hi hello hey]) do |left, right|
left.length - right.length
end
👤 Ikraam Ghoor
- Github: @ikraamg
- Twitter: @GhoorIkraam
- LinkedIn: isghoor
- Email: consult.ikraam@gmail.com
👤 Kubilay Caglayan
- Github: @kblycaglayan
- Twitter: @kbcaglayan
- LinkedIn: Kubilay
- Email: kblycaglayan@gmail.com
Contributions, issues and feature requests are welcome!
Feel free to check the issues page
Start by:
- Forking the project
- Cloning the project to your local machine
cd
into the project directory- Run
git checkout -b your-branch-name
- Make your contributions
- Push your branch up to your forked repository
- Open a Pull Request with a detailed description to the development(or master if not available) branch of the original project for a review
Give a ⭐️ if you like this project!
- The Odin project for the project plan
This project is MIT licensed