SlideShare une entreprise Scribd logo
1  sur  57
Presented to: Madam sobia kazmi
Presented by:-
kinza Arshad(05)
Sayyida Tabinda kokab(08)
Qandeel Saddique(18)
Parveen Tabassum(33)
Amna Aftab(34)
Adin Ajaz(37)
Presentation outlines
 Merge sort
 B-trees
 Hashing
 Heap trees
Merge Sort
 SORTING:-arranging array elements in ascending order
 MERGE SORT:- A sorting technique to arrange array element in
ascending order
 sorting elements by merging elements
 combo of sorting and merging
Merge Sort (cont,,,)
CONCEPT:
1. Divide the list in half
2. Merge sort the first half
3. Merge sort the second half
4. Merge both halves back together
Merge Sort (cont,,,)
 Merge sort is based on the divide-and-conquer paradigm.
 To sort A[p .. r]:
1. Divide Step
If a given array A has zero or one element, simply return; it is already
sorted. Otherwise, split A[p .. r] into two subarrays A[p .. q] and A[q + 1 .. r],
each containing about half of the elements of A[p .. r]. That is, q is the
halfway point of A[p .. r].
2. Conquer Step
Conquer by sorting the two subarrays A[p .. q] and A[q + 1 .. r].
Merge Sort (cont,,,)
3. Combine Step
Combine the elements back in A[p .. r] by merging the two sorted
subarrays A[p .. q] and A[q + 1 .. r] into a sorted sequence. To accomplish
this step, we will define a procedure MERGE (A, p, q, r).
Note that the recursion bottoms out when the subarray has just one
element, so that it is trivially sorted.
Merge sort ( cont,,,)
 Merge sort (divide-and-conquer)
 Divide array into two halves.
A L G O R I T H M S
divideA L G O R I T H M S
Merge sort(cont,,,)
 Mergesort (divide-and-conquer)
 Divide array into two halves.
 Recursively sort each half.
sort
A L G O R I T H M S
divideA L G O R I T H M S
A G L O R H I M S T
Merge sort(cont,,,)
 Mergesort (divide-and-conquer)
 Divide array into two halves.
 Recursively sort each half.
 Merge two halves to make sorted whole.
merge
sort
A L G O R I T H M S
divideA L G O R I T H M S
A G L O R H I M S T
A G H I L M O R S T
auxiliary array
smallest smallest
A G L O R H I M S T
Merging
 Merge.
 Keep track of smallest element in each sorted half.
 Insert smallest of two elements into auxiliary array.
 Repeat until done.
A
auxiliary array
smallest smallest
A G L O R H I M S T
A
Merging
 Merge.
 Keep track of smallest element in each sorted half.
 Insert smallest of two elements into auxiliary array.
 Repeat until done.
G
auxiliary array
smallest smallest
A G L O R H I M S T
A G
Merging
 Merge.
 Keep track of smallest element in each sorted half.
 Insert smallest of two elements into auxiliary array.
 Repeat until done.
H
auxiliary array
smallest smallest
A G L O R H I M S T
A G H
Merging
 Merge.
 Keep track of smallest element in each sorted half.
 Insert smallest of two elements into auxiliary array.
 Repeat until done.
I
auxiliary array
smallest smallest
A G L O R H I M S T
A G H I
Merging
 Merge.
 Keep track of smallest element in each sorted half.
 Insert smallest of two elements into auxiliary array.
 Repeat until done.
L
auxiliary array
smallest smallest
A G L O R H I M S T
A G H I L
Merging
 Merge.
 Keep track of smallest element in each sorted half.
 Insert smallest of two elements into auxiliary array.
 Repeat until done.
M
auxiliary array
smallest smallest
A G L O R H I M S T
A G H I L M
Merging
 Merge.
 Keep track of smallest element in each sorted half.
 Insert smallest of two elements into auxiliary array.
 Repeat until done.
O
auxiliary array
smallest smallest
A G L O R H I M S T
A G H I L M O
Merging
 Merge.
 Keep track of smallest element in each sorted half.
 Insert smallest of two elements into auxiliary array.
 Repeat until done.
R
auxiliary array
first half
exhausted smallest
A G L O R H I M S T
A G H I L M O R
Merging
 Merge.
 Keep track of smallest element in each sorted half.
 Insert smallest of two elements into auxiliary array.
 Repeat until done.
S
auxiliary array
first half
exhausted smallest
A G L O R H I M S T
A G H I L M O R S
Merging
 Merge.
 Keep track of smallest element in each sorted half.
 Insert smallest of two elements into auxiliary array.
 Repeat until done.
