Sorting algorithms are some of the earliest that you typically get exposed to in Computer Science. It may not be immediately obvious how important they are, but it shouldn't be hard to think of some examples where your computer needs to sort some massive datasets during everyday operations.
One of the simpler (but more processor-intensive) ways of sorting a group of items in an array is bubble sort, where each element is compared to the one next to it and they are swapped if the one on the left is larger than the one on the right. This continues until the array is eventually sorted.
Check out this video from Harvard's CS50x on Bubble Sort.
There's also an entry on Bubble Sort on Wikipedia that's worth taking a look at.
- Build a method
#bubble_sort
that takes an array and returns a sorted array. It must use the bubble sort methodology (using#sort
would be pretty pointless, wouldn't it?).
> bubble_sort([4,3,78,2,0,2])
=> [0,2,2,3,4,78]
- Now create a similar method called
#bubble_sort_by
which sorts an array by accepting a block. Remember to use yield inside your method definition to accomplish this. The block will have two arguments that represent the two elements of the array that are currently being compared. The block’s return will be similar to the spaceship operator you learned about before: If the result of the block execution is negative, the element on the left is “smaller” than the element on the right.0
means both elements are equal. A positive result means the left element is greater. Use the block’s return value to sort your array. Test your method with the example below.
> bubble_sort_by(["hi","hello","hey"]) do |left,right|
> left.length - right.length
> end
=> ["hi", "hey", "hello"]
Submit a link below to this file on the ruby course github repo with your files in it by using a pull request. See the section on Contributing for how.
Show Student Solutions
- Add your solution below this line!
- Olugbade Olalekan's Solution
- Robin's Solution
- Run After's Solution
- Noah T's Solution
- Allan Glasier's Solution
- Billy Coffin's Solution
- Michael Marchand's Solution
- Ian's Solution
- Carlos Diaz's Solution
- Saranoya's Solution
- mistervoga's Solution
- Chris' Solution
- Sherman's Solution
- Alain Suarez's Solution
- Rafe Draper's Solution
- Jay Burbyga's Solution
- Nicolás Nisoria's Solution
- Jose Salvador's Solution
- Sarp's Solution
- Loumarven's Solution
- Han Josmer's Solution
- Jacob Thompson's Solution
- Quilmes's Solution
- Arilson Souza
- Austin Fisher's Solution
- Jacques Hovine's Solution
- Vollantre's Solution
- Servante's Solution
- Leonardo Vega's Solution
- Braxton Lemmon's Solution
- BShowen's Solution
- Kevin Vuong's Solution
- Origier's Solution
- Tyler Devers's Solution
- Learnsometing's Solution
- Tommy's Solution
- Akbar's Solution
- Khari's Solution
- RunningCoder123's solution
- WormCrew's solution
- Paul Manot's solution
- Stefano Merazzi's solution
- Chris Wegscheid's solution
- Simon Tharby's solution
- Smetanca52 Solution
- Hummeldon's Solution
- wuaangela's solution
- Tobenski's solution
- Emil Dimitrov's Solution
- gfioravante's Solution
- Mohamed Elattar's Solution
- Ajani Stewart's Solution
- Ahmed Ali's Solution
- Tim Dowd's Solution
- sametcilli's Solution
- Max Garber's Solution
- Malaika (Mic) Solution
- Ian Devins's Solutions
- Keith Amundson's Solutions
- Ngo Van Huong's Solutions
- Nathan Sherburne's Solutions
- Jon Yoo's Solutions
- Henry Kirya's Solution
- Ryan's Solution
- Sherman Bowling's Solution
- Javier Machin's Solution
- Samuel Masters' Soultion
- Marijunjnja's Solution
- Kevin Wahome's Solution
- Turbopro's Solution
- Sava Vuckovic's solution
- MariaTikhonova's solution
- WottatoParrior's solution
- funwithcthulhu's solution
- Bruno Parga's solution
- isildonmez's solution
- Omar Moataz's solution (includes interesting find on what takes precedence when using puts)
- jdrobertso's solution
- rublen's solution
- endotnick's solution
- Jonathan Yiv's solution
- Jason McKee's solution
- Andrew's solution
- MGiagante's solution
- Bn8's Solution
- GuyInALabCoat's Solution
- Ben Deltenre's Solution
- justinckim3's Solution
- Kasey Z.'s Solution
- Demo318's Solution
- Dallaire's Solution
- SadieD's solution
- Grey-Ghost's solution
- Arron's Solution
- xavier solution
- Webdev-burd's Solution
- BenBrewerBowman's Solution
- jfonz412's solution
- Owen Thomas Clark's solution
- João Roberto's solution
- mindovermiles262's Solution
- dfan14051's solution
- Oleh Sliusar's solution
- RichJDSmith's solution
- theghall's solution
- Ovsjah Schweinefresser's solution
- Neelotpal's solution
- yilmazgunalp's solution
- Yash Anand's solution
- Nicolas Amaya's solution
- John Phelps's solution
- Josh Vogel's solution
- Austin Norman's solution
- Jib's Solution(NuclearMachine)
- Chad Kreutzer's solution
- Stefan P's Solution
- Raiko's Solution
- Jamie's solution
- Chris Oldakowski's solution
- Marina Sergeyeva's solution
- Afshin M. afshinator's solution
- Donald's solution
- Alan Russell's solution
- Sessl's solution
- Tommy Noe's solution
- Michael Alexander's solution
- Adrian Badarau's solution
- James MacIvor's solution
- Roman's Solution
- Aleksandar's solution
- Lara Finnegan's solution
- John Quarles' solution
- Jack Nguyen's solution
- Vidul's solution
- Maggie Baker's solution
- Hunter Ducharme's solution
- Artur Janik's solution
- Kate McFaul's solution
- Dominik Stodolny's solution
- Chris Dziewa's solution
- Josh Naughton's solution
- Jason Matthews' solution
- Ali Ayoub's solution
- JrodManU's solution
- Sasi kala's solution
- Kevin Mulhern's solution
- Jeremy Mauzy's solution
- LongPotato's solution
- Dorian Iacobescu's solution
- Sam Padrul's solution
- Eleanor Weigert's solution
- Lyman Wong's solution
- omokoro's solution
- Andrej Dragojevic's solution
- Brann James' solution
- Eduardo Frias' solution
- Antonio Augusto's solution
- Josh Klein's solution
- ll14m4n's solution
- AtActionParks's solution
- Matias Pan's solution
- Mark Viola's solution
- Bhupendra Singh's solution
- Dan Hoying's solution
- Joe Balsamo's solution
- Cody Gipson's solution
- Glenn Crosby's solution
- Raycotek's solution
- M. Edgar Joel's solution
- Miguel Oliveira's solution
- Xavier Reid's solution
- Gb69010p's solution
- Alex Chen's solution
- Aleksandre Clapin-Pepin's solution
- John Tobillo's Solution
- PiotrAleksander's solution
- Jason Symons' solution
- AnthonyL's solution
- Angus Dobson's solution
- Panashe Fundira's solution
- pwdd's solution
- Paweł Cichoń solution
- Noah Prescott's solution
- Florian Mainguy's solution
- Aviv levinsky's solution
- Alex Tsiras' solution
- Scott Bobbitt's solution
- Maciej Panasiewicz's solution
- Simon Kraus' solution
- Giorgos Mitsis's solution
- Theoderik Trajanson's solution
- Sander Schepens's solution
- srashidi's solution
- Eric's solution
- Dave Meister's solution
- Andrew Park's solution
- Austin Mason's solution
- James Brooks's solution
- arocketman's solution
- Ricardo Villegas' solution
- cdouglass's solution
- Skye Free's solution
- Corey Kazaks's solution
- Dominik Chomicki's solution
- Matt Leininger's solution
- Miguel Herrera's solution
- Maia Petee's solution
- Peuchen's solution
- Andrew Johnson's solution
- Bartlomiej Lazarski's solution
- Francisco Carlos's solution
- Luke Walker's solution
- Noobling's solution
- Max Gallant's solution
- Jean Merlet's solution
- parhaml's solution
- Zac Conner's solution
- John Martinez's solution
- Vivek Kumar's solution
- Benjamin Contant's solution
- DV's solution
- Fabricio Carrara's solution
- Omar Cagua's solution
- Deepak's solution
- Lani Huang's solution
- Anthony Vumbaca's solution
- John Connor's solution
- Shala Qweghen's solution
- Earth35's solution
- Kelvin Stone's solution
- Ricardo Ferreira's solution
- Cyprium (Stefan)'s solution
- Michael Sotkin)'s solution
- Shawn Stovall's solution
- Joshua Hipple's solution
- Oscar Y.'s solution
- Adam Graham's solution
- Luchillo's solution
- Rahul's solution
- Clint's
- Clint's
- Jiazhi Guo's solution
- Zuqi Chen's solution
- Manu Phatak's solution
- Cody Buffaloe's solution
- J-kaizen's solution
- Max Platt's solution
- Roy Chen's solution
- Piotr Ejsmont's solution
- Chris Chambers' solution
- Loris Aranda's solution
- Jack Deegan's solution
- Jakub Peikert's solution
- HenrytheDJ's solution
- Derek Kwong's solution
- Sergey Badulin's solution
- Dom Goj's solution
- grzegorzzajac1989's solution
- Jason Keeney's solution
- Hassan's solution
- Bishal's solution
- Kaaviya's solution
- Jerry Gao's solution
- Saroar's solution
- Mike Coon's solution
- Tom Westerhout's solution bubblee sort - Bubble_sort_by
- Sophia Wu's solution
- Smiles's solution
- Joe Himes's solution
- Braydon's solution
- Ryan Barnett's solution
- Kaique "Rique" Borges' solution
- jeff1st's solution
- Niño Mollaneda's solution
- Roan Fourie's solution
- Robert Szabo's solution
- Alan Cruse's solution
- Parker Brown's solution
- Vaclav Skvaril's solution
- Øistein Haugland's solution
- Luján Fernaud's solution
- hs7's solution
- EMuchynski's solution
- Swojeet's solution
- Paritosh's solution
- Roland Studer's Solution
- Anistor86's Solution
- Andy DeNike's Solution
- Iceron's Solution
- coryparham24's Solution
- Oliver Curting's Solution
- Alex's Solution
- HuyAnh's Solution
- Santiago Rodríguez Solution
- Xuan-Son Trinh's Solution
- Kayleigh's Solution
- NJWs Solution
- Niko Caron's Solution
- noloman's solution
- HSaad's Solution
- MPalhidai's Solution
- Alvaro Sanchez's Solution
- Punnadittr's Solution
- cartwheeler's Solution
- Agon's Solution
- Areeba's Solution
- mojotron's Solution
- Akash's Solution
- Felipe Parreira's Solution
- Amy Smith's Solution
- dmarkiewicz's Solution
- codyMalcolm's Solution
- Tommy's Solution
- bchalman's Solution
- Rudi Boshoff's Solution
- Slaven Karamatic's Solution
- jkriesp's Solution
- EdwardHeath's Solution
- Khalal's Solution
- Leila Alderman's solution
- Vitaly Osipov's solution
- Junwen's Solution
- vanny96's Solution
- Joanna's Solution
- JamCry's Solution
- Wesley Wang's Solution
- Sergej Jurchenko's Solution
- Alex Krewson's Solution
- Bendee48's Solution
- keskiviikko's Solution
- Ben Fowler's Solution
- Brett McStotts' Solution
- LastCapricorn's Solution
- Tronerta's Solution
- Brett Bonnet's Solution
- Leo Holanda's Solution
- Barbara Lucas' Solution
- Muminjon's Solution
- Tristan Ross's Solution
- Robert Dunbar's Solution
- Gareth Woodman's Solution
- Konrad Zagozda's Solution
- Carlos Del Real's and António Fernandes's Solution
- Jay Conner's Solution
- Adriel Bruno's Solution
- David Watkin's Solution
- Cameron St. Amant's Solution
- Robert Suazo's Solution
- DalandanJuice's SOlution
- Rafael Rodriguez Garcia's Solution
- hyperturing's Solution
You learned about the Enumerable module that gets mixed into the Array and Hash classes (among others) and provides you with lots of handy iterator methods. To prove that there's no magic to it, you're going to rebuild those methods.
- Create a script file to house your methods and run it in IRB to test them later.
- Add your new methods onto the existing Enumerable module. Ruby makes this easy for you because any class or module can be added to without trouble ... just do something like:
module Enumerable
def my_each
# your code here
end
end
- Create
#my_each
, a method that is identical to#each
but (obviously) does not use#each
. You'll need to remember theyield
statement. Make sure it returns the same thing as#each
as well. - Create
#my_each_with_index
in the same way. - Create
#my_select
in the same way, though you may use#my_each
in your definition (but not#each
). - Create
#my_all?
(continue as above) - Create
#my_any?
- Create
#my_none?
- Create
#my_count
- Create
#my_map
- Create
#my_inject
- Test your
#my_inject
by creating a method called#multiply_els
which multiplies all the elements of the array together by using#my_inject
, e.g.multiply_els([2,4,5]) #=> 40
- Modify your
#my_map
method to take a proc instead. - Modify your
#my_map
method to take either a proc or a block. It won't be necessary to apply both a proc and a block in the same#my_map
call since you could get the same effect by chaining together one#my_map
call with the block and one with the proc. This approach is also clearer, since the user doesn't have to remember whether the proc or block will be run first. So if both a proc and a block are given, only execute the proc.
Quick Tips:
- Remember
yield
and the#call
method.
Submit a link below to this file on the ruby course github repo with your files in it by using a pull request. See the section on Contributing for how.
Show Student Solutions
- Add your solution below this line!
- Olugbade Olalekan's Solution
- Robin's Solution
- Mike Farias' Solution
- Run After's Solution
- Noah T's Solution
- Billy Coffin's Solution
- Michael Marchand's Solution
- proto-dylan's Solution
- Ian's Solution
- Saranoya's Solution
- mistervoga's Solution
- Chris' Solution
- Sherman's Solution
- Alain Suarez's Solution
- Rafe Draper's Solution
- Jay Burbyga's Solution
- Nicolás Nisoria's Solution
- Jose Salvador's Solution
- Sarp's Solution
- Loumarven's Solution
- Han Josmer's Solution
- Jacob Thompson's Solution
- Arilson Souza
- Austin Fisher's Solution
- Vollantre's Solution
- Braxton Lemmon's Solution
- BShowen's Solution
- Kevin Vuong's Solution
- Origier's Solution
- Learnsometing's Solution
- Tommy's Solution
- Akbar's Solution
- Khari's Solution
- RunningCoder123's solution
- Stefano Merazzi's solution
- Chris Wegscheid's solution
- Simon Tharby's solution
- Smetanca52's Solution
- Hummeldon's Solution
- wuaangela's Solution
- Emil Dimitrov's Solution
- gfioravante's Solution
- Mohamed Elattar's Solution
- Ajani Stewart's Solution
- Ahmed Ali's Solution
- Tim Dowd's Solution
- Max Garber's Solution
- Malaika (Mic) Solution
- Ngo Van Huong's Solutions
- Nathan Sherburne's Solutions
- Mike Burke's Solutions
- Jon Yoo's Solutions
- Henry Kirya's Solution
- Ryan's Solution
- Turbopro's Solution
- Sherman Bowling's solution
- Javier Machin's solution
- Samuel Masters' Soultion
- Marijunjnja's solution
- funwithcthulhu's solution
- Bruno Parga's solution
- Btreim's solution
- isildonmez's solution
- jdrobertso's solution
- rublen's solution
- endotnick's solution
- Jonathan Yiv's solution
- Jason McKee's solution
- Andrew's solution
- MGiagante's solution
- Bn8's Solution
- GuyInALabCoat's Solution
- Ben Deltenre's Solution
- justinckim3's solution
- Kasey's solution
- Demo318's solution
- Dallaire's solution
- SadieD's solution
- Grey-Ghost's solution
- Arron's solution
- xavier solution
- Webdev-burd's solution
- theghall's solution
- jfonz412's solution
- João Roberto's solution
- dfan14051's solution
- Oleh Sliusar's solution
- Jerliyah Craig's solution
- Ovsjah Schweinefresser's solution
- Yash Anand's Solution
- Nicolas Amaya's solution
- John Phelps's Solution
- Austin Norman's solution
- Jib's Solution
- Chad Kreutzer's Solution
- Stefan P's Solution
- Raiko's Solution
- Jamie's solution
- Chris Oldakowski's solution
- Marina Sergeyeva's solution
- Afshin M. afshinator's solution
- Donald's solution
- Alan Russell's solution
- Sahil Agarwal's solution
- TomTom's solution
- Tommy Noe's solution
- Michael Alexander's solution
- James MacIvor's solution
- Aleksandar's solution
- Lara Finnegan's solution
- John Quarles' solution
- Jack Nguyen's solution
- Vidul's solution
- Hunter Ducharme's solution
- Artur Janik's solution
- Kate McFaul's solution
- Dominik Stodolny's solution
- Chris Dziewa's solution
- Josh Naughton's solution
- Jason Matthews' solution
- JrodManU's solution
- Kevin Mulhern's solution
- Jeremy Mauzy's solution
- LongPotato's solution
- Dorian Iacobescu's solution
- Sam Padrul's solution
- Eleanor Weigert's solution
- omokoro's solution
- Brann James' solution
- Eduardo Frias' solution
- Andrej Dragojevic's solution
- Antonio Augusto's solution
- ll14m4n's solution
- AtActionParks's solution
- Matias Pan's solution
- Richard Stewart's solution
- Mark Viola's solution
- Dan Hoying's solution
- Joe Balsamo's solution
- Cody Gipson's solution
- Glenn Crosby's solution
- M. Edgar Joel's solution
- Miguel Oliveira's solution
- Xavier Reid's solution
- Gb69010p's solution
- Alex Chen's solution
- John Tobillo's solution
- Jason Symons' solution
- PiotrAleksander's solution
- AnthonyL's solution
- Angus Dobson's solution
- Panashe Fundira's solution
- pwdd's solution
- Paweł Cichoń solution
- Noah Prescott's solution
- Florian Mainguy's solution
- Aviv levinsky's solution
- Alex Tsiras' solution
- Maciej Panasiewicz's solution
- Giorgos Mitsis's solution
- Theoderik Trajanson's solution
- Sander Schepens's solution
- srashidi's solution
- Eric's solution
- Dave Meister's solution
- Andrew Park's solution
- Austin Mason's solution
- James Brooks's solution
- Ricardo Villegas' solution
- cdouglass's solution
- Skye Free's solution
- Dominik Chomicki's solution
- Corey Kazaks' solution
- Miguel Herrera's solution
- Matt Leininger's solution
- Peuchen's solution
- Andrew Johnson's solution
- Bartlomiej Lazarski's solution
- Maia Petee's solution
- Francisco Carlos's solution
- Luke Walker's solution
- Noobling's solution
- TheBetterNewt's solution
- Max Gallant's solution
- Jean Merlet's solution
- parhaml's solution
- Zac Conner's solution
- Vivek Kumar's solution
- Johnny Rasnic's solution
- Fabricio Carrara's solution
- Omar Cagua's solution
- Deepak's solution
- Lani Huang's solution
- Anthony Vumbaca's solution
- Shala Qweghen's solution
- John Connor's solution
- Earth35's solution
- Kelvin Stone's solution
- Ricardo Ferreira's solution
- Michael Sotkin's solution
- Joshua Hipple's solution
- Oscar Y.'s solution
- Shawn Stovall's solution
- Luchillo's solution
- Adam Graham's solution
- Rahul's solution
- Jiazhi Guo's solution
- Clint's solution
- Manu Phatak's solution
- Cody Buffaloe's solution
- J-kaizen's solution
- Max Platt's solution
- Roy Chen's solution
- Piotr Ejsmont's solution
- Chris Chambers' solution
- Loris Aranda's solution
- Jack Deegan's solution
- Jakub Peikert's solution
- HenrytheDJ's solution
- Derek Kwong's solution
- Sergey Badulin's solution
- Dom Goj's solution
- grzegorzzajac1989's solution
- Jason Keeney's solution
- Hassan's solution
- Bishal's solution
- Kaaviya's solution
- Jerry Gao's solution
- Mike Coon's solution
- Saroar'S Soulution
- Tom Westerhout's solution
- Sophia Wu's solution
- Smiles's solution
- Joe Himes's solution
- Braydon's solution
- Ryan Barnett's solution
- Kaique "Rique" Borges' solution
- jeff1st's solution
- Niño Mollaneda's solution
- Roan Fourie's solution
- Robert Szabo's solution
- Alan Cruse's solution
- husein ghafari's solution
- Vaclav Skvaril's solution
- Parker Brown's solution
- Øistein Haugland's solution
- Luján Fernaud's solution
- hs7's solution
- EMuchynski's solution
- Ian's solution
- Swojeet's solution
- Paritosh's solution
- Roland Studer's Solution
- Anistor86's Solution
- Andy DeNike's Solution
- Iceron's Solution
- coryparham24's solution
- Oliver Curting's Solution
- Alex's Solution
- HuyAnh's Solution
- Santiago Rodríguez Solution
- Alexander Luna's solution
- NJW's Solution
- Niko Caron's Solution
- HSaad's Solution
- Punnadittr's Solution
- Agon's Solution
- Encolpius's solution
- Areeba's Solution
- mojotron's Solution
- Josh Hansen's Solution
- Felipe Parreira's Solution
- Amy Smith's Solution
- dmarkiewicz's Solution
- codyMalcolm's Solution
- Tommy's Solution
- bchalman's Solution
- Rudi Boshoff's Solution with testing
- Slaven Karamatic's Solution
- mwk913's Solution
- EdwardHeath's Soultion
- Leila Alderman's solution
- Junwen's solution
- vanny96's Solution
- Joanna's Solution
- JamCry's Solution
- Wesley Wang's Solution
- Sergej Jurchenko's Solution
- Alex Krewson's Solution
- Bendee48's Solution
- Ben Fowler's Solution
- Tronerta's Solution
- Nyaga Roy's solution
- LastCapricorn's solution
- Barbara Lucas' solution
- Muminjon's Solution
- Leo Holanda's Solution
- Tristan Ross's Solution
- Brett Bonnet's Solution
- Franklyn Afeso's Solution
- Robert Dunbar's Solution
- Gareth Woodman's Solution
- Adriel Bruno's Solution
- Tolulope Olusakin's Solution - Enumerable Module Methods
- Miguel's Solution
- David Watkin's Solution
- Cameron St. Amant's Solution
- Robert Suazo's Solution
- DalandanJuice's Solution
- hyperturing's Solution
This section contains helpful links to other content. It isn't required, so consider it supplemental for if you need to dive deeper into something.