• No results found

IIT Bombay

N/A
N/A
Protected

Academic year: 2022

Share "IIT Bombay"

Copied!
42
0
0

Loading.... (view fulltext now)

Full text

(1)

IIT Bombay

Computer Programming

Dr. Deepak B Phatak Dr. Supratik Chakraborty

Department of Computer Science and Engineering

IIT Bombay

(2)

IIT Bombay

• The sorting problem

• Selection sort

• Intuition

• C++ implementation

• Analysis of performance

2 Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay

Quick Recap of Relevant Topics

(3)

IIT Bombay

• Merge sort

• Intuition

• Animated example

Overview of This Lecture

(4)

IIT Bombay

Recall: Intuition Behind Selection Sort

Solve part of the problem

Arrive at a similar but

“simpler” problem to solve:

Sort remaining marks

Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay 4

24 18 17 25 27 24 1

1 27

Get only the first

element in its right place

18

17

25

24

24

(5)

IIT Bombay

A General Paradigm In Computing

• Decompose a larger problem into smaller sub-problems

• Solve each sub-problem separately

Often using same techniques as used to address the larger problem

• Combine results of sub-problems to obtain solution of larger

problem

(6)

IIT Bombay

Sorting by Divide-and-Conquer

Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay 6

24 18 17 25 27 24

24 18 17 25 27 24 Divide

Similar but simpler problem Sort second half of array Similar but simpler problem

Sort first half of array

(7)

IIT Bombay

Sorting by Divide-and-Conquer

24 18 17 25 27

24 18 17 25 27 Divide

Similar but simpler problem Sort second half of array

24 18 17

Sort 24

18

17

(8)

IIT Bombay

Sorting by Divide-and-Conquer

Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay 8

24 18 17 25 27 24

24 18 17 25 27 24 Divide

24 18 17 27 25 24 Sort

Sort

Merge

27

25

24

24

18

17

(9)

IIT Bombay

What Were The Steps?

• Divide an array of size n into two sub-arrays of size  n/2

• Sub-array sizes may differ by 1 if n is odd

• Easy!

• Sort each sub-array of size n/2

• Hmm … how?

• Selection sort ???

• Merge sorted sub-arrays, each of size n/2

Wait for a

few slides !!!

(10)

IIT Bombay

Merging Sorted Sub-arrays

Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay 10

24 18 17 27 25 24 27 25 24

24 18 17

27 > 24

Sorted

Sub-array1

Sorted

Sub-array2

(11)

IIT Bombay

Merging Sorted Sub-arrays

24 18 17 27 24 18 17

25 > 24

Sorted 27

Sub-array1

Sorted

(12)

IIT Bombay

Merging Sorted Sub-arrays

Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay 12

24 18 17 27 25 24 27 25 24

24 18 17

24 > 24

27 25 Sorted

Sub-array1

Sorted

Sub-array2

(13)

IIT Bombay

Merging Sorted Sub-arrays

24 18 17 27 27 24 18 17

24 > 18

27 25 24 Sorted

Sub-array1

Sorted

(14)

IIT Bombay

Merging Sorted Sub-arrays

Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay 14

24 18 17 27 25 24 27 25 24

24 18 17 Sorted

Sub-array1

Sorted

Sub-array2

Sub-array2

27 over

25

24

24

(15)

IIT Bombay

Merging Sorted Sub-arrays

24 18 17 27 27 24 18 17 Sorted

Sub-array1

Sorted

Sub-array2

27 over

25

24

24

18

(16)

IIT Bombay

Merging Sorted Sub-arrays

Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay 16

24 18 17 27 25 24 27 25 24

24 18 17 Sorted

Sub-array1

Sorted

Sub-array2

24 18 24 27 25 24 24 18 17

Both sorted sub-arrays

over

Merged sorted array

(17)

IIT Bombay

What Were The Steps?

• Divide an array of size n (assume n is even) into two sub- arrays of size n/2

• Easy!

• Sort each sub-array of size n/2

• Hmm … how?

• Selection sort ???

• Merge sorted sub-arrays, each of size n/2

We were trying to sort an array

of size n

(18)

IIT Bombay

What Were The Steps?

• Divide an array of size n (assume n is even) into two sub- arrays of size n/2

• Easy!

• Sort each sub-array of size n/2

• Hmm … how?

• Selection sort ???

• Merge sorted sub-arrays, each of size n/2

• Hmm … how?

Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay 18

Why not try the same steps

on each sub-

array?

(19)

IIT Bombay

What Were The Steps?

• Divide an array of size n (assume n is even) into two sub- arrays of size n/2

• Easy!

• Sort each sub-array of size n/2