T
Algorithm Merge sort
Input :- An array A[l…r] where l and r are the lower and upper index
of A.
Output :- Array a[l…r] with all element arranged in ascending order .
Steps:
1. if(r<=l) then
2. Return
3. else
4. Mid=[(l+r)/2]
5. MergeSort(A[l…mid])
6. MergeSort(A[mid+1…r])
7. Merge(A,L,mid,r)
8. Endif
9. Stop
Pros:
 It is a stable sort, and there is no worst-case scenario.
 It is faster, the temp array holds the resulting array until both left and right
sides are merged into the temp array, then the temp array is appended
over the input array.
 It is used in tape drives to sort data - its good with parallel processing.
Cons:
 The memory requirement is doubled.
 Takes longer to merge because if the next element is in the right side then
all of the elements must be moved down.
b-trees
Definition of a B-tree
A B-tree of order m is an m-way tree (i.e., a tree where
each node may have up to m children) in which:
 1. the number of keys in each non-leaf node is one less
than the number of its children and these keys partition the
keys in the children in the fashion of a search tree
 2.all leaves are on the same level
 3.all non-leaf nodes except the root have at least m / 2
children
 5. a leaf node contains no more than m – 1 keys
The number m should always be odd
B-Trees 23
An example B-Tree
B-Trees 24
51 6242
6 12
26
55 60
7064 90
45
1 2 4 7 8 13 15 18 25
27 29 46 48 53
A B-tree of order 5
containing 26 items
Note that all the leaves are at the same levelNote that all the leaves are at the same level
Constructing a B-tree
Suppose we start with an empty B-tree and keys
arrive in the following order:1 12 8 2 25 5 14
28 17 7 52 16 48 68 3 26 29 53 55 45
We want to construct a B-tree of order 5
The first four items go into the root:
To put the fifth item in the root would violate
condition 5
Therefore, when 25 arrives, pick the middle key to
make a new root
B-Trees 25
1 2 8 12
Constructing a B-tree
B-Trees 26
Constructing a B-tree (cont,,,)
B-Trees
27
1 2
8
12 25
6, 14, 28 get added to the leaf nodes:
1 2
8
12 146 25 28
Constructing a B-tree (cont,,,)
B-Trees
Adding 17 to the right leaf node would over-fill it, so we take the
middle key, promote it (to the root) and split the leaf
8 17
12 14 25 281 2 6
7, 52, 16, 48 get added to the leaf nodes
8 17
12 14 25 281 2 6 16 48 527
Constructing a B-tree (cont,,,)
B-Trees
Adding 68 causes us to split the right most leaf, promoting 48 to the
root, and adding 3 causes us to split the left most leaf, promoting 3
to the root; 26, 29, 53, 55 then go into the leaves
3 8 17 48
52 53 55 6825 26 28 291 2 6 7 12 14 16
Adding 45 causes a split of 25 26 28 29
and promoting 28 to the root then causes the root to split
Constructing a B-tree (cont,,,)
B-Trees
17
3 8 28 48
1 2 6 7 12 14 16 52 53 55 6825 26 29 45
Inserting into a B-Tree
Attempt to insert the new key into a leaf
If this would result in that leaf becoming too big, split the
leaf into two, promoting the middle key to the leaf’s
parent
If this would result in the parent becoming too big, split
the parent into two, promoting the middle key
This strategy might have to be repeated all the way to
the top
If necessary, the root is split in two and the middle key is
promoted to a new root, making the tree one level higher
B-Trees
Insert Node (63)
B-Trees
After Insert 63
B-Trees
Insert Node (93)
B-Trees
After Insert 99
B-Trees
Removal from a B-tree
• During insertion, the key always goes into a leaf. There
are three possible ways we can do this:
• 1 - If the key is already in a leaf node, and removing it
doesn’t cause that leaf node to have too few keys, then
simply remove the key to be deleted.
• 2 - If the key is not in a leaf then it is guaranteed (by the
nature of a B-tree) that its predecessor or successor will
be in a leaf -- in this case we can delete the key and
promote the predecessor or successor key to the non-
leaf deleted key’s position.
B-Trees
Removal from a B-tree (2)
3: if one of them has more than the min.
number of keys then we can promote one of
its keys to the parent and take the parent key
into our lacking leaf
if neither of them has more than the min.
number of keys then the lacking leaf and one
of its neighbours can be combined with their
shared parent (the opposite of promoting a
key) and the new leaf will have the correct
number of keys.
B-Trees
B-Tree Delete Operation
• After deletion node is at least half full.
(inverse of insertion case 1)
B-Tree Delete Operation
B-Tree Delete Operation
Underflow occurs and the keys in the left & right .
Merge the underflow node and a sibling
B-Tree Delete Operation
• Underflow occurs, height decreases after
merging.
HASHING
Hashing
• Is a means used to order and access elements
in a list quickly by using a function of the key
value to identify its location in the list.
• The function of the key value is called a hash
function.
Hashing
Idea:
– Use a function h to compute the slot for each key
– Store the element in slot h(k)
• A hash function h transforms a key into an index
in a hash table T[0…m-1]:
h : U {0, 1, . . . , m - 1}→
• We say that k hashes to slot h(k)
U
(universe of keys)
K
(actual
keys)
0
m - 1
h(k3)
h(k2) = h(k5)
h(k1)
h(k4)
k1
k4 k2
k5
k3
h : U → {0, 1, . . . , m - 1}
hash table size: m
Hashing
Advantages of Hashing
• Reduce the range of array indices handled:
m instead of |U|
where m is the hash table size: T[0, …, m-1]
• Storage is reduced.
• simplicity
Properties of Good Hash Functions
• Good hash function properties
(1) Easy to compute
(2) Approximates a random function
i.e., for every input, every output is equally likely.
(3) Minimizes the chance that similar keys hash to the
same slot
i.e., strings such as pt and pts should hash to different slot.
• We will discuss two methods:
– Division method
– Multiplication method
The Division Method
• Idea:
– Map a key k into one of the m slots by taking
the remainder of k divided by m
h(k) = k mod m
• Advantage:
– fast, requires only one operation
• Disadvantage:
– Certain values of m are bad (i.e., collisions), e.g.,
• power of 2
• non-prime numbers
The Multiplication Method
Idea:
(1) Multiply key k by a constant A, where 0 < A < 1
(2) Extract the fractional part of kA
(3) Multiply the fractional part by m
(4) Truncate the result
h(k) = = m (k A mod 1)
• Disadvantage: Slower than division method
• Advantage: Value of m is not critical
fractional part of kA = kA - kA. ., 12.3 12e g =  
Example – Multiplication Method
Suppose k=6, A=0.3, m=32
(1) k x A = 1.8
(2) fractional part:
(3) m x 0.8 = 32 x 0.8 = 25.6
(4)
1.8 1.8 0.8− =  
25.6 25=   h(6)=25
Collisions
U
(universe of keys)
K
(actual
keys)
0
m - 1
h(k3)
h(k2) = h(k5)
h(k1)
h(k4)
k1
k4 k2
k5
k3
Collisions occur when h(ki)=h(kj), i≠j
• For a given set K of keys:
– If |K| ≤ m, collisions may or may not happen,
depending on the hash function!
– If |K| > m, collisions will definitely happen
• i.e., there must be at least two keys that have the same hash
value
• Avoiding collisions completely might not be easy.
Collisions
Handling Collisions
• We will discuss main methods:
(1) Chaining
Chaining
• Idea:
– Put all elements that hash to the same slot into a
linked list
– Slot j contains a pointer to the head of the list of all
elements that hash to j
Chaining
• How to choose the size of the hash table m?
– Small enough to avoid wasting space.
– Large enough to avoid many collisions and keep
linked-lists short.
– Typically 1/5 or 1/10 of the total number of elements.
• Should we use sorted or unsorted linked lists?
– Unsorted
• Insert is fast
• Can easily remove the most recently inserted elements
Double Hashing
(1) Use one hash function to determine the first slot.
(2) Use a second hash function to determine the
increment for the probe sequence:
h(k,i) = (h1(k) + i h2(k) ) mod m, i=0,1,...
• Initial probe: h1(k)
• Second probe is offset by h2(k) mod m, so on ...
• Advantage: handles clustering better
• Disadvantage: more time consuming
• How many probe sequences can double hashing
generate?
m2
Double Hashing: Example
h1(k) = k mod 13
h2(k) = 1+ (k mod 11)
h(k,i) = (h1(k) + i h2(k) ) mod 13
• Insert key 14:
i=0: h(14,0) = h1(14) = 14 mod 13 = 1
i=1: h(14,1) = (h1(14) + h2(14)) mod 13
= (1 + 4) mod 13 = 5
i=2: h(14,2) = (h1(14) + 2 h2(14)) mod 13
= (1 + 8) mod 13 = 9
0
9
4
2
3
1
5
6
7
8
10
11
12
14

