Code Monkey home page Code Monkey logo

devleague--bubble-sort's Introduction

Sorting Algorithms

Bubble Sort

ELI5

Look at each number in a list and compare it to the number on its right.

If it is bigger than the number on its right, swap them.

Repeat this process until there are no more numbers to swap.

Pseudocode

while not sorted
	loop through array
		if number is bigger than number on its right
		swap the two numbers 

Performance

Best case scenario: O(n)

Worst case scenario: O(n^2)

Insertion Sort

ELI5

Look at each number in a list and compare it to the number on its left.

If it is smaller than the number on its left, swap them.

Repeat this process until there are no more numbers to swap.

Pseudocode

loop through array
	while current number smaller than number on its left
		swap the two numbers
		compare with one more number to the left

Performance

Best case scenario: O(n)

Worst case scenario: O(n^2)

Selection Sort

ELI5

Look for the smallest number in array.

When found, push number to end of sorted array.

Repeat until original array is empty.

Pseudocode

prepare new array to put numbers in
find smallest number in original array until empty
	put smallest number at the end of new array

Performance

Best case scenario: O(n^2)

Worst case scenario: O(n^2)

Quick Sort

ELI5

Find a number in array as divider.

Numbers smaller than divider will group to its left.

Numbers greater than divider will group to its right.

Repeat this process for these small groups until it is indivisible.

Pseudocode

Use first number in unsorted array as pivot.
Loop through remaining array
	if number smaller than pivot
		push to left array
	else
		push to right array
	if left array has more than 2 numbers
		quick sort left array
	else
		if left array has 2 numbers
			swap them
		else
			leave left array as it is
		if right array has 2 numbers
			swap them
		else
			leave right array as it is
return left array concat pivot concat right array

Performance

Best case scenario: O(n log n)

Worst case scenario: O(n^2)

Merge Sort

ELI5

Break array into smaller pieces until there is one number in each piece.

Sort and group each small piece one at a time.

Repeat until all small pieces are grouped back to size of original array.

Pseudocode

if array has only 1 number
	return array
if array has only 2 numbers
	if first number is greater than second number
		swap them
	return array
if array has more than 2 numbers
	cut array in half to arr1 and arr2
	merge sort arr1 and arr2
	repeat until new array is equal to length of arr1 and arr2 combined
		if arr1 is empty
			append rest of arr2 to new array
			break out of loop
		if arr2 is empty
			append rest of arr1 to new array
			break out of loop
		find smaller number among first item in arr1 and arr2
		push smaller number into new array

Performance

Best case scenario: O(n log n)

Worst case scenario: O(n log n)

devleague--bubble-sort's People

Watchers

James Cloos avatar Edward Gao 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.