Git Product home page Git Product logo

bubble-sort's Introduction

Days 1-2: Bubble Sort

Learning Goals

  • Define the bubble sort algorithm and its advantages/disadvantages
  • Implement the bubble sort algorithm

Introduction

Bubble sort is considered one of the simpler sorting algorithms, and it is highly efficient at handling already sorted lists. While it performs well for sorted lists and short lists, it does not perform well for longer lists. As a result, the sorting functions built into programming languages do not use bubble sort.

Input: [3, 2, 1, 4]
Output: [1, 2, 3, 4]

Input: []
Output: []

How Does Bubble Sort Work?

Bubble sort sorts a list in place. In other words, it does not create a new Array; instead, it modifies the Array that was passed to the function as an argument.

To achieve this in-place sorting, bubble sort swaps elements when they are in the incorrect order. When no swaps occur, the Array is considered sorted. This algorithm iterates over an Array over and over until it is sorted. If the input Array is already sorted, it iterates over it only once because no swaps occur.

Let's look at a step-by-step example with an unsorted list:

Input: [2, 3, 1]
// 2 is less than 3, so it stays where it is
// But 1 is less than 3 so those two values are swapped
Pass 1: [2, 1, 3]

// 1 is less than 2, so those values are swapped
// 2 is less than 3, so those values stay as is
Pass 2: [1, 2, 3]

// On this final pass, no swaps occur, so Array is sorted
Pass 3: [1, 2, 3]

And here's what happens with a sorted list:

Input: [1, 2, 3]

// No swaps occur when iterating over Array
// Input Array is returned as is
Pass 1: [1, 2, 3]

Implement Bubble Sort

Given what you know about bubble sort, implement your own version. Remember, we need to sort the Array in place, so there's no need to declare a new Array and push to it: be sure to return the input Array with its values in order.

Lastly, you may wish to look up how to swap values in whichever language you choose to solve the problem in. Some languages, like Ruby and JavaScript, provide shortcuts!

When you write up the explanation of your solution, don't forget to calculate Big O for its time complexity.

Also take some time to think about what you learned by solving this challenge. What problem solving techniques did you use, coding or otherwise? How can they help you solve other problems?

Use the language of your choosing. We've included starter files for some languages where you can pseudocode, explain your solution and code.

Before you start coding

  1. Rewrite the problem in your own words
  2. Validate that you understand the problem
  3. Write your own test cases
  4. Pseudocode
  5. Code!

And remember, don't run our tests until you've passed your own!

How to run your own tests

Ruby

  1. cd into the ruby folder
  2. ruby <filename>.rb

JavaScript

  1. cd into the javascript folder
  2. node <filename>.js

How to run our tests

Ruby

  1. cd into the ruby folder
  2. bundle install
  3. rspec

JavaScript

  1. cd into the javascript folder
  2. npm i
  3. npm test

bubble-sort's People

Contributors

hellorupa avatar ihollander avatar jlboba avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.