Professional Documents
Culture Documents
Fall-2012 Lecture#13
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
Call
X=2 , Y=4 Size=1 Return X*Y Return 1+2 Return 1+4
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))
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
Return 6+50
Return 56+100
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
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
4/28/2013
10
6 5 4 3 2 1
12
4/28/2013
5 6 4 3 2 1
Muhammad Usman Arif 13
4/28/2013
Compare
4/28/2013
Swap
4/28/2013
Compare
4/28/2013
Swap
4/28/2013
Compare
4/28/2013
Swap
4/28/2013
Compare
4/28/2013
Swap
5 4 3 2 1 6
21
4/28/2013
5 4 3 2 1 6
22
4/28/2013
4 5 3 2 1 6
Muhammad Usman Arif 23
4/28/2013
Compare
4/28/2013
Swap
4/28/2013
Compare
4/28/2013
Swap
4/28/2013
Compare
4/28/2013
Swap
4 3 2 1 5 6
29
4/28/2013
4 3 2 1 5 6
30
4/28/2013
3 4 2 1 5 6
Muhammad Usman Arif 31
4/28/2013
Compare
4/28/2013
Swap
4/28/2013
Compare
4/28/2013
3 2 1 4 5 6
35
4/28/2013
3 2 1 4 5 6
36
4/28/2013
2 3 1 4 5 6
Muhammad Usman Arif 37
4/28/2013
Compare
4/28/2013
4/28/2013
2 1 3 4 5 6
40
4/28/2013
1 2 3 4 5 6
41
4/28/2013
6
Place the first element into the Temporary Variable.
6 5 4 3 2 1
42
4/28/2013
6
Replace the first element with the second element.
5 5 4 3 2 1
43
4/28/2013
6
Replace the second element with the Temporary Variable.
5 6 4 3 2 1
44
4/28/2013
4/28/2013
45
4/28/2013
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
47
4/28/2013
48