lec 13 bblsort mul
TRANSCRIPT
-
7/30/2019 Lec 13 BblSORT Mul
1/48
Fall-2012
Lecture#13
Data Structures and Algorithms
CSE 246
-
7/30/2019 Lec 13 BblSORT Mul
2/48
Multiplication of large integer
Quratulain 2
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) digitoperations.
He show that a simple recursive algorithm
solves the problem in (n) digit operations,
-
7/30/2019 Lec 13 BblSORT Mul
3/48
Karatsuba Algorithm
Quratulain 3
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
-
7/30/2019 Lec 13 BblSORT Mul
4/48
KARATSUBA Algorithm
Quratulain 4
= 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
-
7/30/2019 Lec 13 BblSORT Mul
5/48
Algorithm
Quratulain 5
multiply(1113,1
412)
Call recursivelyuntil number is
of digit one.
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/2Tem =Add BD AddZero G Newsiz
-
7/30/2019 Lec 13 BblSORT Mul
6/48
Quratulain 6
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)Term=multiply(ApB,CpD)
G=Term-AC-BD
Newsize=2
Temp=Add(BD,AddZero(G,newsize
))
Return
Add(Temp,AddZero(Temp,size))
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-BDNewsize=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))
X=1 , Y=1
Size=1
Return
X*Y
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
Call
Ret 69
X=1 , Y=1
Size=1
Return
X*Y
X=3, Y=2
Size=1Return
X*Y
Return 1+3
Return 1+2
X=4 , Y=3
Size=1
Return
X*Y
Return
6+50
Return
56+100
Visual representation of function call
Do it . And compute final answer
-
7/30/2019 Lec 13 BblSORT Mul
7/48
Elementary Sorting
Methods
Lecture #13
-
7/30/2019 Lec 13 BblSORT Mul
8/48
Why study elementary methods?
They provide context in which we can learnterminology and basic mechanisms for sortingalgorithms
These simple methods are actually more effectivethan the more powerful general-purposemethods in many applications of sorting
Third, several of the simple methods extend to
better general-purpose methods or are useful inimproving the efficiency of more sophisticatedmethods.
4/28/2013 Muhammad Usman Arif 8
-
7/30/2019 Lec 13 BblSORT Mul
9/48
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 9
-
7/30/2019 Lec 13 BblSORT Mul
10/48
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
-
7/30/2019 Lec 13 BblSORT Mul
11/48
The Bubble Sort
Algorithm
The Bubble Sort compares adjacent elements in alist, and swaps them if they are not in order.Each pair of adjacent elements is compared andswapped until the largest element bubbles tothe bottom. Repeat this process each timestopping 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 thenested loops have finished.
4/28/2013 Muhammad Usman Arif 11
-
7/30/2019 Lec 13 BblSORT Mul
12/48
A Bubble Sort Example
4/28/2013 Muhammad Usman Arif 12
6
5
43
2
1
Compare
We start by comparing the first
two elements in the List.
-
7/30/2019 Lec 13 BblSORT Mul
13/48
4/28/2013 Muhammad Usman Arif 13
A Bubble Sort Example
5
6
43
2
1
Swap
-
7/30/2019 Lec 13 BblSORT Mul
14/48
4/28/2013 Muhammad Usman Arif 14
A Bubble Sort Example
5
6
43
2
1
Compare
-
7/30/2019 Lec 13 BblSORT Mul
15/48
4/28/2013 Muhammad Usman Arif 15
A Bubble Sort Example
5
4
63
2
1
Swap
-
7/30/2019 Lec 13 BblSORT Mul
16/48
4/28/2013 Muhammad Usman Arif 16
A Bubble Sort Example
5
4
63
2
1
Compare
-
7/30/2019 Lec 13 BblSORT Mul
17/48
4/28/2013 Muhammad Usman Arif 17
A Bubble Sort Example
5
4
36
2
1
Swap
-
7/30/2019 Lec 13 BblSORT Mul
18/48
4/28/2013 Muhammad Usman Arif 18
A Bubble Sort Example
5
4
36
2
1
Compare
-
7/30/2019 Lec 13 BblSORT Mul
19/48
4/28/2013 Muhammad Usman Arif 19
A Bubble Sort Example
5
4
32
6
1
Swap
-
7/30/2019 Lec 13 BblSORT Mul
20/48
4/28/2013 Muhammad Usman Arif 20
A Bubble Sort Example
5
4
32
6
1Compare
-
7/30/2019 Lec 13 BblSORT Mul
21/48
4/28/2013 Muhammad Usman Arif 21
A Bubble Sort Example
5
4
32
1
6Swap
As you can see, the largest
number has bubbled down to
the bottom of the List after the
first pass through the List.
-
7/30/2019 Lec 13 BblSORT Mul
22/48
4/28/2013 Muhammad Usman Arif 22
A Bubble Sort Example
5
4
32
1
6
Compare
For our second pass throughthe List, we start by
comparing these first two
elements in the List.
-
7/30/2019 Lec 13 BblSORT Mul
23/48
4/28/2013 Muhammad Usman Arif 23
A Bubble Sort Example
4
5
32
1
6
Swap
-
7/30/2019 Lec 13 BblSORT Mul
24/48
4/28/2013 Muhammad Usman Arif 24
A Bubble Sort Example
4
5
32
1
6
Compare
-
7/30/2019 Lec 13 BblSORT Mul
25/48
4/28/2013 Muhammad Usman Arif 25
A Bubble Sort Example
4
3
52
1
6
Swap
-
7/30/2019 Lec 13 BblSORT Mul
26/48
4/28/2013 Muhammad Usman Arif 26
A Bubble Sort Example
4
3
52
1
6
Compare
-
7/30/2019 Lec 13 BblSORT Mul
27/48
4/28/2013 Muhammad Usman Arif 27
A Bubble Sort Example
4
3
25
1
6
Swap
-
7/30/2019 Lec 13 BblSORT Mul
28/48
4/28/2013 Muhammad Usman Arif 28
A Bubble Sort Example
4
3
25
1
6
Compare
-
7/30/2019 Lec 13 BblSORT Mul
29/48
4/28/2013 Muhammad Usman Arif 29
A Bubble Sort Example
4
3
21
5
6
Swap
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.
-
7/30/2019 Lec 13 BblSORT Mul
30/48
4/28/2013 Muhammad Usman Arif 30
A Bubble Sort Example
4
3
21
5
6
Compare
We start with the first twoelements again at the beginning
of the third pass.
-
7/30/2019 Lec 13 BblSORT Mul
31/48
4/28/2013 Muhammad Usman Arif 31
A Bubble Sort Example
3
4
21
5
6
Swap
-
7/30/2019 Lec 13 BblSORT Mul
32/48
4/28/2013 Muhammad Usman Arif 32
A Bubble Sort Example
3
4
21
5
6
Compare
-
7/30/2019 Lec 13 BblSORT Mul
33/48
4/28/2013 Muhammad Usman Arif 33
A Bubble Sort Example
3
2
41
5
6
Swap
-
7/30/2019 Lec 13 BblSORT Mul
34/48
4/28/2013 Muhammad Usman Arif 34
A Bubble Sort Example
3
2
41
5
6
Compare
-
7/30/2019 Lec 13 BblSORT Mul
35/48
4/28/2013 Muhammad Usman Arif 35
A Bubble Sort Example
3
2
14
5
6
Swap
At the end of the third pass, we stop
comparing and swapping at element
number n - 2.
-
7/30/2019 Lec 13 BblSORT Mul
36/48
4/28/2013 Muhammad Usman Arif 36
A Bubble Sort Example
3
2
14
5
6
Compare
The beginning of the fourth pass...
-
7/30/2019 Lec 13 BblSORT Mul
37/48
4/28/2013 Muhammad Usman Arif 37
A Bubble Sort Example
2
3
14
5
6
Swap
-
7/30/2019 Lec 13 BblSORT Mul
38/48
4/28/2013 Muhammad Usman Arif 38
A Bubble Sort Example
2
3
14
5
6
Compare
-
7/30/2019 Lec 13 BblSORT Mul
39/48
4/28/2013 Muhammad Usman Arif 39
A Bubble Sort Example
2
1
34
5
6
Swap
The end of the fourth pass
stops at element number n - 3.
-
7/30/2019 Lec 13 BblSORT Mul
40/48
4/28/2013 Muhammad Usman Arif 40
A Bubble Sort Example
2
1
34
5
6
Compare
The beginning of the fifth pass...
-
7/30/2019 Lec 13 BblSORT Mul
41/48
4/28/2013 Muhammad Usman Arif 41
A Bubble Sort Example
1
2
34
5
6
Swap
The last pass compares onlythe first two elements of the
List. After this comparison
and possible swap, the
smallest element has
bubbled to the top.
-
7/30/2019 Lec 13 BblSORT Mul
42/48
4/28/2013 Muhammad Usman Arif 42
What Swapping Means
6
5
43
2
1
TEMP
Place the first element into the
Temporary Variable.
6
-
7/30/2019 Lec 13 BblSORT Mul
43/48
4/28/2013 Muhammad Usman Arif 43
What Swapping Means
5
5
43
2
1
TEMP
Replace the first element with
the second element.
6
-
7/30/2019 Lec 13 BblSORT Mul
44/48
4/28/2013 Muhammad Usman Arif 44
What Swapping Means
5
6
43
2
1
TEMP
Replace the second element
with the Temporary Variable.
6
-
7/30/2019 Lec 13 BblSORT Mul
45/48
Java Code For Bubble Sort
4/28/2013 Muhammad Usman Arif 45
-
7/30/2019 Lec 13 BblSORT Mul
46/48
4/28/2013 Muhammad Usman Arif 46
Java Code for Bubble sort
-
7/30/2019 Lec 13 BblSORT Mul
47/48
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 approximatemathematical 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 tosort the elements.
4/28/2013 Muhammad Usman Arif 47
B bbl t Si l t f ll b t
-
7/30/2019 Lec 13 BblSORT Mul
48/48
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.