diff --git a/machine_learning/Confusion_Matrix.xlsx b/machine_learning/Confusion_Matrix.xlsx new file mode 100644 index 000000000000..0d10528ed6a4 Binary files /dev/null and b/machine_learning/Confusion_Matrix.xlsx differ diff --git a/sorts/Sorting Algorithms.ipynb b/sorts/Sorting Algorithms.ipynb new file mode 100644 index 000000000000..b9e8b5e5bb86 --- /dev/null +++ b/sorts/Sorting Algorithms.ipynb @@ -0,0 +1,508 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 1, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "from IPython.core.interactiveshell import InteractiveShell\n", + "InteractiveShell.ast_node_interactivity = 'all'" + ] + }, + { + "cell_type": "code", + "execution_count": 38, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "test_x = [9 , 4, 5 ,1 ,3, 8, 6, 7, 4, 0, 5, -4 , -6]" + ] + }, + { + "cell_type": "code", + "execution_count": 40, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "13\n", + "i 12\n", + "j 0 i 12\n", + "j 1 i 12\n", + "j 2 i 12\n", + "j 3 i 12\n", + "j 4 i 12\n", + "j 5 i 12\n", + "j 6 i 12\n", + "j 7 i 12\n", + "j 8 i 12\n", + "j 9 i 12\n", + "j 10 i 12\n", + "j 11 i 12\n", + "12 th pass : [4, 5, 1, 3, 8, 6, 7, 4, 0, 5, -4, -6, 9]\n", + "i 11\n", + "j 0 i 11\n", + "j 1 i 11\n", + "j 2 i 11\n", + "j 3 i 11\n", + "j 4 i 11\n", + "j 5 i 11\n", + "j 6 i 11\n", + "j 7 i 11\n", + "j 8 i 11\n", + "j 9 i 11\n", + "j 10 i 11\n", + "11 th pass : [4, 1, 3, 5, 6, 7, 4, 0, 5, -4, -6, 8, 9]\n", + "i 10\n", + "j 0 i 10\n", + "j 1 i 10\n", + "j 2 i 10\n", + "j 3 i 10\n", + "j 4 i 10\n", + "j 5 i 10\n", + "j 6 i 10\n", + "j 7 i 10\n", + "j 8 i 10\n", + "j 9 i 10\n", + "10 th pass : [1, 3, 4, 5, 6, 4, 0, 5, -4, -6, 7, 8, 9]\n", + "i 9\n", + "j 0 i 9\n", + "j 1 i 9\n", + "j 2 i 9\n", + "j 3 i 9\n", + "j 4 i 9\n", + "j 5 i 9\n", + "j 6 i 9\n", + "j 7 i 9\n", + "j 8 i 9\n", + "9 th pass : [1, 3, 4, 5, 4, 0, 5, -4, -6, 6, 7, 8, 9]\n", + "i 8\n", + "j 0 i 8\n", + "j 1 i 8\n", + "j 2 i 8\n", + "j 3 i 8\n", + "j 4 i 8\n", + "j 5 i 8\n", + "j 6 i 8\n", + "j 7 i 8\n", + "8 th pass : [1, 3, 4, 4, 0, 5, -4, -6, 5, 6, 7, 8, 9]\n", + "i 7\n", + "j 0 i 7\n", + "j 1 i 7\n", + "j 2 i 7\n", + "j 3 i 7\n", + "j 4 i 7\n", + "j 5 i 7\n", + "j 6 i 7\n", + "7 th pass : [1, 3, 4, 0, 4, -4, -6, 5, 5, 6, 7, 8, 9]\n", + "i 6\n", + "j 0 i 6\n", + "j 1 i 6\n", + "j 2 i 6\n", + "j 3 i 6\n", + "j 4 i 6\n", + "j 5 i 6\n", + "6 th pass : [1, 3, 0, 4, -4, -6, 4, 5, 5, 6, 7, 8, 9]\n", + "i 5\n", + "j 0 i 5\n", + "j 1 i 5\n", + "j 2 i 5\n", + "j 3 i 5\n", + "j 4 i 5\n", + "5 th pass : [1, 0, 3, -4, -6, 4, 4, 5, 5, 6, 7, 8, 9]\n", + "i 4\n", + "j 0 i 4\n", + "j 1 i 4\n", + "j 2 i 4\n", + "j 3 i 4\n", + "4 th pass : [0, 1, -4, -6, 3, 4, 4, 5, 5, 6, 7, 8, 9]\n", + "i 3\n", + "j 0 i 3\n", + "j 1 i 3\n", + "j 2 i 3\n", + "3 th pass : [0, -4, -6, 1, 3, 4, 4, 5, 5, 6, 7, 8, 9]\n", + "i 2\n", + "j 0 i 2\n", + "j 1 i 2\n", + "2 th pass : [-4, -6, 0, 1, 3, 4, 4, 5, 5, 6, 7, 8, 9]\n", + "i 1\n", + "j 0 i 1\n", + "1 th pass : [-6, -4, 0, 1, 3, 4, 4, 5, 5, 6, 7, 8, 9]\n", + "i 0\n", + "0 th pass : [-6, -4, 0, 1, 3, 4, 4, 5, 5, 6, 7, 8, 9]\n" + ] + }, + { + "data": { + "text/plain": [ + "[-6, -4, 0, 1, 3, 4, 4, 5, 5, 6, 7, 8, 9]" + ] + }, + "execution_count": 40, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Implementation #1, bubble up each index\n", + "def bubble_sort(x):\n", + " print(len(x))\n", + " for i in range(len(x)-1,-1,-1):\n", + " print(\"i\",i)\n", + " for j in range(i):\n", + " print(\"j\",j,\"i\",i)\n", + " if x[j] > x[j+1]:\n", + "# temp = x[j+1]\n", + "# x[j+1], x[j] = x[j], x[j+1]\n", + " x[j], x[j+1] = x[j+1], x[j]\n", + "# x[j] = temp\n", + " print(i,\"th pass :\", x)\n", + " \n", + " \n", + " return x\n", + " \n", + "\n", + "# test_x = [8,3,-1,0]\n", + "bubble_sort(test_x.copy())\n" + ] + }, + { + "cell_type": "code", + "execution_count": 24, + "metadata": { + "collapsed": true + }, + "outputs": [ + { + "data": { + "text/plain": [ + "13" + ] + }, + "execution_count": 24, + "metadata": {}, + "output_type": "execute_result" + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[-6, -4, 0, 1, 3, 4, 4, 5, 5, 6, 7, 8, 9]\n", + "i:seq[12]:9 , j:seq[0]:-6\n", + "i:seq[12]:9 , j:seq[1]:-4\n", + "i:seq[12]:9 , j:seq[2]:0\n", + "i:seq[12]:9 , j:seq[3]:1\n", + "i:seq[12]:9 , j:seq[4]:3\n", + "i:seq[12]:9 , j:seq[5]:4\n", + "i:seq[12]:9 , j:seq[6]:4\n", + "i:seq[12]:9 , j:seq[7]:5\n", + "i:seq[12]:9 , j:seq[8]:5\n", + "i:seq[12]:9 , j:seq[9]:6\n", + "i:seq[12]:9 , j:seq[10]:7\n", + "i:seq[12]:9 , j:seq[11]:8\n", + "i:seq[11]:8 , j:seq[0]:-6\n", + "i:seq[11]:8 , j:seq[1]:-4\n", + "i:seq[11]:8 , j:seq[2]:0\n", + "i:seq[11]:8 , j:seq[3]:1\n", + "i:seq[11]:8 , j:seq[4]:3\n", + "i:seq[11]:8 , j:seq[5]:4\n", + "i:seq[11]:8 , j:seq[6]:4\n", + "i:seq[11]:8 , j:seq[7]:5\n", + "i:seq[11]:8 , j:seq[8]:5\n", + "i:seq[11]:8 , j:seq[9]:6\n", + "i:seq[11]:8 , j:seq[10]:7\n", + "i:seq[10]:7 , j:seq[0]:-6\n", + "i:seq[10]:7 , j:seq[1]:-4\n", + "i:seq[10]:7 , j:seq[2]:0\n", + "i:seq[10]:7 , j:seq[3]:1\n", + "i:seq[10]:7 , j:seq[4]:3\n", + "i:seq[10]:7 , j:seq[5]:4\n", + "i:seq[10]:7 , j:seq[6]:4\n", + "i:seq[10]:7 , j:seq[7]:5\n", + "i:seq[10]:7 , j:seq[8]:5\n", + "i:seq[10]:7 , j:seq[9]:6\n", + "i:seq[9]:6 , j:seq[0]:-6\n", + "i:seq[9]:6 , j:seq[1]:-4\n", + "i:seq[9]:6 , j:seq[2]:0\n", + "i:seq[9]:6 , j:seq[3]:1\n", + "i:seq[9]:6 , j:seq[4]:3\n", + "i:seq[9]:6 , j:seq[5]:4\n", + "i:seq[9]:6 , j:seq[6]:4\n", + "i:seq[9]:6 , j:seq[7]:5\n", + "i:seq[9]:6 , j:seq[8]:5\n", + "i:seq[8]:5 , j:seq[0]:-6\n", + "i:seq[8]:5 , j:seq[1]:-4\n", + "i:seq[8]:5 , j:seq[2]:0\n", + "i:seq[8]:5 , j:seq[3]:1\n", + "i:seq[8]:5 , j:seq[4]:3\n", + "i:seq[8]:5 , j:seq[5]:4\n", + "i:seq[8]:5 , j:seq[6]:4\n", + "i:seq[8]:5 , j:seq[7]:5\n", + "i:seq[7]:5 , j:seq[0]:-6\n", + "i:seq[7]:5 , j:seq[1]:-4\n", + "i:seq[7]:5 , j:seq[2]:0\n", + "i:seq[7]:5 , j:seq[3]:1\n", + "i:seq[7]:5 , j:seq[4]:3\n", + "i:seq[7]:5 , j:seq[5]:4\n", + "i:seq[7]:5 , j:seq[6]:4\n", + "i:seq[6]:4 , j:seq[0]:-6\n", + "i:seq[6]:4 , j:seq[1]:-4\n", + "i:seq[6]:4 , j:seq[2]:0\n", + "i:seq[6]:4 , j:seq[3]:1\n", + "i:seq[6]:4 , j:seq[4]:3\n", + "i:seq[6]:4 , j:seq[5]:4\n", + "i:seq[5]:4 , j:seq[0]:-6\n", + "i:seq[5]:4 , j:seq[1]:-4\n", + "i:seq[5]:4 , j:seq[2]:0\n", + "i:seq[5]:4 , j:seq[3]:1\n", + "i:seq[5]:4 , j:seq[4]:3\n", + "i:seq[4]:3 , j:seq[0]:-6\n", + "i:seq[4]:3 , j:seq[1]:-4\n", + "i:seq[4]:3 , j:seq[2]:0\n", + "i:seq[4]:3 , j:seq[3]:1\n", + "i:seq[3]:1 , j:seq[0]:-6\n", + "i:seq[3]:1 , j:seq[1]:-4\n", + "i:seq[3]:1 , j:seq[2]:0\n", + "i:seq[2]:0 , j:seq[0]:-6\n", + "i:seq[2]:0 , j:seq[1]:-4\n", + "i:seq[1]:-4 , j:seq[0]:-6\n" + ] + }, + { + "data": { + "text/plain": [ + "[-6, -4, 0, 1, 3, 4, 4, 5, 5, 6, 7, 8, 9]" + ] + }, + "execution_count": 24, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Implementation 2 : compare with last index directly\n", + "def bubble_sort(seq):\n", + " print(seq)\n", + " for i in range( len(seq)-1, -1 , -1):\n", + "# print(i)\n", + " for j in range(i):\n", + "# print(i,j)\n", + " print(\"i:seq[{}]:{} , j:seq[{}]:{}\".format(i,seq[i],j,seq[j]))\n", + " if seq[j] > seq[i]:\n", + " print(\"{} in index {} swapping with {} in idx {}\".format(seq[i], i, seq[j], j))\n", + " temp = seq[i]\n", + " seq[i] = seq[j]\n", + " seq[j] = temp\n", + " print(\"Swapped Array:\",seq)\n", + " else:\n", + "# print(\"seq[{}]:{} larger than seq[{}]:{}, no swap\".format(i,seq[i],j,seq[j]))\n", + " pass\n", + " return seq\n", + " \n", + " \n", + " \n", + "simple_test = [ 3, -1, 9, 0 ]\n", + "len(test_arr)\n", + "bubble_sort(test_arr.copy())" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Merge Sort" + ] + }, + { + "cell_type": "code", + "execution_count": 41, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Input arr: [9, 4, 5, 1, 3, 8, 6, 7, 4, 0, 5, -4, -6]\n", + "Input arr: [9, 4, 5, 1, 3, 8]\n", + "Input arr: [9, 4, 5]\n", + "Input arr: [9]\n", + "Input arr: [4, 5]\n", + "Input arr: [4]\n", + "Input arr: [5]\n", + "Input arr: [1, 3, 8]\n", + "Input arr: [1]\n", + "Input arr: [3, 8]\n", + "Input arr: [3]\n", + "Input arr: [8]\n", + "Input arr: [6, 7, 4, 0, 5, -4, -6]\n", + "Input arr: [6, 7, 4]\n", + "Input arr: [6]\n", + "Input arr: [7, 4]\n", + "Input arr: [7]\n", + "Input arr: [4]\n", + "Input arr: [0, 5, -4, -6]\n", + "Input arr: [0, 5]\n", + "Input arr: [0]\n", + "Input arr: [5]\n", + "Input arr: [-4, -6]\n", + "Input arr: [-4]\n", + "Input arr: [-6]\n" + ] + }, + { + "data": { + "text/plain": [ + "[-6, -4, 0, 1, 3, 4, 4, 5, 5, 6, 7, 8, 9]" + ] + }, + "execution_count": 41, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "\n", + "def merge_sort(arr):\n", + "# arr = x.copy()\n", + " print(\"Input arr:\",arr)\n", + " \n", + " if len(arr) > 1:\n", + " midpoint = len(arr) // 2\n", + " left_half = merge_sort(arr[:midpoint])\n", + " right_half = merge_sort(arr[midpoint:])\n", + " \n", + " #Merge\n", + " i = 0\n", + " j = 0\n", + " k = 0\n", + " \n", + " while i < len(left_half) and j < len(right_half):\n", + " if left_half[i] < right_half[j]:\n", + " arr[k] = left_half[i]\n", + " i+=1\n", + " else:\n", + " arr[k] = right_half[j]\n", + " j+=1\n", + " k+=1\n", + " #leftover \n", + " while i < len(left_half):\n", + " arr[k] = left_half[i]\n", + " i +=1\n", + " k+=1\n", + " while j < len(right_half):\n", + " arr[k] = right_half[j]\n", + " j+=1\n", + " k+=1\n", + " \n", + " \n", + " \n", + " return arr\n", + " \n", + " \n", + " \n", + "simple_x = [9,1,8]\n", + "merge_sort(test_x.copy())\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": 56, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[9, 4, 5, 1, 3, 8, 6, 7, 4, 0, 5, -4, -6]\n" + ] + }, + { + "data": { + "text/plain": [ + "[-6, -4, 0, 1, 3, 4, 4, 5, 5, 6, 7, 8, 9]" + ] + }, + "execution_count": 56, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "## Quicksort\n", + "\n", + "def quick_sort(arr):\n", + " print(arr)\n", + " \n", + " def quickSort(arr, lowIndex, highIndex):\n", + " if highIndex - lowIndex >= 1:\n", + " p = partition(arr, lowIndex, highIndex) # obtained partition index (divider index) is already in its final place\n", + " quickSort(arr,lowIndex,p-1) # Recursive quickSort into portion of the array before and after the partition\n", + " quickSort(arr,p+1,highIndex)\n", + " \n", + " def partition(arr, lowIndex, highIndex):\n", + " divider = lowIndex # initialize divider index\n", + " \n", + " #Custom algorithim to choose pivot, but move it to the highest Index after selecting the pivot\n", + " # Modify pivot position to low or high or random\n", + "# pivot_pos = (highIndex + lowIndex ) // 2 #eg. midpoint for pivot\n", + " pivot_pos = lowIndex #eg. use lowest position as pivot\n", + "# pivot_pos = highIndex # eg. use highest position as pivot\n", + " \n", + " #Swap pivot position to the highest index\n", + " arr[pivot_pos], arr[highIndex] = arr[highIndex], arr[pivot_pos]\n", + " pivot = highIndex # use the highest index as the pivot\n", + " for i in range(lowIndex, highIndex): #loop through all elements, excluding pivot at the highest index\n", + " if arr[i] < arr[pivot]:\n", + " #if element is smaller than the pivot, move it to the begining of the list(behind the divider index)\n", + " arr[divider] , arr[i] = arr[i] ,arr[divider]\n", + " divider+=1 #Increment divider position\n", + " #After completing loop, place the pivot at the divider position. this is its final place.\n", + " arr[divider], arr[pivot] = arr[pivot], arr[divider]\n", + " \n", + " #now return the position of the pivot so we can ignore sorting this position on subsequent recursive calls\n", + " return divider\n", + " \n", + "\n", + " quickSort(arr, 0, len(arr)-1)\n", + " return arr\n", + "\n", + "quick_sort(test_x.copy())\n" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python [conda env:dlnd-tf-lab]", + "language": "python", + "name": "conda-env-dlnd-tf-lab-py" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.5.2" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/sorts/Sorting_practice.ipynb b/sorts/Sorting_practice.ipynb new file mode 100644 index 000000000000..97270d55f202 --- /dev/null +++ b/sorts/Sorting_practice.ipynb @@ -0,0 +1,480 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 2, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "from IPython.core.interactiveshell import InteractiveShell\n", + "InteractiveShell.ast_node_interactivity = 'all'" + ] + }, + { + "cell_type": "code", + "execution_count": 77, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "test_x = [15, 6, 1, 7, -3 , 9, 0 , 2, 4, -5 ]\n", + "simple_x = [9, 4 ,2 , 0,- 5]" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "metadata": { + "collapsed": false, + "scrolled": true + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "length of x 10 [15, 6, 1, 7, -3, 9, 0, 2, 4, -5]\n", + "Swapped 15 and 6\n", + "Swapped 15 and 1\n", + "Swapped 15 and 7\n", + "Swapped 15 and -3\n", + "Swapped 15 and 9\n", + "Swapped 15 and 0\n", + "Swapped 15 and 2\n", + "Swapped 15 and 4\n", + "Swapped 15 and -5\n", + "i 9 th pass: [6, 1, 7, -3, 9, 0, 2, 4, -5, 15]\n", + "Swapped 6 and 1\n", + "Swapped 7 and -3\n", + "Swapped 9 and 0\n", + "Swapped 9 and 2\n", + "Swapped 9 and 4\n", + "Swapped 9 and -5\n", + "i 8 th pass: [1, 6, -3, 7, 0, 2, 4, -5, 9, 15]\n", + "Swapped 6 and -3\n", + "Swapped 7 and 0\n", + "Swapped 7 and 2\n", + "Swapped 7 and 4\n", + "Swapped 7 and -5\n", + "i 7 th pass: [1, -3, 6, 0, 2, 4, -5, 7, 9, 15]\n", + "Swapped 1 and -3\n", + "Swapped 6 and 0\n", + "Swapped 6 and 2\n", + "Swapped 6 and 4\n", + "Swapped 6 and -5\n", + "i 6 th pass: [-3, 1, 0, 2, 4, -5, 6, 7, 9, 15]\n", + "Swapped 1 and 0\n", + "Swapped 4 and -5\n", + "i 5 th pass: [-3, 0, 1, 2, -5, 4, 6, 7, 9, 15]\n", + "Swapped 2 and -5\n", + "i 4 th pass: [-3, 0, 1, -5, 2, 4, 6, 7, 9, 15]\n", + "Swapped 1 and -5\n", + "i 3 th pass: [-3, 0, -5, 1, 2, 4, 6, 7, 9, 15]\n", + "Swapped 0 and -5\n", + "i 2 th pass: [-3, -5, 0, 1, 2, 4, 6, 7, 9, 15]\n", + "Swapped -3 and -5\n", + "i 1 th pass: [-5, -3, 0, 1, 2, 4, 6, 7, 9, 15]\n", + "i 0 th pass: [-5, -3, 0, 1, 2, 4, 6, 7, 9, 15]\n" + ] + }, + { + "data": { + "text/plain": [ + "[-5, -3, 0, 1, 2, 4, 6, 7, 9, 15]" + ] + }, + "execution_count": 14, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "def bubble_sort(x):\n", + " print(\"length of x\",len(x), x)\n", + " for i in range(len(x)-1, -1 , -1):\n", + " for j in range(i):\n", + " if x[j] > x[j+1]:\n", + " x[j], x[j+1] = x[j+1], x[j]\n", + " print(\"Swapped\", x[j+1], \" and \", x[j])\n", + " print(\"i\",i,\"th pass:\",x)\n", + " return x\n", + " \n", + "bubble_sort(test_x.copy())" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "metadata": { + "collapsed": true + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[15, 6, 1, 7, -3, 9, 0, 2, 4, -5]\n", + "[15, 6, 1, 7, -3]\n", + "[15, 6]\n", + "[15]\n", + "[6]\n", + "Merged : [6, 15]\n", + "[1, 7, -3]\n", + "[1]\n", + "[7, -3]\n", + "[7]\n", + "[-3]\n", + "Merged : [-3, 7]\n", + "Merged : [-3, 1, 7]\n", + "Merged : [-3, 1, 6, 7, 15]\n", + "[9, 0, 2, 4, -5]\n", + "[9, 0]\n", + "[9]\n", + "[0]\n", + "Merged : [0, 9]\n", + "[2, 4, -5]\n", + "[2]\n", + "[4, -5]\n", + "[4]\n", + "[-5]\n", + "Merged : [-5, 4]\n", + "Merged : [-5, 2, 4]\n", + "Merged : [-5, 0, 2, 4, 9]\n", + "Merged : [-5, -3, 0, 1, 2, 4, 6, 7, 9, 15]\n" + ] + }, + { + "data": { + "text/plain": [ + "[-5, -3, 0, 1, 2, 4, 6, 7, 9, 15]" + ] + }, + "execution_count": 17, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "def merge_sort(x):\n", + " print(x)\n", + " if len(x) > 1 :\n", + " midpoint = len(x)//2\n", + " left_x = merge_sort(x[:midpoint])\n", + " right_x = merge_sort(x[midpoint:])\n", + " \n", + " #Merge\n", + " i = 0 #left_x current index\n", + " j = 0 # right_x current index\n", + " k = 0 # index of merging parent array, x\n", + " \n", + " while i < len(left_x) and j < len(right_x):\n", + " if left_x[i] < right_x[j]:\n", + " x[k] = left_x[i]\n", + " i+=1\n", + " else:\n", + " x[k] = right_x[j]\n", + " j+=1\n", + " k+=1\n", + " \n", + " #Handle leftovers\n", + " while i < len(left_x):\n", + " x[k] = left_x[i]\n", + " i+=1\n", + " k+=1\n", + " while j < len(right_x):\n", + " x[k] = right_x[j]\n", + " j+=1\n", + " k+=1\n", + " print(\"Merged :\",x)\n", + " \n", + " return x\n", + "\n", + "merge_sort(test_x.copy())" + ] + }, + { + "cell_type": "code", + "execution_count": 49, + "metadata": { + "collapsed": false, + "scrolled": true + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Input: [15, 6, 1, 7, -3, 9, 0, 2, 4, -5]\n", + "Pivot: 15\n", + "lesser: [6, 1, 7, -3, 9, 0, 2, 4, -5]\n", + "greater: []\n", + "Merging [6, 1, 7, -3, 9, 0, 2, 4, -5] + [ 15 ] + []\n", + "Input: [6, 1, 7, -3, 9, 0, 2, 4, -5]\n", + "Pivot: 6\n", + "lesser: [1, -3, 0, 2, 4, -5]\n", + "greater: [7, 9]\n", + "Merging [1, -3, 0, 2, 4, -5] + [ 6 ] + [7, 9]\n", + "Input: [1, -3, 0, 2, 4, -5]\n", + "Pivot: 1\n", + "lesser: [-3, 0, -5]\n", + "greater: [2, 4]\n", + "Merging [-3, 0, -5] + [ 1 ] + [2, 4]\n", + "Input: [-3, 0, -5]\n", + "Pivot: -3\n", + "lesser: [-5]\n", + "greater: [0]\n", + "Merging [-5] + [ -3 ] + [0]\n", + "Input: [-5]\n", + "Input: [0]\n", + "Input: [2, 4]\n", + "Pivot: 2\n", + "lesser: []\n", + "greater: [4]\n", + "Merging [] + [ 2 ] + [4]\n", + "Input: []\n", + "Input: [4]\n", + "Input: [7, 9]\n", + "Pivot: 7\n", + "lesser: []\n", + "greater: [9]\n", + "Merging [] + [ 7 ] + [9]\n", + "Input: []\n", + "Input: [9]\n", + "Input: []\n" + ] + }, + { + "data": { + "text/plain": [ + "[-5, -3, 0, 1, 2, 4, 6, 7, 9, 15]" + ] + }, + "execution_count": 49, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "def quicksort(x):\n", + " print(\"Input:\",x)\n", + " if len(x) <= 1:\n", + " return x\n", + " else:\n", + " \n", + " pivot = x[0]\n", + " print(\"Pivot:\", pivot)\n", + " lesser = [i for i in x[1:] if i <= pivot]\n", + " print(\"lesser:\",lesser)\n", + " greater = [i for i in x[1:] if i > pivot ]\n", + " print(\"greater:\",greater)\n", + " \n", + " \n", + " print(\"Merging \", lesser, \" + [\", pivot , \"] + \", greater)\n", + " return quicksort(lesser) + [pivot] + quicksort(greater)\n", + "\n", + "quicksort(test_x.copy())\n", + " " + ] + }, + { + "cell_type": "code", + "execution_count": 97, + "metadata": { + "collapsed": false, + "scrolled": true + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[15, 6, 1, 7, -3, 9, 0, 2, 4, -5]\n", + "Current partition snapshot: [-5, 6, 1, 7, -3, 9, 0, 2, 4] + [pivot: 15 ]\n", + "in partition lowIndex:0, highIndex:9\n", + "divider first idx:0 pivot idx:9, value:15\n", + "x[0] (-5) is lesser than x[pivot] (15), swapping with divider\n", + "Swapped x: [-5, 6, 1, 7, -3, 9, 0, 2, 4]\n", + "x[1] (6) is lesser than x[pivot] (15), swapping with divider\n", + "Swapped x: [-5, 6, 1, 7, -3, 9, 0, 2, 4]\n", + "x[2] (1) is lesser than x[pivot] (15), swapping with divider\n", + "Swapped x: [-5, 6, 1, 7, -3, 9, 0, 2, 4]\n", + "x[3] (7) is lesser than x[pivot] (15), swapping with divider\n", + "Swapped x: [-5, 6, 1, 7, -3, 9, 0, 2, 4]\n", + "x[4] (-3) is lesser than x[pivot] (15), swapping with divider\n", + "Swapped x: [-5, 6, 1, 7, -3, 9, 0, 2, 4]\n", + "x[5] (9) is lesser than x[pivot] (15), swapping with divider\n", + "Swapped x: [-5, 6, 1, 7, -3, 9, 0, 2, 4]\n", + "x[6] (0) is lesser than x[pivot] (15), swapping with divider\n", + "Swapped x: [-5, 6, 1, 7, -3, 9, 0, 2, 4]\n", + "x[7] (2) is lesser than x[pivot] (15), swapping with divider\n", + "Swapped x: [-5, 6, 1, 7, -3, 9, 0, 2, 4]\n", + "x[8] (4) is lesser than x[pivot] (15), swapping with divider\n", + "Swapped x: [-5, 6, 1, 7, -3, 9, 0, 2, 4]\n", + "Swapping pivot x[9] (15) with divider x[9] (15)\n", + "Swapped pivot with divider: [-5, 6, 1, 7, -3, 9, 0, 2, 4]\n", + "Full x After partitioning [-5, 6, 1, 7, -3, 9, 0, 2, 4, 15]\n", + "Current partition snapshot: [4, 6, 1, 7, -3, 9, 0, 2] + [pivot: -5 ]\n", + "in partition lowIndex:0, highIndex:8\n", + "divider first idx:0 pivot idx:8, value:-5\n", + "Swapping pivot x[8] (-5) with divider x[0] (4)\n", + "Swapped pivot with divider: [-5, 6, 1, 7, -3, 9, 0, 2]\n", + "Full x After partitioning [-5, 6, 1, 7, -3, 9, 0, 2, 4, 15]\n", + "Current partition snapshot: [4, 1, 7, -3, 9, 0, 2] + [pivot: 6 ]\n", + "in partition lowIndex:1, highIndex:8\n", + "divider first idx:1 pivot idx:8, value:6\n", + "x[1] (4) is lesser than x[pivot] (6), swapping with divider\n", + "Swapped x: [4, 1, 7, -3, 9, 0, 2]\n", + "x[2] (1) is lesser than x[pivot] (6), swapping with divider\n", + "Swapped x: [4, 1, 7, -3, 9, 0, 2]\n", + "x[4] (-3) is lesser than x[pivot] (6), swapping with divider\n", + "Swapped x: [4, 1, -3, 7, 9, 0, 2]\n", + "x[6] (0) is lesser than x[pivot] (6), swapping with divider\n", + "Swapped x: [4, 1, -3, 0, 9, 7, 2]\n", + "x[7] (2) is lesser than x[pivot] (6), swapping with divider\n", + "Swapped x: [4, 1, -3, 0, 2, 7, 9]\n", + "Swapping pivot x[8] (6) with divider x[6] (7)\n", + "Swapped pivot with divider: [4, 1, -3, 0, 2, 6, 9]\n", + "Full x After partitioning [-5, 4, 1, -3, 0, 2, 6, 9, 7, 15]\n", + "Current partition snapshot: [2, 1, -3, 0] + [pivot: 4 ]\n", + "in partition lowIndex:1, highIndex:5\n", + "divider first idx:1 pivot idx:5, value:4\n", + "x[1] (2) is lesser than x[pivot] (4), swapping with divider\n", + "Swapped x: [2, 1, -3, 0]\n", + "x[2] (1) is lesser than x[pivot] (4), swapping with divider\n", + "Swapped x: [2, 1, -3, 0]\n", + "x[3] (-3) is lesser than x[pivot] (4), swapping with divider\n", + "Swapped x: [2, 1, -3, 0]\n", + "x[4] (0) is lesser than x[pivot] (4), swapping with divider\n", + "Swapped x: [2, 1, -3, 0]\n", + "Swapping pivot x[5] (4) with divider x[5] (4)\n", + "Swapped pivot with divider: [2, 1, -3, 0]\n", + "Full x After partitioning [-5, 2, 1, -3, 0, 4, 6, 9, 7, 15]\n", + "Current partition snapshot: [0, 1, -3] + [pivot: 2 ]\n", + "in partition lowIndex:1, highIndex:4\n", + "divider first idx:1 pivot idx:4, value:2\n", + "x[1] (0) is lesser than x[pivot] (2), swapping with divider\n", + "Swapped x: [0, 1, -3]\n", + "x[2] (1) is lesser than x[pivot] (2), swapping with divider\n", + "Swapped x: [0, 1, -3]\n", + "x[3] (-3) is lesser than x[pivot] (2), swapping with divider\n", + "Swapped x: [0, 1, -3]\n", + "Swapping pivot x[4] (2) with divider x[4] (2)\n", + "Swapped pivot with divider: [0, 1, -3]\n", + "Full x After partitioning [-5, 0, 1, -3, 2, 4, 6, 9, 7, 15]\n", + "Current partition snapshot: [-3, 1] + [pivot: 0 ]\n", + "in partition lowIndex:1, highIndex:3\n", + "divider first idx:1 pivot idx:3, value:0\n", + "x[1] (-3) is lesser than x[pivot] (0), swapping with divider\n", + "Swapped x: [-3, 1]\n", + "Swapping pivot x[3] (0) with divider x[2] (1)\n", + "Swapped pivot with divider: [-3, 0]\n", + "Full x After partitioning [-5, -3, 0, 1, 2, 4, 6, 9, 7, 15]\n", + "Current partition snapshot: [7] + [pivot: 9 ]\n", + "in partition lowIndex:7, highIndex:8\n", + "divider first idx:7 pivot idx:8, value:9\n", + "x[7] (7) is lesser than x[pivot] (9), swapping with divider\n", + "Swapped x: [7]\n", + "Swapping pivot x[8] (9) with divider x[8] (9)\n", + "Swapped pivot with divider: [7]\n", + "Full x After partitioning [-5, -3, 0, 1, 2, 4, 6, 7, 9, 15]\n" + ] + }, + { + "data": { + "text/plain": [ + "[-5, -3, 0, 1, 2, 4, 6, 7, 9, 15]" + ] + }, + "execution_count": 97, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "def quicksort(x):\n", + " print(x)\n", + " def quick_sort(x, lowIndex, highIndex):\n", + " if( highIndex - lowIndex >= 1):\n", + " p = partition(x, lowIndex, highIndex)\n", + " print(\"Full x After partitioning\", x)\n", + " quick_sort(x,lowIndex, p-1)\n", + " quick_sort(x,p+1,highIndex)\n", + " \n", + " def partition(x, lowIndex, highIndex):\n", + " \n", + " divider = lowIndex\n", + " \n", + " # To choose pivot at the position of lowIndex, just swap selected pivot to the last index and isolate it \n", + " # from the modifications\n", + " x[lowIndex], x[highIndex] = x[highIndex], x[lowIndex]\n", + " pivot = highIndex\n", + " print(\"Current partition snapshot:\", x[lowIndex:highIndex], \" + [pivot:\",x[pivot],\"]\")\n", + " print(\"in partition lowIndex:{}, highIndex:{}\".format(lowIndex,highIndex))\n", + " print(\"divider first idx:{} pivot idx:{}, value:{}\".format(divider, pivot, x[pivot]))\n", + " for i in range(lowIndex,highIndex): #Not inclusive of highIndex, pivot ignored in swapping\n", + " if x[i] < x[pivot]:\n", + " print(\"x[{}] ({}) is lesser than x[pivot] ({}), swapping with divider\".format(i,x[i],x[pivot]))\n", + " x[i] , x[divider] = x[divider], x[i]\n", + " divider += 1\n", + " print(\"Swapped x:\",x[lowIndex:highIndex])\n", + " print(\"Swapping pivot x[{}] ({}) with divider x[{}] ({})\".format(pivot,x[pivot], divider, x[divider]))\n", + " x[pivot], x[divider] = x[divider], x[pivot]\n", + " print(\"Swapped pivot with divider:\", x[lowIndex:highIndex])\n", + " return divider\n", + " \n", + " quick_sort(x, 0, len(x) -1)\n", + " return x\n", + " \n", + " \n", + "\n", + "quicksort(test_x.copy())" + ] + }, + { + "cell_type": "code", + "execution_count": 89, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "[1, 2, 3]" + ] + }, + "execution_count": 89, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "[ i for i in range(1,4)]" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python [conda env:dlnd-tf-lab]", + "language": "python", + "name": "conda-env-dlnd-tf-lab-py" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.5.2" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +}