Unit 3
Unit 3
Searching and
III
Sorting
2
SYLLABU
S
• Searching: Search Techniques-Sequential Search/Linear Search, Variant
of Sequential Search- Sentinel Search, Binary Search, Fibonacci Search,
and Indexed Sequential Search.
• Sorting: Types of Sorting-Internal and External Sorting, General Sort
Concepts- Sort Order, Stability, Efficiency, and Number of Passes,
Comparison Based Sorting Methods-Bubble Sort, Insertion Sort, Selection Sort,
Quick Sort, Shell Sort, Non-comparison Based Sorting Methods-Radix Sort,
Counting Sort, and Bucket Sort, Comparison of All Sorting Methods and
their complexities.
• Case Studies:Use of Fibonacci search in non-uniform access memory
storage and in Optimization of Unimodal Functions. Timsort as a hybrid
stable sorting algorithm
3
WHAT IS
• Searching : SEARCHING
“ Searching is a techniques of finding an element in a given
list of
elements.”
NEED OF
SEARCHING
Why do we need searching?
• Searching is one of the core computer science algorithms.
• We know that today’s computers store a lot of information.
• To retrieve this information proficiently we need very efficient
searching
algorithms.
5
SEARCH
TECHNIQUES
• Sequential Search/Linear Search
Variants
of
Sequential
Search
Indexed
Sentinel Binary Fibonacc
Sequentia
Search i Search
l Search
Search
6
LINEAR
SEARCH
• The linear search is a sequential search, which uses
through an array, starting with the first element.
a loop to step
• It compares each element with the value being searched for, and
stops when either the value is found or the end of the array is
encountered.
• If the value being searched is not in the array, the algorithm will
unsuccessfully search to the end of the array.
• Since the array elements are stored in linear order searching the
element
in the linear order make it easy and efficient.
• The search may be successful or unsuccessfully. That is, if the required
element is found then the search is successful other wise it is
unsuccessful.
7
ADVANTAGES OF LINEAR
SEARCH
• easy to understand.
• It operates on both sorted and unsorted
list
• It does’t not require array to be in order
• Easy to implement
• Time complexity O(n)
8
DISADVANTAGES LINEAR
SEARCH
• Linear search is not efficient when list is large
• Maximum no. of comparisons are N(n
Element).
• Not suitable for large problem.
• You need to search whole list.
• Linear search is slower
9
LINEAR SEARCH
ALGORITHM
• Consider an integer type array A with size n. So list of
elements from that array are,
A[0], A[1], A[2], A[3],… … … … … … , A[n-1]
1. Declare and initialize one variable which contents the
number to be search in an array A.
( variable key is declared)
2. Start Comparing each element from array A with the
key LOOP: A[size] == key
Repeat step no 2 while A[size] key
3. if key is found, display the location of
element(index+1)
or else display message KEY NOT FOUND
4. Terminate the program successfully
10
ANALYSIS OF LINEAR
SEARCH
• Complexity of linear
search :
1.Best Case = O(1)
2.Average Case =
O(n) 3.Worst case =
O(n)
11
SENTINEL
SEARCH
• This additional entry at the end of the list is called as Sentinel.
• The speed of sequential search can be improved by storing the
key
being searched at end of the array.
• This will eliminate extra comparison inside the loop for number of
element in the array.
ALGORITHM SENTINEL 14
BINARY SEARCH
• Assume that two variables are declared, variable first and last, they
denotes beginning and ending indices of the list under consideration
respectively.
• Step 1. Algorithm compares key with middle element from
list ( A[middle] == key ), if true go to step 4 or else go to next.
• Step 2. if key < A[ middle ], search in left half of the list or else
go to step 3
• Step 3. if key > A[ middle ], search in right half of the list or go to
step 1
• Step 4. display the position of key else display message “NOT
FOUND”
17
ADVANTAGES OF BINARY
SEARCH
1. Binary search is optimal searching
algorithms
2. Excellent time efficiency
3. Suitable for large list.
4. Faster because no need to check all
element.
5. Most suitable for sorted array
6. It can be search quickly
7. Time c omplexity O(log n)
18
DISADVANTAGES BINARY
SEARCH
1. Element must be sorted
2. Need to find mid element
3. Bit more complicated to implement and
test
4. It does not support random a c c ess.
5. Key element require to compare with
middle.
19
FIBONACCI SEQUENCE/
SERIES
• It is the sequence of Fibonacci numbers denoted by F n
• such that each number is the sum of the two preceding ones, starting
from 0 and 1
• i.e. 𝐹0 = 0, 𝐹1 = 1, 𝑎𝑛𝑑
𝐹𝑛 = 𝐹𝑛−1 +
for
𝐹𝑛−2
n>1
The beginning of the sequence is
thus : 0, 1, 1, 2, 3, 5, 8, 13, …..
23
FIBONACCI SEARCH
Fibonacci Search is a comparison-based technique
that uses Fibonacci numbers to search an element in
a sorted array.
i 1 2 3 4 5 6 7 8 9 10 11
Arr[i] 10 22 35 40 45 50 80 82 85 90 100
i 1 2 3 4 5 6 7 8 9 10 11
F. S. 0 1 1 2 3 5 8 13 21 34 55
EXAMPL
i 1 2 3 4 5 6 7 8 9
E 10
Arr[i] 3 5 6 11 15 17 18 20 21 25
i 1 2 3 4 5 6 7 8 9 10
F. S. 0 1 1 2 3 5 8 13 21 34
INDEXED SEQUENTIAL
SEARCH
Mr
SORTING:
DEFINITION
Sorting = ordering.
Sorted = ordered based on a
particular way.
Sorting is any process of arranging items in some sequence
in different
and/or
sets.
Sorting: an operation that segregates items into groups
according to specified criterion.
A={3162134590}
A={0112334569}
30
SORTING :
EXAMPLES
• Words in a dictionary are sorted
INTERNAL SORTING
• Internal sorting are type of sorting which is used when the entire collection
of data is small enough that sorting can take place within main memory.
There is no need for external memory for execution of sorting program.
• It is used when size of input is small
• Examples:- Bubble sort, insertion sort, quicksort, heapsort
45 23 12 56 9
RAM
32
EXTERNAL SORTING
• External sorting is a class of sorting algorithms that can handle
massive amounts of data.
• External sorting is required when the data being sorted do not fit
into
the main memory of a computing device (usually RAM) and instead
they must reside in the slower external memory, usually a hard disk
drive.
• Example: distribution sorting, external merge sort. 76
Hard
13 Disk
45 23 12 56 9 51
49 23
RAM
33
SORT
• A sorting technique is said to be stable if keys that are equal
retain STABILITY
their relative orders of occurrence even after sorting.
Before After
Ex:
Name Grade Name Grade
Aayana C Yojana A
Ira B Zara A
Ketan B Ira B
Vivek B Ketan B
Yojana A Vivek B
Zara A Aayana C
UNSTABLE
SORTING
• Ex: Does not preserve the initial ,alphabetical order after sorting by the
grade column
Before After
Name Grade Name Grade
Aayana C Yojana A
Ira B Zara A
Ketan B Vivek B
Vivek B Ketan B
Yojana A Ira B
Zara A Aayana C
35
SORT
STABILITY
• If k1,k2 are two keys such that k1=k2 and p(k1) < p(k2)
where p(ki) is the position index of the keys in the unsorted
list .
• then after sorting p’(k1)<p’(k2) where p’(ki) is the
position index of the keys in the sorted list.
Ex:
0 1 2 3 4 5 6 7 8
45 23 14 11 78 56 14 34 76
11 14 14 23 34 45 56 76 78
Mrs. Manisha 1 2 3 4 5 6 7 8
0
36
SORT
EFFICIENCY
• In order to find the amount of time required to sort list of n elements by a
particular method we must find the number of comparisons required to
sort .
• Some methods are data sensitive so finding exact number of
comparisons become difficult, so they are analysed by various cases
like Best, Worst & Average.
• Algorithms like bubble sort, selection sort, insertion sort are not suited for
sorting a large file. these algorithms Have time requirement of O(𝑛2)where
as they hardly require any additional memory space for sorting.
• Advance sorting algorithms like Quick sort, Merge Sort and heap sort have
a timing requirement of O(n log n)but they too have some additional
problems as Complex Algo, Complex data structure, Additional memory
requirement, Unstability, data sensitivity etc.
37
NUMBER OF
PASSES
• Most of the sorting algorithms work in passes.
• In every pass a number is placed at position where it will appear in the
sorted list.
• Inside a pass numbers are compared and exchanged as required by
the algorithm.
38
SORTING METHODS
• Comparison Based Sorting Methods-
• Bubble Sort
• Insertion Sort
• Selection Sort
• Quick Sort
• Shell Sort
• Non-comparison Based Sorting
Methods-
• Radix Sort
• Counting Sort
• Bucket Sort
BUBBLE SORT:
39
BASICS
• Sorting by exchange
• Idea: bubble in water.
• comparing adjacent elements, and swapping them if
they are in the wrong order.
• The process is continued until the list is sorted.
BUBBLE SORT 40
9, 6, EXAMPLE
2, 12, 11 9, 3, 7
,
6, 9, 2, 12, 11 9, 3, 7
,
6, 2, 9, 12, 11 9, 3, 7
,
6, 2, 9, 12, 11 9, 3, 7
Mrs. Manisha
6, 2, 9, 11, ,9, 3, 7, 12
Desai
BUBBLE SORT 41
EXAMPLE
First Pass
6, 2, 9, 11, 9, 3, 7, 12
Second Pass
2, 6, 9, 11,
6, 2, 9, 11,
3,
3, 7,
3, 11,
9, 11, 7, 12
BUBBLE SORT 42
EXAMPLE
First Pass
6, 2, 9, 11, 9, 3, 7, 12
Second Pass
Third Pass
2, 6, 9, 9, 3, 7, 11, 12
2, 6, 9, 3, 7, 7,
9, 3,
9, 9, 11, 12
BUBBLE SORT 43
EXAMPLE
First Pass
6, 2, 9, 11, 9, 3, 7,
12 2, 6, 9, 9, 3, 7, 11, 12
Second Pass
Third Pass
2, 6, 9, 3, 7, 9, 11,
12
2, 6, 3,
9, 9,
3, 7, 9, 11, 12
Fourth Pass
2, 6, 3, 7, 9, 9, 11, 12
BUBBLE SORT 44
EXAMPLE
First Pass
6, 2, 9, 11, 9, 3, 7,
12 2, 6, 9, 9, 3, 7, 11, 12
Second Pass
2, 6, 9, 3, 7, 9, 11,
Third Pass
12
Fourth Pass
2,
2, 6,
6, 3,
3, 7,
7, 9,
9, 9,
9, 11,
11, 12
12
Fifth Pass
2, 3, 6, 7, 9, 9, 11, 12
BUBBLE SORT 45
EXAMPLE
First Pass
6, 2, 9, 11, 9, 3, 7,
12 2, 6, 9, 9, 3, 7, 11, 12
Second Pass
2, 6, 9, 3, 7, 9, 11,
Third Pass
12
Fourth Pass
2, 6, 3, 7, 9, 9, 11, 12
Fifth Pass
2, 3, 6, 7, 9, 9, 11, 12
Sixth
Pass 2, 3, 6, 7, 9, 9, 11, 12
46
SORT-
void BubbleSort (int List[ ] , int Size)
{
int temp; //temp variable for swapping list
elements
for (int i=0; i< size;i++) // make a pass
{
for (int j= 0; j <size-i; j++)
{
{ (List[j] > List[j+1]) // compare
if // swap if in the wrong
elements
order temp = List[j];
List[j] =
List[j+1];
}
List[j+1] =
} }
Mrs. Manisha
Desai temp;
48
EXAMPLE
ALGORITHM
Step 1 − If it is the first element, it is already
sorted. return 1;
Step 2 − Pick next element
Step 3 − Compare with all elements in the
sorted sub-list
Step 4 − Shift all the elements in the sorted sub-
list that is greater than the value to be sorted
Step 5 − Insert the value
Step 6 − Repeat until list is sorted
53
INSERTION SORT -
procedure insertionSort( A : array of items )
int holePosition
int valueToInsert
for i = 1 to length(A) inclusive do:
/* select value to be
inserted */
valueToInsert = A[i]
holePosition = i
/*locate hole position for the
element to be inserted */
while holePosition > 0 and A[holePosition-1] >
valueToInsert do: A[holePosition] = A[holePosition-
1]
holePosition = holePosition -1
end while
/* insert the number at hole
position */ A[holePosition]
= valueToInsert
end for
54
SELECTION SORT-
BASICS
• The algorithm works by selecting the smallest
unsorted item and then
• swapping it with the item in the next position to
be filled.
56
SELECTION SORT -
EXAMPLE
57
SELECTION
procedure selection
sort
56
SORT
47 65 33 12 24
element
/* check the element to be in array 12 24 65 33 56 47
minimum */ for j = i+1 to
j<n 12 24 33 65 56 47
if list[j] < list[min]
then min = j; 12 24 33 47 56 65
end
end forif
/* swap the minimum element with the current 12 24 33 47 56 65
element*/ if Min != i
then swap list[min] and list[i]
end
if end
for
end
proce
58
• The worst-ca se
runtime
59
QUICK SORT
60
QUICK SORT
7 6 10 5
EXAMPLE
9 2 1 15 7
Lets 7 6 10 5 9 2 1 15 7
consider
star en
pivot
t d
element= 7 6 10 5 9 2 1 15 7
starting
element star en
of Array t d
7 6 7 5 9 2 1 15 10
star en
t7 d
7 6 5 9 2 1 15 10
star en
t1 d
7 6 7 5 2 9 15 10
star en
t1 d
7 6 7 5 2 9 15 10
Mrs. Manisha end
Desai start
QUICK SORT
5 EXAMPLE
63
7 6 7 1 2 9 15 10
end
start
0 1 2 3 4 5 6 7 8
Loca tion of
7=5 2 6 7 5 1 7 9 15 10
Loca tion of end
9=6 start
Partition Partition
1 2
2 6 7 5 1 9 15 10
star en star en
t d t d
2 6 7 5 1 9 15 10
star en en star
t d6 d t
2 1 7 5
15 10
star en
Mrs. Manisha start
Desai t d
2 1 7 5 6 15 10
star en end
t d 6 4
2 1 7 5 6 10 s ta
15 rt
en star start
d t end
1 2 7 5 6 10
Loca tion of end
15=8 1 start 7 5 6
Loca tion of 10 star en
=7 t 7 d6
5
Loca tion of
2=1
Loca tion of end
6 5 7
1=0 start
Loca tion of
6 5
7=4
Loca tion of star en
6=3 t 6 d5
Loca tion of
5=2 end
5 6
start
Mrs. Manisha
Desai 5
65
QUICK SORT
EXAMPLE
Loca tion of 7=5
0 1 2 3 4 5 6 7 8
Loca tion of 9=6
Loca tion of 1 2 5 6 7 7 9 10 15
15=8
Loca tion of 10
=7
Loca tion of 2=1
Loca tion of 1=0
Loca tion of 7=4
Loca tion of 6=3
Loca tion of 5=2
66
EXAMPLE
2
10 5 8 9 3 6 15 12 16
10 5 8 9 3 6 15 12 16
star en
t d
10 5 8 9 3 6 15 12 16
end
start
6 5 8 9 3 10 15 12 16
Partition Partition
1 2
6 5 8 9 3 15 12 16
67
star en star end
t d3 t
6 5 8 9 15 12 16
star en end
t d 12 start
15 16
6 5 3 9 8
star en
t d 12 16
6 5 3 9 8
end
start
3 5 6 9 8
3 5 9 8
star en star en
t d t d8
3 5 9
en star end
d t 5 8 start
9
8
Quick Sort :
Example 68
QUICK SORT : 69
EXAMPLE
70
QUICK SORT -
EXAMPLE
• Finally sorted List
45 50 55 60 65 70
75 80 85
71
QUICK SORT
procedure quickSort(A,lb,
ub) if ub-lb <= 0
return
else
Loc = partitionFunc(A,lb,
ub) quickSort(A,lb,Loc-1)
quickSort(A,Loc+1,ub)
end if
end
procedure
72
• partitionFunc(A,lb, ub)
{
Pivot =A[ lb
] Start = lb;
End = ub;
While( start
< end )
{
while(a
[start]
<=
pivot )
{
s
t
a
r
t
;
73
• Stability:
QUICK SORT :ANALYSIS
• Unstable Sort
• Proof: let l={5,5,5}
• Time c omplexity
• Worst case- O(n2)
• Average case O(n log
n)
74
6. SHELL
SORT
76
SHELL SORT :
BASICS
• Proposed by Donald L shell in 1959
• Shell sort, also known as the diminishing increment sort
• Shell sort is a highly efficient sorting algorithm
• It is based on insertion sort algorithm
• Shell sort avoids large shift as insertion sort, in case of smaller value
is to the far right and need to be moved to far left.
• This algorithm first sorts widely spaced elements and then sort less
widely spaced elements.
• In shellSort, we make the array h-sorted for a large value of
h. We keep reducing the value of h until it becomes 1. An array is
said to be h-sorted if all sublists of every h’th element is sorted.
•shaLDeesatis call h as g a p
Mrs. Mani
Increment sequence
(4,2,1)
SHELL SORT: PRACTICE
80 93 EXAMPLE
60 12 42 30 68 85 10
gap=floor(9/2)
=4
80 93 60 12 42 30 68 85 10
42 93 60 12 80 30 68 85 10
42 93 60 12 80 30 68 85 10
42 30 60 12 80 93 68 85 10
pass 42 30 60 12 80 93 68 85 10
1
42 30 60 12 80 93 68 85 10
Actual
42 30 60 12 80 93 68 85 10
steps are
shown
using arrow 42 30 60 12 10 93 68 85 80
10 30 60 12 42 93 68 85 80
10 30 60 12 42 93 68 85 80
78
10 30 60 12 42 93 68 85 80
After pass 1 , 10 12 60 30 42 93 68 85 80
reca lculate
gap Gap1= 10 12 60 30 42 93 68 85 80
gap/2
=4/2 10 12 42 30 60 93 68 85 80
=2
If swapping is done, also compare previous element with gap of
Pass 2
10 12 42 30 60 93 68 85 80
2
10 12 42 30 60 93 68 85 80
Actual
steps 10 12 42 30 60 93 68 85 80
are
shown 10 12 42 30 60 85 68 93 80
using
arrow 10 12 42 30 60 85 68 93 80
79
10 12 42 30 60 85 68 93 80
10 12 42 30 60 85 68 93 80
10 12 42 30 60 85 68 93 80
Gap2=gap1/2
=2/2=1 10 12 30 42 60 85 68 93 80
10 12 30 42 60 85 68 93 80
10 12 30 42 60 85 68 93 80
Pass
3 10 12 30 42 60 85 68 93 80
10 12 30 42 60 68 85 93 80
Actual
steps 10 12 30 42 60 68 85 93 80
are
shown 10 12 30 42 60 68 85 93 80
using
10 12 30 42 60 68 85 80 93
arrow
10 12 30 42 60 68 80 85 93
80
gap=floor(16/2)=8
Gap1=gap/2 =8/2=4
Gap2=gap1/2 =4/2=2
Gap3=gap2/2
=2/2=1
24 37 46 11 85 47 33 66 22 84 95 55 14 09 76 35
81
SHELL SORT :
ALGORITHM
• Step 1 − Initialize the value of h
• Step 2 − Divide the list into smaller sub-list of
equal
interval h
• Step 3 − Sort these sub-lists using insertion sort
• Step 4 − Repeat until complete list is sorted
82
SHELL
For ( gap = n/2 ; gap >= 1 ; gap/2 ) SORT
{
For ( j = gap ; j < n ; j ++ )
{
For ( i = j – gap ; i >= 0 ; i –
gap )
{
If ( a [ i + gap ] > a
[ i ] ) Break ;
Else
swap ( a [ i + gap ] >
a[i])
}
}
}
83
COUNTING SORT
4 2 3 2 1 0 4 1 7 1
N=number of elements : 10
K=max value in given array
:7 Size of count array=
k+1
0 1 2 3 4 5 6 7
Initialize array 0 0 0 0 0 0 0 0
count
C
o 0 1 2 3 4 5 6 7
u 1 3 2 1 2 0 0 1
n
t
o
f 0 1 2 3 4 5 6 7
coun e 1 4 6 7 9 9 9 10
t a
86
COUNTING SORT
Find the index of each element of the original array in the count array.
This gives the cumulative count. Place the element at the index
calculated as shown in figure below.
4 2 3 2 1 0 4 1 7 1
0 1 2 3 4 5 6 7
coun 1 4 6 7 9 9 9 10
t 1- 4- 9- 10-1=9
1=0 1=3 1=8
3-
0 11=2 2 3 4 5 6 7 8 9
Sorted 0 1 1 4 7
Array
87
Countsort ( a , n , k )
{ int count [ k + 1 ], b [ n
COUNTING SORT
];
For ( i = 0 ; i < n ; i + //for counting occurrence of each
+) number
{
+ + C ount [ a [ i ] ]
; //for storing cumulative count to get
}
{ location
For (Ciount
= 1;[ ii < k ount
]=C ; i +[ i ] + C ount [ i – 1 ] ;
} )
+
For ( i = n - 1; i >= 0 ; i - -) // store number at its location by maintaining
stability
{
B [ - - count [ a [ i ] ] ] = a [ i ] ;
For
} (i = 0 ; i < n ;i + //for copying sorted number in original
+) array
{
a [ i ] = b [i ] ;
} }
TIME
COMPLEXITY 88
Time complexity = n + k + n + n
=3 n + k // lets assume k as constant
=O(n)
Drawback
• K value should be feasible
i.e. K=O(n) – K should be order of
n Ex: a=100 & K=10000 not
feasible
• Count sorting can not be directly
apply on negative values or floating
point
values. You can apply by normalizing
the data
EX: (-4, 5, 3, 2, 1, 3, 5, -4 ) =after
normalizing = ( 0, 9, 7, 2, 5, 7, 9,
0)
89
8. RADIX
SORT
90
RADIX SORT :
BASICS
• Sorting by distribution…..
• Radix Sort is a non-comparative sorting algorithm
with asymptotic complexity O(nd).
• Radix sort was developed to sort large integers.
• As integer is treated as a string of digits so we can
also call it as string sorting algorithm.
91
RADIX SORT
RADIX SORT
EXAMPLE
• Let L={387, 690, 234, 435, 567, 123, 441}
• no. of elements n=7…..(keys)
• no. of digits d=3… (passes)
• Radix r=10… ..(bins)
• It means sort would require 10 bins and 3
passes to complete the sort
94
0 1 2 3 4 5 6 7 8 9
Coun 2 0 2 1 0 3 0 1 0 0
t
0 1 2 3 4 5 6 7 8 9
Coun 2 2 4 5 5 8 8 9 9 9
t
96
0 1 2 3 4 5 6 7 8 9
Coun 2 2 4 5 5 8 8 9 9 9
t 4- 5- 9-
2- 8-
1=1 1=3 1=4 1=7 1=8
1- 3- 7-
1=0 1=2 1=6
0 1 2 3 4 6- 5 6 7 8
1=5
B 710 340 812 582 493 715 195 385 437
97
0 1 2 3 4 5 6 7 8 9
Coun 0 0 0 0 0 0 0 0 0 0
t
0 1 2 3 4 5 6 7 8 9
Coun 0 3 0 1 1 0 0 0 2 2
t
0 1 2 3 4 5 6 7 8 9
Coun 0 3 3 4 5 5 5 5 7 9
t
98
0 1 2 3 4 5 6 7 8 9
Coun 0 3 3 4 5 5 5 5 7 9
t 3- 4- 5- 7- 9-
1=2 1=3 1=4 1=6 1=8
2- 6- 8-
1=1 1=5 1=7
0 1- 1 2 3 4 5 6 7 8
1=0
B 710 812 715 437 340 582 385 493 195
99
0 1 2 3 4 5 6 7 8 9
Coun 0 0 0 0 0 0 0 0 0 0
t
0 1 2 3 4 5 6 7 8 9
Coun 0 1 0 2 2 1 0 2 1 0
t
0 1 2 3 4 5 6 7 8 9
Coun 0 1 1 3 5 6 6 8 9 9
t
100
0 1 2 3 4 5 6 7 8 9
Coun 0 1 1 3 5 6 6 8 9 9
t 1- 5- 6- 8- 9-
3-
1=0 1=2 1=4 1=5 1=7 1=8
2- 4- 7-
1=1 1=3 1=6
0 1 2 3 4 5 6 7 8
B 195 340 385 437 493 582 710 715 812
RADIX SORT 101
For ( i = 0 ; i < n ; i + + )
{
Radix sort ( a , n ) + + Count [ ( a [ i ] / pos ) % 10 ] ;
{ }
For ( i = 1; i <= k ; i + + )
Int max = getmax ( A , n ) {
For ( pos = 1 ; max / pos > 0 ; pos * Count [ i ] = Count [ i ] + Count [ i –
10 ) 1];
{ }
countSort ( a , n , pos ) For ( i = n - 1; i >= 0 ; i - -)
{
}
B [ - - count [( a [ i ] / pos ) % 10 ] ] = a
} [i];
}
For ( i = 0 ; i < n ; i + + )
{
} a [i]=b[i];
}
102
7. BUCKET
SORT
• Bucket sort (bin sort) is a stable sorting algorithm
based on partitioning the input array into several
parts called buckets .
• using some other sorting algorithm for the actual
sorting of these sub-problems.
104
EXAMPL
E
• A={34,2,13,89,44,23,108,34,321,111}
034 002 013 089 044 023 108 034 321 111
03
32 01
0 00 4 0 0 0 10 08
1 3
2 04 8 9
11 02
4
1 3
0 1 2 3 034 5 6 7 8 9
4
Merged 321 111 002 013 023 034 044 034 108 089
Mrs. Manisha
List Desai
105
EXAMPL
E
321 111 002 013 023 034 044 034 108 089
00 11 32 03
04 0 0 0 08 0
108
2 013
1 023
1 034
4
4 9
0 1 2 3 4 5 6 7 8 9
Merged 002 108 111 013 321 023 034 034 044 089
Mrs. Manisha
List Desai
106
EXAMPL
E
002 108 111 013 321 023 034 034 044 089
002 10
013,023
034,034 8 0 32 0 0 0 0 0 0
044,089 11 1
1
0 1 2 3 4 5 6 7 8 9
Merged 002 013 023 034 034 044 089 108 111 321
List Mrs. Manisha
Desai
ALGORITH
BUCKET SORT (A)
/*Numbers to be sorted are in array a[] and thee are N M107
numbers*/
1. Large=find the largest number in a[ ]
2. Passes=Number of digits in large
3. Div=1 /* divisor for extracting ith least significant
digit */
4. For(i=1; i<= passes; i++)
{
initialize all buckets b0 to b9
for each number x from a[0] to a[N-1]
{
bucket_no=(x/pos)%10
insert x in bucket with bucket_number
}
Copy elements of buckets back in
}
array a[ ] Pos=pos*10
Mrs. Manisha
5.Exi Desai
108
TIME COMPLEXITY
• O(d*(n+b))
• d: number of digits in maximum number
• N= total number of elements
• b= base of number system/ in case of alphabets , we have 26 letters so
26 buckets are required.
Mrs. Manisha
Desai
109
Mrs.
110
CASE STUDIES
• Use of Fibonacci search in non-uniform access memory storage and
in Optimization of Unimodal Functions. Timesort as a hybrid stable
sorting algorithm
Mrs. Manisha
Desai