Contenu connexe

Tendances

How To Write Set Notation
How To Write Set NotationHow To Write Set Notation
How To Write Set NotationDon Sevcik
 
Interpolation search
Interpolation searchInterpolation search
Interpolation searchUsr11011
 
Law of exponent Teacher slide
Law of exponent Teacher slideLaw of exponent Teacher slide
Law of exponent Teacher slideGita Pakpahan
 
7 searching injava-binary
7 searching injava-binary7 searching injava-binary
7 searching injava-binaryirdginfo
 
Searching linear &amp; binary search
Searching linear &amp; binary searchSearching linear &amp; binary search
Searching linear &amp; binary searchnikunjandy
 
Demonstrate interpolation search
Demonstrate interpolation searchDemonstrate interpolation search
Demonstrate interpolation searchmanojmanoj218596
 
Sorting algorithms v01
Sorting algorithms v01Sorting algorithms v01
Sorting algorithms v01Dusan Vuckovic
 
Exponents and powers by arjun rastogi
Exponents and powers by arjun rastogiExponents and powers by arjun rastogi
Exponents and powers by arjun rastogiARJUN RASTOGI
 
computer notes - Stack
computer notes - Stackcomputer notes - Stack
computer notes - Stackecomputernotes
 
1 ESO - UNIT 02 - POWERS AND SQUARE ROOTS
1 ESO - UNIT 02 - POWERS AND SQUARE ROOTS 1 ESO - UNIT 02 - POWERS AND SQUARE ROOTS
1 ESO - UNIT 02 - POWERS AND SQUARE ROOTS Gogely The Great
 
