├── Cryptography ├── README.md └── Caesar_Cipher.py ├── Sorting ├── README.md ├── QuickSort.py └── SortingAlgorithms.md ├── First_programs ├── README.md ├── helloworld.py └── input_name.py ├── Numbers ├── tax.py ├── fibonacci_value.py ├── fibonacci.py ├── next_prime.py ├── calc.py ├── README.md ├── prime.py ├── pi.py └── change.py ├── LICENSE └── README.md /Cryptography/README.md: -------------------------------------------------------------------------------- 1 | Cryptography 2 | --------- 3 | 4 | [Caesar Cipher](https://github.com/MrBlaise/learnpython/blob/master/Cryptography/Caesar_Cipher.py) - A Caesar Cipher written in python3 (Contributor: [khazelrigg](https://github.com/khazelrigg)) 5 | 6 | -------------------------------------------------------------------------------- /Sorting/README.md: -------------------------------------------------------------------------------- 1 | Sorting 2 | --------- 3 | 4 | [**QuickSort**](https://github.com/MrBlaise/learnpython/blob/master/Sorting/QuickSort.py) – This is for creating a quicksort algorithm that uses an array of elements which are taken from a Standard Normal Distribution and prints out the Normal Distribution Graph of the array elements and also the the number of comparisons taking place. (Contributor: [prateekiiest](https://github.com/prateekiiest)) 5 | 6 | [Read more about sorting algorithms](https://github.com/MrBlaise/learnpython/blob/master/Sorting/SortingAlgorithms.md) 7 | -------------------------------------------------------------------------------- /First_programs/README.md: -------------------------------------------------------------------------------- 1 | First Programs 2 | --------- 3 | 4 | In this folder you will find the most basic programs written in Python. If you just started learning the language this is the place to start. 5 | 6 | [**Hello, World Program**](https://github.com/MrBlaise/learnpython/blob/master/First_programs/helloworld.py) - The classic "Hello, World" program that prints out this famous message. 7 | 8 | [**Text/Name Input Program**](https://github.com/MrBlaise/learnpython/blob/master/First_programs/input_name.py) - Asks for the user's name, greets the user using that name. (print function's 'sep' and 'end' arguments explained) -------------------------------------------------------------------------------- /First_programs/helloworld.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | 3 | """ 4 | 5 | The following main function is a wrapper function. 6 | It contains the program itself and it will only be 7 | executed if it is not imported to another *.py file 8 | 9 | """ 10 | 11 | 12 | def main(): 13 | 14 | print('Hello, World!') 15 | 16 | """ 17 | 18 | The following if statetmant triggers the main function 19 | if it wasn't called by another program, therefore its 20 | __name__ = '__main__' if it was imported into another file 21 | the __name__ would change since it is not the main program 22 | anymore. 23 | 24 | """ 25 | 26 | if __name__ == '__main__': 27 | main() 28 | -------------------------------------------------------------------------------- /Numbers/tax.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | 3 | # Tax calculator 4 | # This program currently 5 | # only works with the hungarian tax 6 | 7 | def calcTax(cost, country='hungary'): 8 | 9 | countries = {'hungary' : 27} 10 | 11 | if country in countries.keys(): 12 | tax = (cost / 100) * countries[country] 13 | else: 14 | return "Country can't be found" 15 | 16 | return tax 17 | 18 | def main(): # Wrapper function 19 | 20 | tax = calcTax(int(input('What was the cost of your purchase? ')), 21 | input('Which country are you in? ')) 22 | 23 | if type(tax) == str: 24 | print(tax) 25 | else: 26 | print('The tax on your purchase was:', tax) 27 | 28 | if __name__ == '__main__': 29 | main() -------------------------------------------------------------------------------- /Numbers/fibonacci_value.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | 3 | 4 | # Fibonacci Value 5 | # Have the user enter a number 6 | # and calculate that number's 7 | # fibonacci value. 8 | 9 | def fib(x): 10 | """ 11 | Assumes x an integer >= 0 12 | Returns Fibonacci value of x 13 | 14 | """ 15 | assert isinstance(x, int) and x >= 0 16 | 17 | n_1, n_2, i = 1, 1, 2 18 | 19 | while i <= x: 20 | n_new = n_1 + n_2 21 | n_1, n_2 = n_2, n_new 22 | i += 1 23 | 24 | return n_2 25 | 26 | 27 | def main(): # Wrapper function 28 | 29 | x = int(input('Enter a number to get its fibonacci value: ')) 30 | 31 | print('The fibonacci value of', x, 'is:', fib(x)) 32 | 33 | if __name__ == '__main__': 34 | main() 35 | -------------------------------------------------------------------------------- /Numbers/fibonacci.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | 3 | # Fibonacci Sequence Generator 4 | # Have the user enter a number and 5 | # generate a fibonacci sequence 6 | # which size is equivalent to that number. 7 | 8 | 9 | def fibSequence(n): 10 | """ 11 | Generates a fibonacci sequence 12 | with the size of n 13 | """ 14 | assert n > 0 15 | 16 | series = [1] 17 | 18 | while len(series) < n: 19 | if len(series) == 1: 20 | series.append(1) 21 | else: 22 | series.append(series[-1] + series[-2]) 23 | 24 | for i in range(len(series)): # Convert the numbers to strings 25 | series[i] = str(series[i]) 26 | 27 | return(', '.join(series)) # Return the sequence seperated by commas 28 | 29 | 30 | def main(): # Wrapper function 31 | 32 | print(fibSequence(int(input('How many numbers do you need? ')))) 33 | 34 | if __name__ == '__main__': 35 | main() 36 | -------------------------------------------------------------------------------- /First_programs/input_name.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | 3 | # The program ask for the user's name 4 | # Greets the user using his name 5 | 6 | 7 | def main(): # Wrapper function 8 | 9 | name = input('What is your name? ') # Asks for a string - name 10 | 11 | print('Hello ', name, '! How are you?', sep='', end='\n') 12 | 13 | # PRINT FUNCTION 14 | 15 | # SEP 16 | # The 'sep' argument in Python 3 will seperate the text 17 | # with the given character, since now I want an exclamation mark 18 | # after 'name' I don't like to have any space between that, 19 | # so I set 'sep' to be nothing. 20 | 21 | # END 22 | # The 'end' argument is what the program prints 23 | # after the print function. 24 | # By default is '\n' (newline) 25 | # so you don't need to put that there, however 26 | # there are some cases when you don't want a newline after 27 | # every print function, so this is good to know. 28 | 29 | 30 | if __name__ == '__main__': 31 | main() 32 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 Balázs Rostás 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /Numbers/next_prime.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | 3 | # Next Prime Number 4 | # Generate prime numbers until 5 | # the user chooses to stop 6 | 7 | 8 | def isPrime(x): 9 | """ 10 | Checks whether the given 11 | number x is prime or not 12 | """ 13 | 14 | if x == 2: 15 | return True 16 | 17 | if x % 2 == 0: 18 | return False 19 | 20 | for i in range(3, int(x**0.5)+1, 2): 21 | if x % i == 0: 22 | return False 23 | 24 | return True 25 | 26 | 27 | def genPrime(currentPrime): 28 | """ 29 | Returns the next prime 30 | after the currentPrime 31 | """ 32 | 33 | newPrime = currentPrime + 1 34 | 35 | while True: 36 | 37 | if not isPrime(newPrime): 38 | newPrime += 1 39 | else: 40 | break 41 | 42 | return newPrime 43 | 44 | 45 | def main(): # Wrapper function 46 | 47 | currentPrime = 2 48 | 49 | while True: 50 | 51 | answer = input('Would you like to see the next prime? (Y/N) ') 52 | 53 | if answer.lower().startswith('y'): 54 | print(currentPrime) 55 | currentPrime = genPrime(currentPrime) 56 | 57 | else: 58 | break 59 | 60 | if __name__ == '__main__': 61 | main() 62 | -------------------------------------------------------------------------------- /Numbers/calc.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | 3 | # Calculator 4 | # Have the user enter 2 number - a. b - 5 | # and an operator - op - and calculate 6 | # the solution - c - according to the 7 | # type of the given operator 8 | 9 | 10 | def calc(a, b, op): 11 | """ 12 | Returns a string like this: a op b = c 13 | where c is the computed value according to the opeartor 14 | """ 15 | 16 | if op not in '+-/*': 17 | return 'Please only type one of these characters: "+, -, *, /"!' 18 | 19 | if op == '+': 20 | return(str(a) + ' ' + op + ' ' + str(b) + ' = ' + str(a + b)) 21 | if op == '-': 22 | return(str(a) + ' ' + op + ' ' + str(b) + ' = ' + str(a - b)) 23 | if op == '*': 24 | return(str(a) + ' ' + op + ' ' + str(b) + ' = ' + str(a * b)) 25 | if op == '/': 26 | return(str(a) + ' ' + op + ' ' + str(b) + ' = ' + str(a / b)) 27 | 28 | 29 | def main(): # Wrapper function 30 | 31 | a = int(input('Please type the first number: ')) 32 | b = int(input('Please type the second number: ')) 33 | op = input( 34 | 'What kind of operation would you like to do?\ 35 | \nChoose between "+, -, *, /" : ') 36 | 37 | print(calc(a, b, op)) 38 | 39 | if __name__ == '__main__': 40 | main() 41 | -------------------------------------------------------------------------------- /Numbers/README.md: -------------------------------------------------------------------------------- 1 | Numbers 2 | --------- 3 | 4 | [**Find PI to the Nth Digit**](https://github.com/MrBlaise/learnpython/blob/master/Numbers/pi.py) – Enter a number and have the program generate PI up to that many decimal places. No limit to the number of decimals 5 | 6 | [**Fibonacci Value**](https://github.com/MrBlaise/learnpython/blob/master/Numbers/fibonacci_value.py) - Enter a number and have the program generate its Fibonacci value. [[Fibonacci Sequence Wiki]](https://github.com/prateekiiest/SelfProjects/wiki) 7 | 8 | [**Fibonacci Sequence**](https://github.com/MrBlaise/learnpython/blob/master/Numbers/fibonacci.py) – Enter a number and have the program generate the Fibonacci sequence to that number. 9 | 10 | [**Calculator**](https://github.com/MrBlaise/learnpython/blob/master/Numbers/calc.py) - A simple calculator to do the 4 basic operations (Later make it scientific). 11 | 12 | [**Prime Factorization**](https://github.com/MrBlaise/learnpython/blob/master/Numbers/prime.py) – Have the user enter a number and find its prome factors. Extra: Show the exponent of each prime factor as well. 13 | 14 | [**Next Prime Number**](https://github.com/MrBlaise/learnpython/blob/master/Numbers/next_prime.py) – Have the program generate prime numbers until the user chooses to stop. 15 | 16 | [**Change Return Program**](https://github.com/MrBlaise/learnpython/blob/master/Numbers/change.py) – The user enters a cost of the purchase and then the amount of money given. The program will figure out the change and the number of bills and coins for the change. 17 | -------------------------------------------------------------------------------- /Numbers/prime.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | 3 | # Prime Factorization 4 | # Have the user enter a number 5 | # and return its prime factors 6 | 7 | # Extra: show the exponent of 8 | # the prime factors as well 9 | 10 | from collections import Counter 11 | 12 | 13 | def is_prime(x): 14 | """ 15 | Checks whether the given 16 | number x is prime or not 17 | """ 18 | 19 | if x == 2: 20 | return True 21 | 22 | if x % 2 == 0: 23 | return False 24 | 25 | for i in range(3, int(x ** 0.5) + 1, 2): 26 | if x % i == 0: 27 | return False 28 | 29 | return True 30 | 31 | 32 | def get_exponent(n): 33 | """ 34 | Counts the same elements in n list 35 | returns a list with the exponent of 36 | the multiple elements 37 | """ 38 | 39 | c = Counter(n) 40 | factors = [] 41 | 42 | for i in range(min(n), max(n) + 1): 43 | if i in n: 44 | if c[i] != 1: 45 | factors.append(str(i) + '^' + str(c[i])) 46 | else: 47 | factors.append(str(i)) 48 | 49 | return factors 50 | 51 | 52 | def main(): # Wrapper function 53 | 54 | n = int(input('Enter a number to find its prime factors: ')) 55 | 56 | factors = [] 57 | counter = 2 58 | 59 | while True: 60 | 61 | if n == 0 or n == 1: 62 | break 63 | 64 | for i in range(counter, n + 1): 65 | if n % i == 0: 66 | if is_prime(i): 67 | factors.append(i) 68 | n //= i 69 | break 70 | 71 | if len(factors) != 0: 72 | factors = get_exponent(factors) 73 | print(', '.join(factors)) 74 | else: 75 | print('The number', n, 'does not have any prime factors.') 76 | 77 | if __name__ == '__main__': 78 | main() 79 | -------------------------------------------------------------------------------- /Numbers/pi.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | 3 | # Find PI to the Nth Digit 4 | # Have the user enter a number 'n' 5 | # and print out PI to the 'n'th digit 6 | 7 | 8 | def calcPi(limit): # Generator function 9 | """ 10 | Prints out the digits of PI 11 | until it reaches the given limit 12 | """ 13 | 14 | q, r, t, k, n, l = 1, 0, 1, 1, 3, 3 15 | 16 | decimal = limit 17 | counter = 0 18 | 19 | while counter != decimal + 1: 20 | if 4 * q + r - t < n * t: 21 | # yield digit 22 | yield n 23 | # insert period after first digit 24 | if counter == 0: 25 | yield '.' 26 | # end 27 | if decimal == counter: 28 | print('') 29 | break 30 | counter += 1 31 | nr = 10 * (r - n * t) 32 | n = ((10 * (3 * q + r)) // t) - 10 * n 33 | q *= 10 34 | r = nr 35 | else: 36 | nr = (2 * q + r) * l 37 | nn = (q * (7 * k) + 2 + (r * l)) // (t * l) 38 | q *= k 39 | t *= l 40 | l += 2 41 | k += 1 42 | n = nn 43 | r = nr 44 | 45 | 46 | def main(): # Wrapper function 47 | 48 | # Calls CalcPi with the given limit 49 | pi_digits = calcPi(int(input( 50 | "Enter the number of decimals to calculate to: "))) 51 | 52 | i = 0 53 | 54 | # Prints the output of calcPi generator function 55 | # Inserts a newline after every 40th number 56 | for d in pi_digits: 57 | print(d, end='') 58 | i += 1 59 | if i == 40: 60 | print("") 61 | i = 0 62 | 63 | if __name__ == '__main__': 64 | main() 65 | -------------------------------------------------------------------------------- /Sorting/QuickSort.py: -------------------------------------------------------------------------------- 1 | import random 2 | from tempfile import TemporaryFile 3 | import numpy as np 4 | import math 5 | import matplotlib.pyplot as plt 6 | 7 | 8 | def _inPlaceQuickSort(A,start,end): 9 | count = 0 10 | if start= 1 and shift_key <= MAX_SHIFT): 28 | return shift_key 29 | 30 | def crypt_message(mode, message, shift_key): 31 | """ The encryption / decryption action is here """ 32 | if mode[0] == 'd': 33 | shift_key = -shift_key 34 | translated = '' 35 | 36 | for symbol in message: # The encryption stuff 37 | if symbol.isalpha(): 38 | num = ord(symbol) 39 | num += shift_key 40 | 41 | if symbol.isupper(): 42 | if num > ord('Z'): 43 | num -= 26 44 | elif num < ord('A'): 45 | num += 26 46 | 47 | elif symbol.islower(): 48 | if num > ord('z'): 49 | num -= 26 50 | elif num < ord('a'): 51 | num += 26 52 | 53 | translated += chr(num) 54 | else: 55 | translated += symbol 56 | return translated 57 | 58 | mode = what_mode() 59 | message = plain_message() 60 | 61 | if mode[0] != 'b': 62 | shift_key = get_key() 63 | 64 | print('Your translated text is:') 65 | 66 | if mode[0] != 'b': #Brute force settings 67 | print(crypt_message(mode, message, shift_key)) 68 | else: 69 | for shift_key in range(1, MAX_SHIFT + 1): 70 | print(shift_key, crypt_message('decrypt', message, shift_key)) 71 | -------------------------------------------------------------------------------- /Numbers/change.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | 3 | # Change Calculator 4 | # Calculates the change for US dollar 5 | # Prints out the type of bills and coins 6 | # that needs to be given to the customer 7 | 8 | 9 | def changeCoins(change): 10 | 11 | p = 0 # 0.01 12 | n = 0 # 0.05 13 | d = 0 # 0.10 14 | q = 0 # 0.25 15 | 16 | changeCoins = int((change - int(change)) * 100) 17 | 18 | if changeCoins >= 25: 19 | q = int(changeCoins / 25) 20 | changeCoins = changeCoins - q * 25 21 | 22 | if changeCoins >= 10: 23 | d = int(changeCoins / 10) 24 | changeCoins = changeCoins - d * 10 25 | 26 | if changeCoins >= 5: 27 | n = int(changeCoins / 5) 28 | changeCoins = changeCoins - n * 5 29 | 30 | if changeCoins >= 1: 31 | p = int(changeCoins / 1) 32 | 33 | print('\nPlease give the customer the following coins\n\ 34 | Quarters:', q, 'Dimes:', d, 'Nickels:', n, 'Pennies: ', p) 35 | 36 | 37 | def changeBill(change): 38 | 39 | changeBills = int(change) 40 | 41 | hun = 0 42 | fif = 0 43 | twe = 0 44 | ten = 0 45 | one = 0 46 | 47 | if changeBills >= 100: 48 | hun = int(changeBills / 100) 49 | changeBills = changeBills - hun * 100 50 | 51 | if changeBills >= 50: 52 | fif = int(changeBills / 50) 53 | changeBills = changeBills - fif * 50 54 | 55 | if changeBills >= 20: 56 | twe = int(changeBills / 20) 57 | changeBills = changeBills - twe * 20 58 | 59 | if changeBills >= 10: 60 | ten = int(changeBills / 10) 61 | changeBills = changeBills - ten * 10 62 | 63 | if changeBills >= 1: 64 | one = int(changeBills / 1) 65 | 66 | print('\nPlease give the customer the following bills:\n', 67 | 'Hundreds: ', hun, ' Fifties: ', fif, 68 | ' Twenties: ', twe, ' Tens: ', ten, ' Ones: ', one, '\n', sep='') 69 | 70 | 71 | def changeCalc(cost, money): 72 | 73 | if money < cost: 74 | 75 | change = cost - money 76 | 77 | print('\nPlease pay %.2f$ more\n', change) 78 | 79 | else: 80 | 81 | change = money - cost 82 | 83 | print('\nThe change is: %.2f$\n' % change) 84 | 85 | changeBill(change) 86 | changeCoins(change) 87 | 88 | 89 | def main(): 90 | 91 | cost = round(float(input('Enter the cost of the purchase: ')), 2) 92 | money = round(float(input('Enter the money given: ')), 2) 93 | 94 | changeCalc(cost, money) 95 | 96 | if __name__ == '__main__': 97 | main() 98 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Learn Python 3 2 | ======== 3 | 4 | This repository was created for educational purposes. If you'd like to share a project that would help others learn Python 3 please leave a comment or a pull request. Thank you! :) 5 | 6 | You are welcome to fork this repo and add your projects to it as well. 7 | 8 | In this README file I will link each project that I finish. Projects that share some common properties (eg. They all about numbers) will be in the same folder. 9 | 10 | =============================== 11 | 12 | First Programs 13 | --------- 14 | 15 | In this folder you will find the most basic programs written in Python. If you just started learning the language this is the place to start. 16 | 17 | [**Hello, World Program**](https://github.com/MrBlaise/learnpython/blob/master/First_programs/helloworld.py) - The classic "Hello, World" program that prints out this famous message. 18 | 19 | [**Text/Name Input Program**](https://github.com/MrBlaise/learnpython/blob/master/First_programs/input_name.py) - Asks for the user's name, greets the user using that name. (print function's 'sep' and 'end' arguments explained) 20 | 21 | Numbers 22 | --------- 23 | 24 | [**Find PI to the Nth Digit**](https://github.com/MrBlaise/learnpython/blob/master/Numbers/pi.py) – Enter a number and have the program generate PI up to that many decimal places. No limit to the number of decimals 25 | 26 | [**Fibonacci Value**](https://github.com/MrBlaise/learnpython/blob/master/Numbers/fibonacci_value.py) - Enter a number and have the program generate its Fibonacci value. [[Fibonacci Sequence Wiki]](https://github.com/prateekiiest/SelfProjects/wiki) 27 | 28 | [**Fibonacci Sequence**](https://github.com/MrBlaise/learnpython/blob/master/Numbers/fibonacci.py) – Enter a number and have the program generate the Fibonacci sequence to that number. 29 | 30 | [**Calculator**](https://github.com/MrBlaise/learnpython/blob/master/Numbers/calc.py) - A simple calculator to do the 4 basic operations (Later make it scientific). 31 | 32 | [**Prime Factorization**](https://github.com/MrBlaise/learnpython/blob/master/Numbers/prime.py) – Have the user enter a number and find its prome factors. Extra: Show the exponent of each prime factor as well. 33 | 34 | [**Next Prime Number**](https://github.com/MrBlaise/learnpython/blob/master/Numbers/next_prime.py) – Have the program generate prime numbers until the user chooses to stop. 35 | 36 | [**Change Return Program**](https://github.com/MrBlaise/learnpython/blob/master/Numbers/change.py) – The user enters a cost of the purchase and then the amount of money given. The program will figure out the change and the number of bills and coins for the change. 37 | 38 | Sorting 39 | --------- 40 | 41 | [**QuickSort**](https://github.com/MrBlaise/learnpython/blob/master/Sorting/QuickSort.py) – This is for creating a quicksort algorithm that uses an array of elements which are taken from a Standard Normal Distribution and prints out the Normal Distribution Graph of the array elements and also the the number of comparisons taking place. (Contributor: [prateekiiest](https://github.com/prateekiiest)) 42 | 43 | Cryptography 44 | --------- 45 | 46 | [Caesar Cipher](https://github.com/MrBlaise/learnpython/blob/master/Cryptography/Caesar_Cipher.py) - A Caesar Cipher written in python3 (Contributor: [khazelrigg](https://github.com/khazelrigg)) 47 | 48 | -------------------------------------------------------------------------------- /Sorting/SortingAlgorithms.md: -------------------------------------------------------------------------------- 1 | ## Sort Algorithms 2 | 3 | 4 | ### Bubble 5 | ![alt text][bubble-image] 6 | 7 | From [Wikipedia][bubble-wiki]: Bubble sort, sometimes referred to as sinking sort, is a simple sorting algorithm that repeatedly steps through the list to be sorted, compares each pair of adjacent items and swaps them if they are in the wrong order. The pass through the list is repeated until no swaps are needed, which indicates that the list is sorted. 8 | 9 | __Properties__ 10 | * Worst case performance O(n^2) 11 | * Best case performance O(n) 12 | * Average case performance O(n^2) 13 | 14 | ###### View the algorithm in [action][bubble-toptal] 15 | 16 | 17 | 18 | ### Insertion 19 | ![alt text][insertion-image] 20 | 21 | From [Wikipedia][insertion-wiki]: Insertion sort is a simple sorting algorithm that builds the final sorted array (or list) one item at a time. It is much less efficient on large lists than more advanced algorithms such as quicksort, heapsort, or merge sort. 22 | 23 | __Properties__ 24 | * Worst case performance O(n^2) 25 | * Best case performance O(n) 26 | * Average case performance O(n^2) 27 | 28 | ###### View the algorithm in [action][insertion-toptal] 29 | 30 | 31 | ### Merge 32 | ![alt text][merge-image] 33 | 34 | From [Wikipedia][merge-wiki]: In computer science, merge sort (also commonly spelled mergesort) is an efficient, general-purpose, comparison-based sorting algorithm. Most implementations produce a stable sort, which means that the implementation preserves the input order of equal elements in the sorted output. Mergesort is a divide and conquer algorithm that was invented by John von Neumann in 1945. 35 | 36 | __Properties__ 37 | * Worst case performance O(n log n) 38 | * Best case performance O(n) 39 | * Average case performance O(n) 40 | 41 | 42 | ###### View the algorithm in [action][merge-toptal] 43 | 44 | ### Quick 45 | ![alt text][quick-image] 46 | 47 | From [Wikipedia][quick-wiki]: Quicksort (sometimes called partition-exchange sort) is an efficient sorting algorithm, serving as a systematic method for placing the elements of an array in order. 48 | 49 | __Properties__ 50 | * Worst case performance O(n^2) 51 | * Best case performance O(n log n) or O(n) with three-way partition 52 | * Average case performance O(n^2) 53 | 54 | ###### View the algorithm in [action][quick-toptal] 55 | 56 | ### Selection 57 | ![alt text][selection-image] 58 | 59 | From [Wikipedia][selection-wiki]: The algorithm divides the input list into two parts: the sublist of items already sorted, which is built up from left to right at the front (left) of the list, and the sublist of items remaining to be sorted that occupy the rest of the list. Initially, the sorted sublist is empty and the unsorted sublist is the entire input list. The algorithm proceeds by finding the smallest (or largest, depending on sorting order) element in the unsorted sublist, exchanging (swapping) it with the leftmost unsorted element (putting it in sorted order), and moving the sublist boundaries one element to the right. 60 | 61 | __Properties__ 62 | * Worst case performance O(n^2) 63 | * Best case performance O(n^2) 64 | * Average case performance O(n^2) 65 | 66 | ###### View the algorithm in [action][selection-toptal] 67 | 68 | ### Shell 69 | ![alt text][shell-image] 70 | 71 | From [Wikipedia][shell-wiki]: Shellsort is a generalization of insertion sort that allows the exchange of items that are far apart. The idea is to arrange the list of elements so that, starting anywherem considereing every nth element gives a sorted list. Such a list is said to be h-sorted. Equivanelty, it can be thought of as h intterleaved lists, each individually sorted. 72 | 73 | __Properties__ 74 | * Worst case performance O(nlog2 2n) 75 | * Best case performance O(n log n) 76 | * Average case performance depends on gap sequence 77 | 78 | ###### View the algorithm in [action][shell-toptal] 79 | 80 | ###Time-Compexity Graphs 81 | 82 | Comparing the complexity of sorting algorithms (Bubble Sort, Insertion Sort, Selection Sort) 83 | 84 | [Complexity Graphs](https://github.com/prateekiiest/Python/blob/master/sorts/sortinggraphs.png) 85 | 86 | 87 | ###TimSort 88 | 89 | ![alt text][timsort-image] 90 | 91 | From [Wikipedia][timsort-wiki]: Timsort is a hybrid stable sorting algorithm, derived from merge sort and insertion sort, designed to perform well on many kinds of real-world data. It was implemented by Tim Peters in 2002 for use in the Python programming language. The algorithm finds subsequences of the data that are already ordered, and uses that knowledge to sort the remainder more efficiently. This is done by merging an identified subsequence, called a run, with existing runs until certain criteria are fulfilled. 92 | 93 | On small lists of random elements, Timsort looks just like mergesort. The image above was generated with the type of data that Timsort really shines on - a partially ordered array. This lets us see behaviours like run detection and the bulk reversal of chunks of reverse-sorted data. 94 | 95 | __Properties__ 96 | * Worst case performance O(nlogn) 97 | * Best-case performance O(n^2) 98 | * Average performance O(nlogn) 99 | 100 | ---------------------------------------------------------------------------------- 101 | 102 | 103 | [bubble-toptal]: https://www.toptal.com/developers/sorting-algorithms/bubble-sort 104 | [bubble-wiki]: https://en.wikipedia.org/wiki/Bubble_sort 105 | [bubble-image]: https://upload.wikimedia.org/wikipedia/commons/thumb/8/83/Bubblesort-edited-color.svg/220px-Bubblesort-edited-color.svg.png "Bubble Sort" 106 | 107 | [insertion-toptal]: https://www.toptal.com/developers/sorting-algorithms/insertion-sort 108 | [insertion-wiki]: https://en.wikipedia.org/wiki/Insertion_sort 109 | [insertion-image]: https://upload.wikimedia.org/wikipedia/commons/7/7e/Insertionsort-edited.png "Insertion Sort" 110 | 111 | [quick-toptal]: https://www.toptal.com/developers/sorting-algorithms/quick-sort 112 | [quick-wiki]: https://en.wikipedia.org/wiki/Quicksort 113 | [quick-image]: https://upload.wikimedia.org/wikipedia/commons/6/6a/Sorting_quicksort_anim.gif "Quick Sort" 114 | 115 | [merge-toptal]: https://www.toptal.com/developers/sorting-algorithms/merge-sort 116 | [merge-wiki]: https://en.wikipedia.org/wiki/Merge_sort 117 | [merge-image]: https://upload.wikimedia.org/wikipedia/commons/c/cc/Merge-sort-example-300px.gif "Merge Sort" 118 | 119 | [selection-toptal]: https://www.toptal.com/developers/sorting-algorithms/selection-sort 120 | [selection-wiki]: https://en.wikipedia.org/wiki/Selection_sort 121 | [selection-image]: https://upload.wikimedia.org/wikipedia/commons/thumb/b/b0/Selection_sort_animation.gif/250px-Selection_sort_animation.gif "Selection Sort Sort" 122 | 123 | [shell-toptal]: https://www.toptal.com/developers/sorting-algorithms/shell-sort 124 | [shell-wiki]: https://en.wikipedia.org/wiki/Shellsort 125 | [shell-image]: https://upload.wikimedia.org/wikipedia/commons/d/d8/Sorting_shellsort_anim.gif "Shell Sort" 126 | [timsort-wiki]: https://en.wikipedia.org/wiki/Timsort 127 | [timsort-image]: http://sortvis.org/images/weave-timsort.png 128 | --------------------------------------------------------------------------------