This document is about one of the searching and sorting techniques. I had prepare a PowerPoint Presentation document about one of the Searching technique in Data structure that is Binary Search and one of the Sorting Technique that is Quick Sort.
This PPT will not be Download. SO If u want to download this, here a PDF version is available.
URL : http://www.slideshare.net/diamodi1/quick-sort-and-binary-search-pdf-42245979
5. Quick Sort
• Basic Concept: divide and conquer
• Select a pivot and split the data into two groups: (< pivot) and (> piv
ot):
Quicksort Concept
(<pivot)
LEFT group
(> pivot)
RIGHT group
• Recursively apply Quicksort to the subgroups
6. Quick Sort
Start with all data
in an array, and
consider it unsorted
Quicksort Start
Unsorted Array
7. Quick Sort
Quicksort Step 1
Step 1, select a pivot
(it is arbitrary)
We will select the first
element, as presented in the
original algorithm by
C.A.R. Hoare in 1962.
pivot
26 33 35 29 19
12 22
8. Quick Sort
Quicksort Step 2
Step 2, start process of
dividing data into LEFT
and RIGHT groups:
The LEFT group will
have elements less than
the pivot.
The RIGHT group will have
elements greater that the pivot.
Use markers left and right
pivot
26 33 35 29 19
left
12 22
right
9. Quick Sort
Step 3,
If left element belongs
to LEFT group, then
increment left index.
Quicksort Step 3
If right index element belongs
to RIGHT, then decrement right.
Exchange when you find
elements that belong to the other
group.
pivot
26 33 35 29 19
left
12 22
right
10. Quick Sort
Step 4:
Element 33 belongs
to RIGHT group.
Element 22 belongs
to LEFT group.
Exchange the two
elements.
Quicksort Step 4
pivot
26 33 35 29 19
left
12 22
right
pivot
26 22 35 29 19
left
12 33
right
11. Quick Sort
Step 5:
Quicksort Step 5
After the exchange,
increment left marker,
decrement right marker.
pivot
26 22 35 29 19
left
left
12 33
right
12. Quick Sort
Step 6:
Element 35 belongs
to RIGHT group.
Element 12 belongs
to LEFT group.
Exchange,
increment left, and
decrement right.
Quicksort Step 6
26 22 35 29 19
left
12 33
right
pivot
26 22 12 29 19
35 33
left right
pivot
13. Quick Sort
Step 7:
Element 29 belongs
to RIGHT.
Element 19 belongs
to LEFT.
Exchange,
increment left,
decrement right.
Quicksort Step 7
26 22 12 29 19
left
pivot
35 33
right
26 22 12 19 29
left
pivot
35 33
right
14. Quick Sort
Quicksort Step 8
Step 8:
When the left and right
markers pass each other,
we are done with the
partition task.
Swap the right with pivot.
26 22 12 19 29
left
pivot
35 33
right
pivot
26
19 22 12 29
35
LEFT RIGHT
15. Quick Sort
Step 9:
Apply Quicksort
to the LEFT and
RIGHT groups,
recursively.
Quicksort Step 9
Assemble parts when done
previous pivot
26
Quicksort Quicksort
19 22 12 29
pivot
26
35 33
pivot
12 19 22 29 33 35
12 19 22 26 29 33 35
16. Quick Sort
Quicksort Efficiency
The partitioning of an array into two parts is O(n)
The number of recursive calls to Quicksort depends on how
many times we can split the array into two groups.
On average this is O (log2 n)
The overall Quicksort efficiency is O(n) = n log2n
What is the worst-case efficiency?
Compare this to the worst case for the heapsort.
17. Quick Sort
Best Case
We cut the array size in half each time
So the depth of the recursion in log2n
At each level of the recursion, all the partitions at that level do work th
at is linear in n
O(log2n) * O(n) = O(n log2n)
Hence in the average case, quicksort has time complexity O(n log2n)
What about the worst case?
18. Quick Sort
In the worst case, partitioning always divides the size n array into th
ese three parts:
- A length one part, containing the pivot itself
- A length zero part, and
- A length n-1 part, containing everything else
We don’t recur on the zero-length part
Recurring on the length n-1 part requires (in the worst case) recurrin
g to depth n-1
Worst case
19. Quick Sort
Worst case for quicksort
In the worst case, recursion may be n levels deep (for an array of
size n)
But the partitioning work done at each level is still n
O(n) * O(n) = O(n2)
So worst case for Quicksort is O(n2)
When does this happen?
- When the array is sorted to begin with!
20. Binary Search
Problem: Search
We are given a list of records.
Each record has an associated key.
Give efficient algorithm for searching for a record containing a
particular key.
Efficiency is quantified in terms of average time analysis (number of
comparisons) to retrieve an item.
21. Binary Search
Search
[ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] [ 700 ]
Number 281942902 Number 701466868 Number 233667136
Number 580625685 Number 506643548
…
Number 155778322 Number 580625685
Each record in list has an associated key.
In this example, the keys are ID numbers.
Given a particular key, how can we efficiently
retrieve the record from the list?
22. Binary Search
Binary Search
Binary search is based on the “divide-and-conquer” strategy w
hich works as follows:
Start by looking at the middle element of the array
1. If the value it holds is lower than the search element, elimi
nate the first half of the array from further consideratio
n.
2. If the value it holds is higher than the search element,
eliminate the second half of the array from further
consideration.
Repeat this process until the element is found, or until the enti
re
array has been eliminated.
23. Binary Search
Binary Search
Perhaps we can do better than O(n) in the average case?
Assume that we are give an array of records that is sorted. For
instance:
an array of records with integer keys sorted from smallest to
largest (e.g., ID numbers), or
an array of records with string keys sorted in alphabetical order
(e.g., names).
34. Binary Search
Efficiency of binary search
The maximum number of searches x necessary to find a name is the
smallest integer that satisfies the inequality 2x > 10 or x = 4.
If n represents the number of names, the maximum number of searc
hes x necessary to find a name is the smallest integer that satisfies th
e inequality 2x > n.
The maximum number of
searches is the smallest integer
greater than log n/log 2
2x > n
log (2x) > log n
x log 2>log n
35. Binary Search
Efficiency of binary search
# of names
Maximum sequential
searches necessary
Maximum binary
searches necessary
10 10 4
100 100 7
1,000 1,000 10
5,000 5,000 13
10,000 10,000 14
50,000 50,000 16
100,000 100,000 17
1,000,000 1,000,000 20
10,000,000 10,000,000 24
1,000,000,000 1,000,000,000 30
With the incredible speed of today’s computers, a binary
search becomes necessary only when the number of
names is large.