Master of Computer Application (MCA) – Semester 4 MC0080
Master of Computer Application (MCA) – Semester 4  MC0080Master of Computer Application (MCA) – Semester 4  MC0080
Master of Computer Application (MCA) – Semester 4 MC0080Aravind NC
 

Tendances (19)

How To Write Set Notation
How To Write Set NotationHow To Write Set Notation
How To Write Set Notation
 
The laws of exponents
The laws of exponentsThe laws of exponents
The laws of exponents
 
Linked lists
Linked listsLinked lists
Linked lists
 
Alpha-Beta Search
Alpha-Beta SearchAlpha-Beta Search
Alpha-Beta Search
 
Interpolation search
Interpolation searchInterpolation search
Interpolation search
 
Law of exponent Teacher slide
Law of exponent Teacher slideLaw of exponent Teacher slide
Law of exponent Teacher slide
 
7 searching injava-binary
7 searching injava-binary7 searching injava-binary
7 searching injava-binary
 
Searching linear &amp; binary search
Searching linear &amp; binary searchSearching linear &amp; binary search
Searching linear &amp; binary search
 
Sorting
SortingSorting
Sorting
 
Mathematics project
Mathematics projectMathematics project
Mathematics project
 
Demonstrate interpolation search
Demonstrate interpolation searchDemonstrate interpolation search
Demonstrate interpolation search
 
Sorting algorithms v01
Sorting algorithms v01Sorting algorithms v01
Sorting algorithms v01
 
Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search Tree
 
Sets copy
Sets   copySets   copy
Sets copy
 
Exponents
ExponentsExponents
Exponents
 
Exponents and powers by arjun rastogi
Exponents and powers by arjun rastogiExponents and powers by arjun rastogi
Exponents and powers by arjun rastogi
 
computer notes - Stack
computer notes - Stackcomputer notes - Stack
computer notes - Stack
 
1 ESO - UNIT 02 - POWERS AND SQUARE ROOTS
1 ESO - UNIT 02 - POWERS AND SQUARE ROOTS 1 ESO - UNIT 02 - POWERS AND SQUARE ROOTS
1 ESO - UNIT 02 - POWERS AND SQUARE ROOTS
 
Master of Computer Application (MCA) – Semester 4 MC0080
Master of Computer Application (MCA) – Semester 4  MC0080Master of Computer Application (MCA) – Semester 4  MC0080
Master of Computer Application (MCA) – Semester 4 MC0080
 

Similaire à presentation on b tress. heap trees.hashing

data structures and algorithms Unit 3
data structures and algorithms Unit 3data structures and algorithms Unit 3
data structures and algorithms Unit 3infanciaj
 
3.5 merge sort
3.5 merge sort3.5 merge sort
3.5 merge sortKrish_ver2
 