• Hmm … how?

• Selection sort ???

• Merge sorted sub-arrays, each of size n/2

Recursively sort each sub- array by same

method

Termination case of recursion:

(20)

IIT Bombay

Divide-and-Conquer In Action

Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay 20

24 18 17 25 27 24

24 18 17 25 27 24 Divide

Similar but simpler problems

(21)

IIT Bombay

Divide-and-Conquer In Action

24 18 17 25 27

24 18 17 25 27

24

18

Divide 17

(22)

IIT Bombay

Divide-and-Conquer In Action

Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay 22

24 18 17 25 27 24

24 18 17

25 27 24

24 18 17

Termination case

Similar but simpler problems

(23)

IIT Bombay

17

Divide-and-Conquer In Action

24 18 17 25 27

24 18 17

25 27

24 18 17

18

Divide

(24)

IIT Bombay

Similar but simpler problems

Divide-and-Conquer In Action

Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay 24

24 18 17 25 27 24

24 18 17

25 27 24

24

18 17

18 17

Termination case

(25)

IIT Bombay

Divide-and-Conquer In Action

24

18 17

Termination case 24

18 17 25 27

24 18 17

18 17 25

27

(26)

IIT Bombay

Divide-and-Conquer In Action

Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay 26

25 27 24

24

18 17 Merge

Similar but simpler problem

24 18 17 25 27 24

24 18 17

18

17

(27)

IIT Bombay

Divide-and-Conquer In Action

25 27 24 24

18 17 25 27

24 18 17

18

17

(28)

IIT Bombay

Divide-and-Conquer In Action

Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay 28

25 27 24

24 18 Merge 17

Similar but simpler problem

24 18 17 25 27 24

24

18

17

(29)

IIT Bombay

Divide-and-Conquer In Action

24 18 17 25 27 24

18

17

25

27

(30)

IIT Bombay

Divide-and-Conquer In Action

Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay 30

24 18 17 25 27 24

Similar but simpler problems

25 27 24 Divide

24

18

17

25

27

24

(31)

IIT Bombay

Divide-and-Conquer In Action

24 18 17

25 27

25

Termination case

Similar but simpler problem

24

18

17

25

27

(32)

IIT Bombay

Divide-and-Conquer In Action

Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay 32

24 18 17

25 27 24

Similar but simpler problems

24 27 Divide

24 18 17 25 27 24

25

27

24

(33)

IIT Bombay

Divide-and-Conquer In Action

24 18 17

25

Similar but simpler problem

27

Termination case 25

27 24

18

17

25

27

(34)

IIT Bombay

Divide-and-Conquer In Action

Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay 34

24 18 17

25

24 27

Termination case 24

18 17 25 27 24

25 27 24

27

24

(35)

IIT Bombay

Merge

Divide-and-Conquer In Action

24 18 17

25

27 24

18 17 25 27

25

27

(36)

IIT Bombay

Divide-and-Conquer In Action

Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay 36

24 18 17

25 24

18 17 25 27 24

25 27 24

27

24

(37)

IIT Bombay

Divide-and-Conquer In Action

24 18 17

25 Merge

24 18 17 25 27

25

27

(38)

IIT Bombay

Divide-and-Conquer In Action

Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay 38

24 18 17 27 25 24 24

18

17

25

27

24

(39)

IIT Bombay

Divide-and-Conquer In Action

24 18 17 27 25 Merge

24

18

17

25

27

(40)

IIT Bombay

Divide-and-Conquer In Action

Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay 40

27 25 24 24 18 17

Sorted array !!!

(41)

IIT Bombay

Merge Sort

24 18 17 25 27

27 25 24 24 18

MERGE SORT

(42)

IIT Bombay

Summary

• Merge sort

• Intuition

• Divide-and-conquer approach, leading to recursive formulation

• Key role of merging sorted sub-arrays

Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay 42

References

Related documents

KR School of Information Technology

Session 15,Software Engineering and Course Projects Friday, September 23, 2011..

• Many problems will deal with a large number of values, performing similar operations on each value.. • In this case, we are required to write a separate instruction for

Adverbs of place Where the verb took place walking near the house, here, there..

● Compile the client program with the client stub, and procedure implementation with the server stub, into two independent client and server programs.. IIT Bombay cs

● Resulting cipher text is encrypted again using receiver’s public key, for confidentiality. ● Receiver first decrypts with private key, then decrypts with senders’

● Instead of the entire routing table, a router only provides the state (cost and status) of its directly connected links3. ● Routers use flooding to disseminate

• How will producing an ETD affect my ability to later publish an article or book based on or related to my thesis or dissertation. • Are the rules governing the use of