Bubble Sort

Write a function ‘bubble_sort(arr)’ which will sort an array of integers using the “bubble sort” methodology.  (http://en.wikipedia.org/wiki/Bubble_sort)

Solution:

First and foremost, understand the difference between insertion sort and bubble sort.

Insertion sort:  Faster because it compares a value against the entire array and places it in the right location in one go.  (The .sort method does this).

insertion-sort-example

Bubble sort: Slower because it compares only against the next index, thus resulting in more comparison steps before ending up in the right location.

bubble-sort

screen-shot-2016-11-30-at-5-16-06-pm

Test Cases:

screen-shot-2016-11-30-at-5-16-34-pm

 

Advertisements
Bubble Sort

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s