Advanced s and s algorithm.ppt
Advanced s and s algorithm.pptAdvanced s and s algorithm.ppt
Advanced s and s algorithm.pptLegesseSamuel
 
Best for b trees
Best for b treesBest for b trees
Best for b treesDineshRaaja
 
Unit III Version I.pptx
Unit III Version I.pptxUnit III Version I.pptx
Unit III Version I.pptxssuserd602fd
 
Lecture_10 - Revised.pptx
Lecture_10 - Revised.pptxLecture_10 - Revised.pptx
Lecture_10 - Revised.pptxRedHeart11
 
Sorting algorithums > Data Structures & Algorithums
Sorting algorithums  > Data Structures & AlgorithumsSorting algorithums  > Data Structures & Algorithums
Sorting algorithums > Data Structures & AlgorithumsAin-ul-Moiz Khawaja
 
Trees in data structure
Trees in data structureTrees in data structure
Trees in data structureAnusruti Mitra
 
Binary search tree
Binary search treeBinary search tree
Binary search treeSana Yameen
 
sorting and searching.pptx
sorting and searching.pptxsorting and searching.pptx
sorting and searching.pptxParagAhir1
 
3.8 quick sort
3.8 quick sort3.8 quick sort
3.8 quick sortKrish_ver2
 
Data structures arrays
Data structures   arraysData structures   arrays
Data structures arraysmaamir farooq
 

Similaire à presentation on b tress. heap trees.hashing (20)

data structures and algorithms Unit 3
data structures and algorithms Unit 3data structures and algorithms Unit 3
data structures and algorithms Unit 3
 
3.5 merge sort
3.5 merge sort3.5 merge sort
3.5 merge sort
 
Advanced s and s algorithm.ppt
Advanced s and s algorithm.pptAdvanced s and s algorithm.ppt
Advanced s and s algorithm.ppt
 
Sorting
SortingSorting
Sorting
 
Unit III Heaps.ppt
Unit III Heaps.pptUnit III Heaps.ppt
Unit III Heaps.ppt
 
Best for b trees
Best for b treesBest for b trees
Best for b trees
 
08 B Trees
08 B Trees08 B Trees
08 B Trees
 
Sorting2
Sorting2Sorting2
Sorting2
 
Unit III Version I.pptx
Unit III Version I.pptxUnit III Version I.pptx
Unit III Version I.pptx
 
B and B+ tree
B and B+ treeB and B+ tree
B and B+ tree
 
Sorting
SortingSorting
Sorting
 
Unit vii sorting
Unit   vii sorting Unit   vii sorting
Unit vii sorting
 
Lecture_10 - Revised.pptx
Lecture_10 - Revised.pptxLecture_10 - Revised.pptx
Lecture_10 - Revised.pptx
 
Sorting algorithums > Data Structures & Algorithums
Sorting algorithums  > Data Structures & AlgorithumsSorting algorithums  > Data Structures & Algorithums
Sorting algorithums > Data Structures & Algorithums
 
Trees in data structure
Trees in data structureTrees in data structure
Trees in data structure
 
Binary search tree
Binary search treeBinary search tree
Binary search tree
 
sorting and searching.pptx
sorting and searching.pptxsorting and searching.pptx
sorting and searching.pptx
 
Sorting
SortingSorting
Sorting
 
3.8 quick sort
3.8 quick sort3.8 quick sort
3.8 quick sort
 
Data structures arrays
Data structures   arraysData structures   arrays
Data structures arrays
 

Plus de Bindiya syed

our project presentation on online trading
our project presentation on online tradingour project presentation on online trading
our project presentation on online tradingBindiya syed
 
presentation on nonverbal comunication
presentation on nonverbal comunicationpresentation on nonverbal comunication
presentation on nonverbal comunicationBindiya syed
 
presentation on software,hardware and input output devices
presentation on software,hardware and input output devicespresentation on software,hardware and input output devices
presentation on software,hardware and input output devicesBindiya syed
 
poverty in pakistan
poverty in pakistanpoverty in pakistan
poverty in pakistanBindiya syed
 

Plus de Bindiya syed (6)

Megma 3-d - copy
Megma 3-d - copyMegma 3-d - copy
Megma 3-d - copy
 
our project presentation on online trading
our project presentation on online tradingour project presentation on online trading
our project presentation on online trading
 
Presntation11
Presntation11Presntation11
Presntation11
 
presentation on nonverbal comunication
presentation on nonverbal comunicationpresentation on nonverbal comunication
presentation on nonverbal comunication
 
