Vous êtes sur la page 1sur 48

Data Structures and Algorithms CSE 246

Fall-2012 Lecture#13

Multiplication of large integer


The Karatsuba Ofman algorithm provides a striking example of how the Divide and Conquer technique can achieve an asymptotic speedup over an ancient algorithm. The school classroom method of multiplying two n-digit integers requires O(n2) digit operations. He show that a simple recursive algorithm solves the problem in (n) digit operations,
Quratulain 2

Karatsuba Algorithm
X=4837 Y=5813 X and Y are 4-digit number. Divide numbers in to half until you get number of digit one. A=48, B=37 , C=58, D=13 Therefore X*Y= (10n/2 A+B) (10n/2 C+D) By multiplying =10n/2 A 10n/2 C + 10n/2 AD+ 10n/2 BC +BD = 10n/2 AC + 10n/2 (AD+BC)+BD only 4 multiplication
Quratulain 3

KARATSUBA Algorithm
= 10n/2 AC + 10n/2 (AD+BC)+BD (1) Now replace (AD+BC) with only one multiplication (A+B)(C+D)=AC+AD+BC+BD =this inludes (AD+BC) Thus, (AD+BC)=(A+B)(C+D)-AC-BD Finally, replace (AD+BC) in eq(1) , it become = 10n/2 AC + 10n/2 ((A+B)(C+D)-AC-BD)+BD
Quratulain

