summaryrefslogtreecommitdiff
path: root/Fundamental_of_Computing_and_Programming_in_C/Chapter06.ipynb
diff options
context:
space:
mode:
Diffstat (limited to 'Fundamental_of_Computing_and_Programming_in_C/Chapter06.ipynb')
-rwxr-xr-xFundamental_of_Computing_and_Programming_in_C/Chapter06.ipynb1424
1 files changed, 1424 insertions, 0 deletions
diff --git a/Fundamental_of_Computing_and_Programming_in_C/Chapter06.ipynb b/Fundamental_of_Computing_and_Programming_in_C/Chapter06.ipynb
new file mode 100755
index 00000000..09f324b0
--- /dev/null
+++ b/Fundamental_of_Computing_and_Programming_in_C/Chapter06.ipynb
@@ -0,0 +1,1424 @@
+{
+ "metadata": {
+ "name": "",
+ "signature": "sha256:d7231c68ebf3352357e97f096d7bb05469f2e0aae52fa150a1a57a7e75ef01fb"
+ },
+ "nbformat": 3,
+ "nbformat_minor": 0,
+ "worksheets": [
+ {
+ "cells": [
+ {
+ "cell_type": "heading",
+ "level": 1,
+ "metadata": {},
+ "source": [
+ "Chapter 06 : Handling Of Character Strings"
+ ]
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example: 1, Page Number: 5.19"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#Program for reading a line of text\n",
+ "\n",
+ "n = 0\n",
+ "print \"Enter text press RETRUN to end\"\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter text press RETRUN to end\n"
+ ]
+ }
+ ],
+ "prompt_number": 48
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example: 2, Page Number: 5.20"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#String function\n",
+ "\n",
+ "s1= \"Hello\"\n",
+ "s2='H''e''l''l''o'\n",
+ "\n",
+ "print s1\n",
+ "print s2"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Hello\n",
+ "Hello\n"
+ ]
+ }
+ ],
+ "prompt_number": 4
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example: 3, Page Number: 5.22"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#Program using strlen() function\n",
+ "\n",
+ "name = \"MUNI\"\n",
+ "len1 = len(name)\n",
+ "len2 = len(\"LAK\")\n",
+ "print \"String length of %s is %d\" %(name,len1)\n",
+ "print \"String length of %s is %d\" %(\"LAK\",len2)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "String length of MUNI is 4\n",
+ "String length of LAK is 3\n"
+ ]
+ }
+ ],
+ "prompt_number": 50
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example: 4, Page Number: 5.23"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#Program using strcpy() function\n",
+ "def strcpy(cstring1):\n",
+ " import copy\n",
+ " cstring2=copy.copy(cstring1)\n",
+ " return cstring2 \n",
+ "\n",
+ "source = \"MUNI\"\n",
+ "target = strcpy(source)\n",
+ "print \"Source string is %s\" %source\n",
+ "print \"Target string is %s\" %target"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Source string is MUNI\n",
+ "Target string is MUNI\n"
+ ]
+ }
+ ],
+ "prompt_number": 64
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example: 5, Page Number:5.24"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#Strcat function\n",
+ "\n",
+ "str1=\"MUNI\"\n",
+ "str2=\"LAK\"\n",
+ "print str1+str2\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "MUNILAK\n"
+ ]
+ }
+ ],
+ "prompt_number": 11
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example: 6, Page Number: 5.24"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#Program using strcat() function\n",
+ "def strcat(cstring1, cstring2):\n",
+ " cstring3 = cstring1 + cstring2\n",
+ " return cstring3\n",
+ "\n",
+ "source = \"Ramesh\"\n",
+ "target = \"Babu\"\n",
+ "source = strcat(source,target)\n",
+ "print \"Source string is %s\" %source\n",
+ "print \"Target string is %s\" %target"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Source string is RameshBabu\n",
+ "Target string is Babu\n"
+ ]
+ }
+ ],
+ "prompt_number": 69
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example: 7, Page Number: 5.25"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#Program using strcmp() function\n",
+ "\n",
+ "name = \"Kalai\"\n",
+ "name1 = \"Malai\"\n",
+ "if name ==\"Kalai\":\n",
+ " print \"0\",\n",
+ "if name1!=name:\n",
+ " print \"1\",\n",
+ "if name != \"Kalai mani\":\n",
+ " print \"6\"\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "0 1 6\n"
+ ]
+ }
+ ],
+ "prompt_number": 15
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example: 8, Page Number: 5.26"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#Program using strrev() function\n",
+ "def strrev(cstring1):\n",
+ " cstring2 = ' '\n",
+ " for i in range(len(cstring1)-1,-1,-1):\n",
+ " cstring2 = cstring2 + cstring1[i]\n",
+ " return cstring2\n",
+ "\n",
+ "y = raw_input(\"Enter the string \")\n",
+ "print \"The string reversed is : %s\" %strrev(y)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter the string book\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "The string reversed is : koob\n"
+ ]
+ }
+ ],
+ "prompt_number": 85
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example: 9, Page Number: 5.27"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#Program using enumerated data type\n",
+ "def enum(*sequential, **named):\n",
+ " enums = dict(zip(sequential, range(len(sequential))), **named)\n",
+ " return type('Enum', (), enums)\n",
+ "\n",
+ "week = enum(' ', 'sun','Mon','Tue','Wed','Thr','Fri','Sat')\n",
+ "print \"Mon = %d\" %week.Mon\n",
+ "print \"Tue = %d\" %week.Tue\n",
+ "print \"Wed= %d\" %week.Wed\n",
+ "print \"Sat = %d\" %week.Sat"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Mon = 2\n",
+ "Tue = 3\n",
+ "Wed= 4\n",
+ "Sat = 7\n"
+ ]
+ }
+ ],
+ "prompt_number": 125
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example: 10, Page Number: 5.28"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#Program to identify whether the entered character is letter, digit or other symbols\n",
+ "def enum(*sequential, **named):\n",
+ " enums = dict(zip(sequential, range(len(sequential))), **named)\n",
+ " return type('Enum', (), enums)\n",
+ "\n",
+ "ctype = enum(' ', 'Letter', 'Digit', 'Other')\n",
+ "ch = raw_input(\"Enter a any character.\")\n",
+ "a = ch.isalpha()\n",
+ "if a != False:\n",
+ " print \"%c is %d type of symbol.\" %(ch,ctype.Letter)\n",
+ "else:\n",
+ " a = ch.isdigit()\n",
+ " if a != False:\n",
+ " print \"%c is %d type of symbol.\" %(ch,ctype.Digit)\n",
+ " else:\n",
+ " print \"%c is %d type of symbol.\" %(ch,ctype.Other)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter a any character.%\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "% is 3 type of symbol.\n"
+ ]
+ }
+ ],
+ "prompt_number": 143
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Case Study: 1, Page Number: 5.29"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#Program for sorting an array of elements\n",
+ "\n",
+ "a = []\n",
+ "n = input(\"Enter Upper limit....\")\n",
+ "for i in range(0,n):\n",
+ " a.append(0)\n",
+ "print \"Enter elements....\" \n",
+ "for i in range(0,n):\n",
+ " a[i]=input()\n",
+ "for i in range(0,n):\n",
+ " for j in range(i,n):\n",
+ " if a[i]>a[j]:\n",
+ " t = a[i]\n",
+ " a[i] = a[j]\n",
+ " a[j] = t\n",
+ "print \"Numbers in Ascending order\"\n",
+ "for i in range(0,n):\n",
+ " print \"%d\" %a[i],"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter Upper limit....6\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter elements....\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "5\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "1\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "4\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "6\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "2\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "3\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Numbers in Ascending order\n",
+ "1 2 3 4 5 6\n"
+ ]
+ }
+ ],
+ "prompt_number": 1
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Case Study: 2, Page Number: 5.31"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#Program to fing Median for a given 'n' numbers\n",
+ "\n",
+ "arr = []\n",
+ "n = input(\"Enter Upper limit....\")\n",
+ "for i in range(0,n):\n",
+ " arr.append(0)\n",
+ "print \"Enter..%d..values\" %n \n",
+ "for i in range(0,n):\n",
+ " arr[i]=input()\n",
+ "for i in range(1,n):\n",
+ " for j in range(1,n-i):\n",
+ " if arr[j] <= arr[j+1]:\n",
+ " t = arr[j]\n",
+ " arr[j] = arr[j+1]\n",
+ " arr[j+1] = t\n",
+ " else:\n",
+ " continue\n",
+ "if n%2 == 0:\n",
+ " median = (arr[n/2]+arr[n/2+1])/2.0\n",
+ "else:\n",
+ " median = arr[n/2+1]\n",
+ "print \"Median is....%.f\" %median \n",
+ " "
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter Upper limit....3\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter..3..values\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "10\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "20\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "30\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Median is....20\n"
+ ]
+ }
+ ],
+ "prompt_number": 162
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Case Study: 3, Page Number: 5.32"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#Program to find standard deviation for the given data\n",
+ "import math\n",
+ "\n",
+ "val = []\n",
+ "sum = ssqr = n = 0\n",
+ "for i in range(0,100):\n",
+ " val.append(0)\n",
+ "print \"Enter Values -999 to stop....\"\n",
+ "for i in range(0,100):\n",
+ " val[i] = float(raw_input())\n",
+ " if val[i] == -999:\n",
+ " break\n",
+ " sum = float(sum) + val[i] \n",
+ " n = n + 1\n",
+ "mean = float(sum) / float(n) \n",
+ "for i in range(0,n):\n",
+ " dev = val[i] - mean\n",
+ " ssqr = float(ssqr) + float(dev)*float(dev)\n",
+ "var = ssqr / float(n) \n",
+ "sd = math.sqrt(float(var))\n",
+ "print \"Number of Items.....%d\" %n\n",
+ "print \"Mean is .....%f\" %mean\n",
+ "print \"Standard Deviation is .....%f\" %sd"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter Values -999 to stop....\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "10\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "20\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "30\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "40\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "50\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "-999\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Number of Items.....5\n",
+ "Mean is .....30.000000\n",
+ "Standard Deviation is .....14.142136\n"
+ ]
+ }
+ ],
+ "prompt_number": 169
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Case Study: 4, Page Number: 5.34"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#Bubble sort using function\n",
+ "\n",
+ "def bubble(n, x = []):\n",
+ " for item in range(0,n-1):\n",
+ " for i in range(item+1,n):\n",
+ " if x[i]<x[item]:\n",
+ " temp = x[item]\n",
+ " x[item] = x[i]\n",
+ " x[i] = temp\n",
+ " \n",
+ "a = [] \n",
+ "n = input(\"Enter upper limit: \")\n",
+ "for i in range(0,n):\n",
+ " a.append(0)\n",
+ "for i in range(0,n):\n",
+ " a[i] = input()\n",
+ " \n",
+ "bubble(n,a) \n",
+ "print \"After sorting\",\n",
+ "for i in range(0,n):\n",
+ " print \"%d\" %a[i],"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter upper limit: 5\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "1\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "5\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "3\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "4\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "2\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "After sorting 1 2 3 4 5\n"
+ ]
+ }
+ ],
+ "prompt_number": 174
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Case Study: 5, Page Number: 5.35"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#Program to sort an element using Insertion sort\n",
+ "\n",
+ "a = []\n",
+ "x = input(\"Elements to be inserted \")\n",
+ "for i in range(0,100):\n",
+ " a.append(0)\n",
+ "i = 0\n",
+ "while x != -99:\n",
+ " k = i-1\n",
+ " while (x<a[k]) and (k>=0):\n",
+ " a[k+1] = a[k]\n",
+ " k = k-1\n",
+ " a[k+1]=x\n",
+ " print \"Array after inserting %d:\" %x\n",
+ " for j in range(0,i):\n",
+ " print \"%5d\" %a[j]\n",
+ " x = input(\"Elements to be inserted(-99 to stop)?\")\n",
+ " i = i+1\n",
+ "print \"The final sorted array\"\n",
+ "for j in range(0,i):\n",
+ " print \"%5d\" %a[j],"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Elements to be inserted 5\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Array after inserting 5:\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Elements to be inserted(-99 to stop)?3\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Array after inserting 3:\n",
+ " 3\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Elements to be inserted(-99 to stop)?4\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Array after inserting 4:\n",
+ " 3\n",
+ " 4\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Elements to be inserted(-99 to stop)?2\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Array after inserting 2:\n",
+ " 2\n",
+ " 3\n",
+ " 4\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Elements to be inserted(-99 to stop)?1\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Array after inserting 1:\n",
+ " 1\n",
+ " 2\n",
+ " 3\n",
+ " 4\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Elements to be inserted(-99 to stop)?-99\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "The final sorted array\n",
+ " 1 2 3 4 5\n"
+ ]
+ }
+ ],
+ "prompt_number": 186
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Case Study: 6, Page Number: 5.36"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#Program to sort an element using selection sort\n",
+ "\n",
+ "a = []\n",
+ "for i in range(0,100):\n",
+ " a.append(0)\n",
+ "n = input(\"Enter Number of elements in array...\")\n",
+ "print \"Enter Elements...\",n\n",
+ "for i in range(0,n):\n",
+ " a[i] = input()\n",
+ "for i in range(0,n-1):\n",
+ " m = i\n",
+ " for j in range(i+1,n):\n",
+ " if a[m]>a[j]:\n",
+ " m = j\n",
+ " if i != m:\n",
+ " k = a[i]\n",
+ " a[i] = a[m]\n",
+ " a[m] = k\n",
+ " \n",
+ "print \"Sorted Array (Selection Sort) is...\\n\"\n",
+ "for i in range(0,n):\n",
+ " print \"%5d\" %a[i],"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter Number of elements in array...9\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter Elements... 9\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "56\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "326\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "85\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "42\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "156\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "845\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "66\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "1\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "55\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Sorted Array (Selection Sort) is...\n",
+ "\n",
+ " 1 42 55 56 66 85 156 326 845\n"
+ ]
+ }
+ ],
+ "prompt_number": 190
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Case Study: 7, Page Number: 5.38"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#Quick sort\n",
+ "\n",
+ "def quickSort(array):\n",
+ " quickSortHelper(array,0,len(array)-1)\n",
+ "\n",
+ "def quickSortHelper(array,right,left):\n",
+ " if right<left:\n",
+ " splitpoint = partition(array,right,left)\n",
+ " quickSortHelper(array,right,splitpoint-1)\n",
+ " quickSortHelper(array,splitpoint+1,left)\n",
+ "\n",
+ "def partition(array,right,left):\n",
+ " pivotvalue = array[right]\n",
+ "\n",
+ " begin = right+1\n",
+ " end = left\n",
+ "\n",
+ " done = False\n",
+ " while not done:\n",
+ "\n",
+ " while begin <= end and \\\n",
+ " array[begin] <= pivotvalue:\n",
+ " begin = begin + 1\n",
+ "\n",
+ " while array[end] >= pivotvalue and \\\n",
+ " end >= begin:\n",
+ " end = end -1\n",
+ "\n",
+ " if end < begin:\n",
+ " done = True\n",
+ " else:\n",
+ " temp = array[begin]\n",
+ " alist[begin] = array[end]\n",
+ " alist[end] = temp\n",
+ "\n",
+ " temp = array[right]\n",
+ " array[right] = array[end]\n",
+ " array[end] = temp\n",
+ " return end\n",
+ "\n",
+ "array = []\n",
+ "n=input(\"Enter the values in array: \")\n",
+ "for i in range (0,n):\n",
+ " array.append(0)\n",
+ "for i in range (0,n):\n",
+ " array[i]=input()\n",
+ " \n",
+ "quickSort(array)\n",
+ "print \"Quick Sorted Array is\", array"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter the values in array: 5\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "1\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "3\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "2\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "5\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "4\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Quick Sorted Array is [1, 2, 3, 4, 5]\n"
+ ]
+ }
+ ],
+ "prompt_number": 15
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Case Study: 8, Page Number: 5.40"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#program to find the specified elements from the array using linear searching\n",
+ "\n",
+ "a = [100]\n",
+ "for i in range(0,100):\n",
+ " a.append(0)\n",
+ "c = 'y'\n",
+ "while (c=='y')or(c=='Y'):\n",
+ " no = input(\"Enter the size of sorting\")\n",
+ " print \"Enter the elements of the array\"\n",
+ " for i in range(0,no):\n",
+ " a[i]=input()\n",
+ " k = input(\"Enter the element to be searched\") \n",
+ " for i in range(0,no):\n",
+ " if k == a[i]:\n",
+ " print \"Element %d is in the position %d\" %(k,i+1)\n",
+ " f=2\n",
+ " break\n",
+ " if f==1:\n",
+ " print \"The entered element is not in the array\"\n",
+ " ch = raw_input(\"If you want to continue y/n \")\n",
+ " c = ch\n",
+ " "
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter the size of sorting5\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter the elements of the array\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "3\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "2\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "1\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "4\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "5\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter the element to be searched4\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Element 4 is in the position 4\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "If you want to continue y/n n\n"
+ ]
+ }
+ ],
+ "prompt_number": 206
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Case Study: 9, Page Number: 5.42"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#Program to find the specified element from array using Binary search\n",
+ "\n",
+ "a = [100]\n",
+ "for i in range(0,100):\n",
+ " a.append(0)\n",
+ "no = input(\"Enter the size of sorting\")\n",
+ "print \"Enter the elements in ascending order\"\n",
+ "for i in range(0,no):\n",
+ " a[i]=input() \n",
+ "t = input(\"Enter the element to be searched\") \n",
+ "l=0\n",
+ "h=no-1\n",
+ "while l <= h:\n",
+ " m = (l+h)/2\n",
+ " if t<a[m]:\n",
+ " h = m-1\n",
+ " elif t>a[m]:\n",
+ " l = m+1\n",
+ " else:\n",
+ " print \"Entered %d is in position %d\" %(t,m+1)\n",
+ " f=2\n",
+ " break\n",
+ "if f==1:\n",
+ " print \"Entered element is not in the array\""
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter the size of sorting5\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter the elements in ascending order\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "1\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "3\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "4\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "5\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "6\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter the element to be searched3\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Entered 3 is in position 2\n"
+ ]
+ }
+ ],
+ "prompt_number": 19
+ }
+ ],
+ "metadata": {}
+ }
+ ]
+} \ No newline at end of file