presentation on software,hardware and input output devices
presentation on software,hardware and input output devicespresentation on software,hardware and input output devices
presentation on software,hardware and input output devices
 
poverty in pakistan
poverty in pakistanpoverty in pakistan
poverty in pakistan
 

presentation on b tress. heap trees.hashing

  • 1. Presented to: Madam sobia kazmi Presented by:- kinza Arshad(05) Sayyida Tabinda kokab(08) Qandeel Saddique(18) Parveen Tabassum(33) Amna Aftab(34) Adin Ajaz(37)
  • 2. Presentation outlines  Merge sort  B-trees  Hashing  Heap trees
  • 3. Merge Sort  SORTING:-arranging array elements in ascending order  MERGE SORT:- A sorting technique to arrange array element in ascending order  sorting elements by merging elements  combo of sorting and merging
  • 4. Merge Sort (cont,,,) CONCEPT: 1. Divide the list in half 2. Merge sort the first half 3. Merge sort the second half 4. Merge both halves back together
  • 5. Merge Sort (cont,,,)  Merge sort is based on the divide-and-conquer paradigm.  To sort A[p .. r]: 1. Divide Step If a given array A has zero or one element, simply return; it is already sorted. Otherwise, split A[p .. r] into two subarrays A[p .. q] and A[q + 1 .. r], each containing about half of the elements of A[p .. r]. That is, q is the halfway point of A[p .. r]. 2. Conquer Step Conquer by sorting the two subarrays A[p .. q] and A[q + 1 .. r].
  • 6. Merge Sort (cont,,,) 3. Combine Step Combine the elements back in A[p .. r] by merging the two sorted subarrays A[p .. q] and A[q + 1 .. r] into a sorted sequence. To accomplish this step, we will define a procedure MERGE (A, p, q, r). Note that the recursion bottoms out when the subarray has just one element, so that it is trivially sorted.
  • 7. Merge sort ( cont,,,)  Merge sort (divide-and-conquer)  Divide array into two halves. A L G O R I T H M S divideA L G O R I T H M S
  • 8. Merge sort(cont,,,)  Mergesort (divide-and-conquer)  Divide array into two halves.  Recursively sort each half. sort A L G O R I T H M S divideA L G O R I T H M S A G L O R H I M S T
  • 9. Merge sort(cont,,,)  Mergesort (divide-and-conquer)  Divide array into two halves.  Recursively sort each half.  Merge two halves to make sorted whole. merge sort A L G O R I T H M S divideA L G O R I T H M S A G L O R H I M S T A G H I L M O R S T
  • 10. auxiliary array smallest smallest A G L O R H I M S T Merging  Merge.  Keep track of smallest element in each sorted half.  Insert smallest of two elements into auxiliary array.  Repeat until done. A
  • 11. auxiliary array smallest smallest A G L O R H I M S T A Merging  Merge.  Keep track of smallest element in each sorted half.  Insert smallest of two elements into auxiliary array.  Repeat until done. G
  • 12. auxiliary array smallest smallest A G L O R H I M S T A G Merging  Merge.  Keep track of smallest element in each sorted half.  Insert smallest of two elements into auxiliary array.  Repeat until done. H
  • 13. auxiliary array smallest smallest A G L O R H I M S T A G H Merging  Merge.  Keep track of smallest element in each sorted half.  Insert smallest of two elements into auxiliary array.  Repeat until done. I
  • 14. auxiliary array smallest smallest A G L O R H I M S T A G H I Merging  Merge.  Keep track of smallest element in each sorted half.  Insert smallest of two elements into auxiliary array.  Repeat until done. L
  • 15. auxiliary array smallest smallest A G L O R H I M S T A G H I L Merging  Merge.  Keep track of smallest element in each sorted half.  Insert smallest of two elements into auxiliary array.  Repeat until done. M
  • 16. auxiliary array smallest smallest A G L O R H I M S T A G H I L M Merging  Merge.  Keep track of smallest element in each sorted half.  Insert smallest of two elements into auxiliary array.  Repeat until done. O
  • 17. auxiliary array smallest smallest A G L O R H I M S T A G H I L M O Merging  Merge.  Keep track of smallest element in each sorted half.  Insert smallest of two elements into auxiliary array.  Repeat until done. R
  • 18. auxiliary array first half exhausted smallest A G L O R H I M S T A G H I L M O R Merging  Merge.  Keep track of smallest element in each sorted half.  Insert smallest of two elements into auxiliary array.  Repeat until done. S
  • 19. auxiliary array first half exhausted smallest A G L O R H I M S T A G H I L M O R S Merging  Merge.  Keep track of smallest element in each sorted half.  Insert smallest of two elements into auxiliary array.  Repeat until done. T
  • 20. Algorithm Merge sort Input :- An array A[l…r] where l and r are the lower and upper index of A. Output :- Array a[l…r] with all element arranged in ascending order . Steps: 1. if(r<=l) then 2. Return 3. else 4. Mid=[(l+r)/2] 5. MergeSort(A[l…mid]) 6. MergeSort(A[mid+1…r]) 7. Merge(A,L,mid,r) 8. Endif 9. Stop
  • 21. Pros:  It is a stable sort, and there is no worst-case scenario.  It is faster, the temp array holds the resulting array until both left and right sides are merged into the temp array, then the temp array is appended over the input array.  It is used in tape drives to sort data - its good with parallel processing. Cons:  The memory requirement is doubled.  Takes longer to merge because if the next element is in the right side then all of the elements must be moved down.
  • 23. Definition of a B-tree A B-tree of order m is an m-way tree (i.e., a tree where each node may have up to m children) in which:  1. the number of keys in each non-leaf node is one less than the number of its children and these keys partition the keys in the children in the fashion of a search tree  2.all leaves are on the same level  3.all non-leaf nodes except the root have at least m / 2 children  5. a leaf node contains no more than m – 1 keys The number m should always be odd B-Trees 23
  • 24. An example B-Tree B-Trees 24 51 6242 6 12 26 55 60 7064 90 45 1 2 4 7 8 13 15 18 25 27 29 46 48 53 A B-tree of order 5 containing 26 items Note that all the leaves are at the same levelNote that all the leaves are at the same level
  • 25. Constructing a B-tree Suppose we start with an empty B-tree and keys arrive in the following order:1 12 8 2 25 5 14 28 17 7 52 16 48 68 3 26 29 53 55 45 We want to construct a B-tree of order 5 The first four items go into the root: To put the fifth item in the root would violate condition 5 Therefore, when 25 arrives, pick the middle key to make a new root B-Trees 25 1 2 8 12
  • 27. Constructing a B-tree (cont,,,) B-Trees 27 1 2 8 12 25 6, 14, 28 get added to the leaf nodes: 1 2 8 12 146 25 28
  • 28. Constructing a B-tree (cont,,,) B-Trees Adding 17 to the right leaf node would over-fill it, so we take the middle key, promote it (to the root) and split the leaf 8 17 12 14 25 281 2 6 7, 52, 16, 48 get added to the leaf nodes 8 17 12 14 25 281 2 6 16 48 527
  • 29. Constructing a B-tree (cont,,,) B-Trees Adding 68 causes us to split the right most leaf, promoting 48 to the root, and adding 3 causes us to split the left most leaf, promoting 3 to the root; 26, 29, 53, 55 then go into the leaves 3 8 17 48 52 53 55 6825 26 28 291 2 6 7 12 14 16 Adding 45 causes a split of 25 26 28 29 and promoting 28 to the root then causes the root to split
  • 30. Constructing a B-tree (cont,,,) B-Trees 17 3 8 28 48 1 2 6 7 12 14 16 52 53 55 6825 26 29 45
  • 31. Inserting into a B-Tree Attempt to insert the new key into a leaf If this would result in that leaf becoming too big, split the leaf into two, promoting the middle key to the leaf’s parent If this would result in the parent becoming too big, split the parent into two, promoting the middle key This strategy might have to be repeated all the way to the top If necessary, the root is split in two and the middle key is promoted to a new root, making the tree one level higher B-Trees
  • 36. Removal from a B-tree • During insertion, the key always goes into a leaf. There are three possible ways we can do this: • 1 - If the key is already in a leaf node, and removing it doesn’t cause that leaf node to have too few keys, then simply remove the key to be deleted. • 2 - If the key is not in a leaf then it is guaranteed (by the nature of a B-tree) that its predecessor or successor will be in a leaf -- in this case we can delete the key and promote the predecessor or successor key to the non- leaf deleted key’s position. B-Trees
  • 37. Removal from a B-tree (2) 3: if one of them has more than the min. number of keys then we can promote one of its keys to the parent and take the parent key into our lacking leaf if neither of them has more than the min. number of keys then the lacking leaf and one of its neighbours can be combined with their shared parent (the opposite of promoting a key) and the new leaf will have the correct number of keys. B-Trees
  • 38. B-Tree Delete Operation • After deletion node is at least half full. (inverse of insertion case 1)
  • 40. B-Tree Delete Operation Underflow occurs and the keys in the left & right . Merge the underflow node and a sibling
  • 41. B-Tree Delete Operation • Underflow occurs, height decreases after merging.
  • 43. Hashing • Is a means used to order and access elements in a list quickly by using a function of the key value to identify its location in the list. • The function of the key value is called a hash function.
  • 44. Hashing Idea: – Use a function h to compute the slot for each key – Store the element in slot h(k) • A hash function h transforms a key into an index in a hash table T[0…m-1]: h : U {0, 1, . . . , m - 1}→ • We say that k hashes to slot h(k)
  • 45. U (universe of keys) K (actual keys) 0 m - 1 h(k3) h(k2) = h(k5) h(k1) h(k4) k1 k4 k2 k5 k3 h : U → {0, 1, . . . , m - 1} hash table size: m Hashing
  • 46. Advantages of Hashing • Reduce the range of array indices handled: m instead of |U| where m is the hash table size: T[0, …, m-1] • Storage is reduced. • simplicity
  • 47. Properties of Good Hash Functions • Good hash function properties (1) Easy to compute (2) Approximates a random function i.e., for every input, every output is equally likely. (3) Minimizes the chance that similar keys hash to the same slot i.e., strings such as pt and pts should hash to different slot. • We will discuss two methods: – Division method – Multiplication method
  • 48. The Division Method • Idea: – Map a key k into one of the m slots by taking the remainder of k divided by m h(k) = k mod m • Advantage: – fast, requires only one operation • Disadvantage: – Certain values of m are bad (i.e., collisions), e.g., • power of 2 • non-prime numbers
  • 49. The Multiplication Method Idea: (1) Multiply key k by a constant A, where 0 < A < 1 (2) Extract the fractional part of kA (3) Multiply the fractional part by m (4) Truncate the result h(k) = = m (k A mod 1) • Disadvantage: Slower than division method • Advantage: Value of m is not critical fractional part of kA = kA - kA. ., 12.3 12e g =  
  • 50. Example – Multiplication Method Suppose k=6, A=0.3, m=32 (1) k x A = 1.8 (2) fractional part: (3) m x 0.8 = 32 x 0.8 = 25.6 (4) 1.8 1.8 0.8− =   25.6 25=   h(6)=25
  • 51. Collisions U (universe of keys) K (actual keys) 0 m - 1 h(k3) h(k2) = h(k5) h(k1) h(k4) k1 k4 k2 k5 k3 Collisions occur when h(ki)=h(kj), i≠j
  • 52. • For a given set K of keys: – If |K| ≤ m, collisions may or may not happen, depending on the hash function! – If |K| > m, collisions will definitely happen • i.e., there must be at least two keys that have the same hash value • Avoiding collisions completely might not be easy. Collisions
  • 53. Handling Collisions • We will discuss main methods: (1) Chaining
  • 54. Chaining • Idea: – Put all elements that hash to the same slot into a linked list – Slot j contains a pointer to the head of the list of all elements that hash to j
  • 55. Chaining • How to choose the size of the hash table m? – Small enough to avoid wasting space. – Large enough to avoid many collisions and keep linked-lists short. – Typically 1/5 or 1/10 of the total number of elements. • Should we use sorted or unsorted linked lists? – Unsorted • Insert is fast • Can easily remove the most recently inserted elements
  • 56. Double Hashing (1) Use one hash function to determine the first slot. (2) Use a second hash function to determine the increment for the probe sequence: h(k,i) = (h1(k) + i h2(k) ) mod m, i=0,1,... • Initial probe: h1(k) • Second probe is offset by h2(k) mod m, so on ... • Advantage: handles clustering better • Disadvantage: more time consuming • How many probe sequences can double hashing generate? m2
  • 57. Double Hashing: Example h1(k) = k mod 13 h2(k) = 1+ (k mod 11) h(k,i) = (h1(k) + i h2(k) ) mod 13 • Insert key 14: i=0: h(14,0) = h1(14) = 14 mod 13 = 1 i=1: h(14,1) = (h1(14) + h2(14)) mod 13 = (1 + 4) mod 13 = 5 i=2: h(14,2) = (h1(14) + 2 h2(14)) mod 13 = (1 + 8) mod 13 = 9 0 9 4 2 3 1 5 6 7 8 10 11 12 14