Algorithm
Multiply(X , Y){ Size=max(X.length,Y.length) If (Size==1) return X*Y;

A=11 ,B=13, C=14, D=12 AC=multiply(A,C) BD=multiply(B,D) ApB=Add(A,B) CpD=Add(B,D) Term=multiply(ApB,CpD) G=Term-AC-BD Newsize=Size/2 Quratulain Temp=Add(BD,AddZero(G,Newsiz

multiply(1113,1 412) Call recursively until number is of digit one.

Visual representation of function call


X=1 , Y=1 Size=1 Return X*Y
X=1213 , Y=1412 Size=4 A=12 ,B=13, C=14, D=12 AC=multiply(A,C) BD=multiply(B,D) ApB=Add(A,B) CpD=Add(B,D) Ret 69 Term=multiply(ApB,CpD) G=Term-AC-BD Newsize=2 Temp=Add(BD,AddZero(G,newsize )) Return Add(Temp,AddZero(Temp,size)) X=1 , Y=1 Size=1 Return X*Y X=3, Y=2 Size=1 Return X*Y Return 1+3

Call
X=2 , Y=4 Size=1 Return X*Y Return 1+2 Return 1+4

X=3 , Y=5 Size=1 Return X*Y Return 8+60 Return 68+100

X=12 , Y=14 size=2 A=1 ,B=2, C=1, D=4 AC=multiply(A,C) BD=multiply(B,D) ApB=Add(A,B) CpD=Add(B,D) Term=multiply(ApB,CpD) G=Term-AC-BD Newsize=1 Temp=Add(BD,AddZero(G,newsize )) Return Add(Temp,AddZero(AC,size))

Do it . And compute final answer


Quratulain 6

X=25 , Y=26 size=4 A=12 ,B=13, C=14, D=11 AC=multiply(A,C) BD=multiply(B,D) ApB=Add(A,B) CpD=Add(B,D) Term=multiply(ApB,CpD) G=Term-AC-BD Newsize=2 Temp=Add(BD,AddZero(G,newsize )) Return Add(Temp,AddZero(Temp,size))

X=13 , Y=12 size=2 A=1 ,B=3, C=1, D=2 AC=multiply(A,C) BD=multiply(B,D) ApB=Add(A,B) CpD=Add(B,D) Term=multiply(ApB,CpD) G=Term-AC-BD Newsize=1 Temp=Add(BD,AddZero(G,newsize )) Return Add(Temp,AddZero(AC,size))

Return 1+2

X=4 , Y=3 Size=1 Return X*Y

Return 6+50
Return 56+100

Elementary Sorting Methods


Lecture #13

Why study elementary methods?


They provide context in which we can learn terminology and basic mechanisms for sorting algorithms These simple methods are actually more effective than the more powerful general-purpose methods in many applications of sorting Third, several of the simple methods extend to better general-purpose methods or are useful in improving the efficiency of more sophisticated methods.
4/28/2013 Muhammad Usman Arif 8

Time
Elementary methods we discuss take time N2 to sort N randomly arranged items. These methods might be fast than most of the sophisticated algorithms for smaller files. Not suitable for large files.

4/28/2013

Muhammad Usman Arif

External or Internal?
If the file to be sorted will fit into memory, then the sorting method is called internal
Internal sort can access any item easily

Sorting files from tape or disk is called external sorting


External sort must access items sequentially, or at least in large blocks

4/28/2013

Muhammad Usman Arif

10

The Bubble Sort Algorithm


The Bubble Sort compares adjacent elements in a list, and swaps them if they are not in order. Each pair of adjacent elements is compared and swapped until the largest element bubbles to the bottom. Repeat this process each time stopping one indexed element less, until you compare only the first two elements in the list. We know that the array is sorted after both of the nested loops have finished.
4/28/2013 Muhammad Usman Arif 11

A Bubble Sort Example


Compare

We start by comparing the first two elements in the List.

6 5 4 3 2 1
12

4/28/2013

Muhammad Usman Arif

A Bubble Sort Example


Swap

5 6 4 3 2 1
Muhammad Usman Arif 13

4/28/2013

A Bubble Sort Example


5 6 4 3 2 1
14

Compare

4/28/2013

Muhammad Usman Arif

A Bubble Sort Example


5 4 6 3 2 1
Muhammad Usman Arif 15

Swap

4/28/2013

A Bubble Sort Example


5 4 6 3 2 1
16

Compare

4/28/2013

Muhammad Usman Arif

A Bubble Sort Example


5 4 3 6 2 1
Muhammad Usman Arif 17

Swap

4/28/2013

A Bubble Sort Example


5 4 3 6 2 1
18

Compare

4/28/2013

Muhammad Usman Arif

A Bubble Sort Example


5 4 3 2 6 1
Muhammad Usman Arif 19

Swap

4/28/2013

A Bubble Sort Example


5 4 3 2 6 1
20

Compare

4/28/2013

Muhammad Usman Arif

A Bubble Sort Example


As you can see, the largest number has bubbled down to the bottom of the List after the first pass through the List.

Swap

5 4 3 2 1 6
21

4/28/2013

Muhammad Usman Arif

A Bubble Sort Example


Compare For our second pass through the List, we start by comparing these first two elements in the List.

5 4 3 2 1 6
22

4/28/2013

Muhammad Usman Arif

A Bubble Sort Example


Swap

4 5 3 2 1 6
Muhammad Usman Arif 23

4/28/2013

A Bubble Sort Example


4 5 3 2 1 6
24

Compare

4/28/2013

Muhammad Usman Arif

A Bubble Sort Example


4 3 5 2 1 6
Muhammad Usman Arif 25

Swap

4/28/2013

A Bubble Sort Example


4 3 5 2 1 6
26

Compare

4/28/2013

Muhammad Usman Arif

A Bubble Sort Example


4 3 2 5 1 6
Muhammad Usman Arif 27

Swap

4/28/2013

A Bubble Sort Example


4 3 2 5 1 6
28

Compare

4/28/2013

Muhammad Usman Arif

A Bubble Sort Example


At the end of the second pass, we stop at element number n - 1, because the largest element in the List is already in the last position.

Swap

4 3 2 1 5 6
29

4/28/2013

Muhammad Usman Arif

A Bubble Sort Example


Compare We start with the first two elements again at the beginning of the third pass.

4 3 2 1 5 6
30

4/28/2013

Muhammad Usman Arif

A Bubble Sort Example


Swap

3 4 2 1 5 6
Muhammad Usman Arif 31

4/28/2013

A Bubble Sort Example


3 4 2 1 5 6
32

Compare

4/28/2013

Muhammad Usman Arif

A Bubble Sort Example


3 2 4 1 5 6
Muhammad Usman Arif 33

Swap

4/28/2013

A Bubble Sort Example


3 2 4 1 5 6
34

Compare

4/28/2013

Muhammad Usman Arif

A Bubble Sort Example


At the end of the third pass, we stop comparing and swapping at element number n - 2. Swap

3 2 1 4 5 6
35

4/28/2013

Muhammad Usman Arif

A Bubble Sort Example


Compare The beginning of the fourth pass...

3 2 1 4 5 6
36

4/28/2013

Muhammad Usman Arif

A Bubble Sort Example


Swap

2 3 1 4 5 6
Muhammad Usman Arif 37

4/28/2013

A Bubble Sort Example


2 3 1 4 5 6
38

Compare

4/28/2013

Muhammad Usman Arif

A Bubble Sort Example


2 1 3 4 5 6
39

Swap The end of the fourth pass stops at element number n - 3.

4/28/2013

Muhammad Usman Arif

A Bubble Sort Example


Compare

The beginning of the fifth pass...

2 1 3 4 5 6
40

4/28/2013

Muhammad Usman Arif

A Bubble Sort Example


Swap The last pass compares only the first two elements of the List. After this comparison and possible swap, the smallest element has bubbled to the top.

1 2 3 4 5 6
41

4/28/2013

Muhammad Usman Arif

What Swapping Means


TEMP

6
Place the first element into the Temporary Variable.

6 5 4 3 2 1
42

4/28/2013

Muhammad Usman Arif

What Swapping Means


TEMP

6
Replace the first element with the second element.

5 5 4 3 2 1
43

4/28/2013

Muhammad Usman Arif

What Swapping Means


TEMP

6
Replace the second element with the Temporary Variable.

5 6 4 3 2 1
44

4/28/2013

Muhammad Usman Arif

Java Code For Bubble Sort

4/28/2013

Muhammad Usman Arif

45

Java Code for Bubble sort

4/28/2013

Muhammad Usman Arif

46

Big - O Notation
Big - O notation is used to describe the efficiency of a search or sort. The actual time necessary to complete the sort varies according to the speed of your system. Big - O notation is an approximate mathematical formula to determine how many operations are necessary to perform the search or sort. The Big - O notation for the Bubble Sort is O(n2), because it takes approximately n2 passes to sort the elements.

4/28/2013

Muhammad Usman Arif

47

Bubble sort. Simplest of all, but also slowest


Worst case performance Best case performance Makes passes through a sequence of items. On each pass it compares adjacent elements in pairs and swaps them if they are out of order.

4/28/2013

Muhammad Usman Arif

48

Vous aimerez peut-être aussi