You are on page 1of 11

ESc101: Fundamentals of Computing

2011-12-Monsoon Semester Lecture #37, November 3, 2011

Please switch off your mobile phones.

Announcements
Monday sections will do lab 12 on 5th November (Saturday) y ( y) instead of 7th November (Idul Zuha) Lab exam in the week of 14th to 18th November End-sem exam is on 25th November, 8:00 AM Copies can be seen on 28th afternoon afternoon.

Lec-36

Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon

Best Programmers of Lab 9


Section E1 E3 E5 E7 E9 E11 E13 E15 none Jayesh Kumar Gupta none Mohit Sharma G V S Sasank Mouli Manish Kumar Sharma Karan Singh Abhinay Kumar Name Section E2 E4 E6 E8 E10 E12 E14 E14 Name Siril Pal Pappula Tanmay Sanjay Jaipurkar Sanjay Moudgalya Nikunj Agrawal Gagan Agrawal Samyak Daga Aashish Gupta Ankit Jalan

Lec-36

Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon

Recap
Sorting Algorithms
Insertion Sort Bubble Sort Merge Sort

Lec-36

Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon

Recap: Insertion Sort


Based on the following principle:
Take an element from unsorted list, and place it in the sorted p list at the correct position

For an in-array sort,


The first i elements are sorted
initially i is 1.

Take the (i+1)th element Find the right place for this element amongst the first i+1 positions in the array, and place it there
This can be done by comparing with previous element and if the new element is larger, exchange Keep doing this, till you dont need to exchange.

Lec-36

Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon

Recap: Insertion Sort

http://www.youtube.com/watch?v=ROalU379l3U http://en.wikipedia.org/wiki/Insertion_sort http://www.youtube.com/watch?v=ejpFmtYM8Cw

Lec-36

Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon

Recap: Bubble Sort


The idea here is to keep pushing the largest element to the end in each iteration.
So the first iteration will see the largest element move to the end The second iteration will look at the remaining list, and move the now largest element to the end of this remaining list means that it moves to second last element of original list. Will keep doing this till we are left with just one element. W eep do g t s t a e e twt o e e e e t.

Lec-36

Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon

Recap: Bubble Sort


http://www youtube com/watch?v=lyZQPjUT5B4 http://www.youtube.com/watch?v lyZQPjUT5B4 http://www.youtube.com/watch?v=0mNIYv369eM http://www.youtube.com/watch?v=TQavvE1YZE&annotation_id=annotation_207695&src_vid=myKlT 30nl5Y&feature=iv http://en.wikipedia.org/wiki/Bubble_sort

Lec-36

Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon

Recap: Merge Sort


It is a recursive sorting algorithm. algorithm
If the list is of length 0 or 1, then it is already sorted. Otherwise,
Divide the unsorted list into two sublists of about half the size. Sort each sublist recursively by re-applying merge sort Merge the two sublists back into one sorted list. g

Lec-36

Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon

Recap: Merge Sort


http://www youtube com/watch?v=XaqR3G NVoo http://www.youtube.com/watch?v XaqR3G_NVoo http://www.youtube.com/watch?v=dVaHHwtf8KM http://en.wikipedia.org/wiki/Merge_sort

Lec-36

Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon

Selection Sort
The basic algorithm works as follows:
Find the minimum value in the list Swap it with the value in the first position Repeat these two steps for the remainder of the list (starting at the second position and advancing each time)

Lec-36

Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon

10

Selection Sort (example)


64 11 11 11 11 25 25 12 12 12 12 12 25 22 22 22 22 22 25 25 11 64 64 64 64

Lec-36

Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon

11

Selection Sort (Links)


http://en wikipedia org/wiki/Selection sort http://en.wikipedia.org/wiki/Selection_sort http://www.youtube.com/watch?v=Ns4TPTC8whw http://www.youtube.com/watch?v=hqBPYhAQeTI

Lec-36

Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon

12

Algorithm: Counting routes in a grid

Lec-36

Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon

13

Algorithm: Counting routes in a grid


Number of routes from 0,0 to i, j 00 i
Need to travel right i times Need to travel up j times right and up can be intermixed any number of times So basically ordering of i rights and j ups C (i+j, i)

Lec-36

Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon

14

Algorithm: Number of bit sum primes


How many distinct integers that a computer can store in 32-bits are bit-sum primes A bit sum prime is a number whose sum of digits in the binary representation is a prime number Following numbers are bit-sum prime
1001 1010100010000001

Lec-36

Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon

15

Bit sum primes


List all prime numbers till 32
2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31

Count distinct numbers with


2 ones 3 ones 31 ones

Add them all up.


Lec-36 Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon 16

Bit sum primes


C(32 2) + C (32, 3) + + C (32 31) C(32, (32 (32,

Lec-36

Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon

17

Algorithm: Next Palindrome


Given a positive integer, print the first palindrome greater integer than this number.

Lec-36

Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon

18

Algorithm: Next Palindrome


Examples:
Let the number be: 1762345 Half way mark is 4th digit (2) Palindrome is: 1762671
Lec-36

Let the number be: 81234567 Half H lf way mark is between 4th and 5th di i (3 and 4) ki b d digits d A palindrome is: 81233218, but it is smaller Increment 8123 (half number) to 8124 and now the palindrome is: 81244218
Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon 19

10

Algorithm: Next Palindrome


Count the number of digits Look at the half way mark Construct a palindrome based on this half way mark If the new number is greater than the original number, then the problem is solved. Else, add one to the half number and now construct the palindrome.

Lec-36

Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon

20

Any Questions?

Lec-36

Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon

21

11

You might also like