0% found this document useful (0 votes)
22 views109 pages

Unit 3

Uploaded by

hetavimodi2005
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views109 pages

Unit 3

Uploaded by

hetavimodi2005
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 109

UNIT

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.”

List of element could be represented using an


1. Array
2. Linked list
3. Binary tree
4. B-tree
5. Heap
4

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

C++ PROGRAM FOR


#include LINEAR
<iostream.h>
Using namespace SEARCH
std;
12

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

• Algorithm SentinelSearch (list, last, target,


1.location)
Set list[last+1] to target
2. Set looker to 0
3. Loop(target not equal
list[looker])
index 0 1 2 3 4 5
1. Increment looker
4. End loop list 4 8 3 11 18 12
5. If(looker<=last)
1. Set found to true Last =5,
2. Set location to looker target=3
6. Else
1. Set found to false
2. Set location to last
7. End if
8.
End Return found
SentinelSearch
BINARY SEARCH 15

• Binary search is an searching algorithm which is used to find element


from
the sorted list”
• Concepts :
- Algorithm can be applied only on sorted data
- Position of mid element calculated using Mid = (lower + upper)/2
- Given element is compared with middle element of
the list.
- If key=mid then element is found
- Otherwise list divide into two part.(key <mid) (>mid)
- First to mid-1 or mid+1 to last.
- If given element is less than middle element then
• continue searching in first part (first to mid-1)
• otherwise searching is second part(mid+1 to last).
16

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

LINEAR SEARCH VS BINARY


Linear Search
SEARCH Binary Search
Element is searched by scanning the First list is divided into two sub-
entire list from first element to the lists. Then middle element is
Last compared with key element and
then accordingly left or right sub-
list is searched
Many times entire list is search Only sub-list is search
Simple to implementation Complex to implement, since
it involves computation for
finding the middle element
Time complexity is O(n) Time complexity is O(log n)
Less efficient sort More efficient sort
Mrs.
Manish
2
1
22

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.

Similarities with Binary Search:


1.Works for sorted arrays
2.A Divide and Conquer Algorithm.
3.Has Log n time complexity.

Differences with Binary Search:


4.Fibonacci Search divides given array in unequal parts
5.Binary Search uses division operator to divide range. Fibonacci Search doesn’t use /,
but uses + and -. The division operator may be costly on some CPUs.
6.Fibonacci Search examines relatively closer elements in subsequent steps. So when
input array is big that cannot fit in CPU cache or even in RAM, Fibonacci Search can be
useful.
ALGORITHM 24

Let arr[0..n-1] be the input array and element to be searched be x.


Step 1:Find the smallest Fibonacci Number greater than or equal to n. Let
this number be fibM [m’th Fibonacci Number]. Let the two Fibonacci
numbers preceding it be fibMm1 [(m-1)’th Fibonacci Number] and
fibMm2 [(m-2)’th Fibonacci Number].
Step 2: While the array has elements to be inspected:
1. Compare x with the last element of the range covered by fibMm2
2. If x matches, return index
3. Else If x is less than the element, move the three Fibonacci
variables two Fibonacci down, indicating elimination of
approximately rear two-third of the remaining array.
4. Else x is greater than the element, move the three Fibonacci
variables one Fibonacci down. Reset offset to index. Together
these indicate elimination of approximately front one-third of the
remaining array.
Step 3: Since there might be a single element remaining for
EXAMPLE 25

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

N= 11 and key to be searched is 85.


Smallest Fibonacci number greater than or equal to 11 is 13. As per our illustration,
fibMm2
= 5, fibMm1 = 8, and fibM = 13.
fibMm2 fibM fibM Offset i=min(offset+fibln, Arr[i] Consequence
m N)
1
5 8 13 0 5 45 Move one down, reset offset to i
3 5 8 5 8 82 Move one down, reset offset to i
2 3 5 8 10 90 Move Two Down
1 1 2 8 9 85 Return i
26

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

N= 10 and key to be searched is 20.


Smallest Fibonacci number greater than or equal to 10 is 13. As per our illustration,
fibMm2
= 5, fibMm1 = 8, and fibM = 13.
fibMm2 fibM fibM Offset i=min(offset+fibl Arr[i] Consequence
m n, N)
1
5 8 13 0 5 15 Move one down, reset offset to i
3 5 8 5 8 20 Return i
27

INDEXED SEQUENTIAL SEARCH


In this searching method, first of all, an index file is created, that
contains some specific group or division of required record when
the index is obtained, then the partial indexing takes less time
cause it is located in a specified group.
Characteristics of Indexed Sequential Search:
• In Indexed Sequential Search a sorted index is set aside in
addition to the array.
• Each element in the index points to a block of elements in
the array or another expanded index.
• The index is searched 1st then the array and guides the
search in the array.
28

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

• Files in a directory are often listed in sorted


order.

• The index of a book is sorted


31

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

It preserves the initial alphabetical order after sorting by


grade column
34

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

BUBBLE SORT: PRACTICE


EXAMPLE
• L={ 89 45 68 90 29 34
17}
BUBBLE 47

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

INSERTION SORT : BASICS


• It is the simplest of all sorting algorithms.
• Insertion sort keeps making the left side of the array
sorted until the whole array is sorted.
• An example of an insertion sort occurs in everyday
life while playing cards.
49

INSERTION SORT :EXAMPLE


• Let L = {34 8 64 51 32 21}
• [34] 8 64 51 32 21
• The algorithm sees that 8 is smaller than 34 so it
inserts.
• [8 34] 64 51 32 21
• Compare 64 with sublist and insert at proper
position.
• [8 34 64] 51 32 21
50

INSERTION SORT: EXAMPLE


• [8 34 51 64] 32 21
• The algorithm sees 32 as another smaller number and
inserts it to its appropriate location between
8 and 34.
• [8 32 34 51 64 ] 21
• The algorithm sees 21 as another smaller number
and
inserts between 8 and 32.
• Final sorted numbers:
• 8 2132 34 51 64
INSERTION SORT: PRACTICE 51

EXAMPLE

• L= { 37, 32, 70, 15, 93, 40, 63, 3, 40,


63}
52

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

INSERTION SORT :ANALYSIS

• Best case: O(n). It occurs when the data is in


sorted
order.
• Worst case: O(n2) if the numbers were sorted
in reverse order.
• It is Stable, as it does not change the relative
order
of elements with equal keys
55

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

list : array of Find 56 47 65 33 12 24


foritems
i = 0n to: i<n
size- 1 //number of min
/* set
of
passes list current element as swap min 12 47 65 33 56 24
minimum*/min = i with 1
st

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

SELECTION SORT -ANALYSIS

• 29, 64, 73, 34,


20,
20, 64, 73, 34,
29,
20, 29, 73, 34,
64
20, 29, 34, 73,
64
20, 29, 34, 64,
73

• The worst-ca se
runtime
59

QUICK SORT
60

QUICK SORT

• Invented by C.A.R. (Tony) Hoare


• A divide and conquer approach
that uses recursion
1. If the list has 0 or 1 elements it is sorted
2. otherwise, pick any element p in the list.
This is called the pivot value
3. Partition the list minus the pivot into two sub lists
according to values less than or greater than
the pivot. (equal values go to either)
4. return the quicksort of the first list followed by
the
quicksort of the second list
61

QUICK SORT: BASICS


• Pick an element, say P(the pivot)
• Re-arrange the elements into 3 sub-blocks,
1. those less than or equal to (≤) P (the Left block S1)
2. P(the only element in the middle-block)
3. those greater than or equal to (≥) P(the right block S2)
• Repeat the process recursively for the left-and right-
sub- blocks.
• Return {quicksort(S1), P, quicksort(S2)}
QUICK SORT 62

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

CHOOSING THE PIVOT IS AN ESSENTIAL


STEP.
1. Some fixed element: e.g. the first, the last,
the one in the middle
2. Randomly chosen (by random generator )
- still a bad choice.
3. The median of the array (if the array has
N numbers, the median is the [N/2]
largest number. This is difficult to
c ompute - increases the complexity.
4. The median-of-three choice: take the first,
the last and the middle element.
Choose the median of these three
elements.
75

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

For an incremental sequence


{ h3, h2, h1,h0} = SHELL SORT:
{8,4,2,1}
24 37 46 11 85 47 33 66
EXAMPLE
22 84 95 55 14 09 76 35

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

NON-COMPARISON BASED SORTING


METHODS
• Radix Sort
• Counting
Sort
• Bucket Sort
84

COUNTING SORT

• based on keys between a specific range


• It works by counting the number of objects having distinct key values
• Then doing some arithmetic to calculate the position of each object in
the output sequence
85

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 processing the digits either


by
• Least Significant Digit(LSD) method or
by
• Most Significant Digit(MSD) method.
92

RADIX SORT: BASICS


• Basic Assumption: Keys are sequences of digits in a fixed
range 0,...,radix-1 , all of equal length d

Examples of such keys


• 4 digit hexadecimal numbers
• R=16,d=4
• 5 digit decimal numbers
• R=10,d=5
• Fixed length ASCII character sequences
• R=128
• Fixed length byte sequences
• R=256
93

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

RADIX SORT : PRACTICE


L={ 4 812 715
EXAMPLE
710 195 437 582 340
93 385} 1
0 2 3 4 5 6 7 8
493 812 715 710 195 437 582 340 385

Number of passes= number of digits in max element of input array.

In pass 1, counting Sort will be applied on unit’s place of every


number. And possible numbers are ranging from 0-9 so count array
will have size 10 (index from 0 to 9)

In pass 2, counting Sort will be applied on ten’s place of every


number of
array which is generated in pass 1.

In pass 3, counting Sort will be applied on thousand’s place of


every number of array which is generated in pass 2.
95

PASS 1: RADIX SORT


0 1 2
EXAMPLE
3 4 5 6 7 8
Input Array 493 812 715 710 195 437 582 340 385
:
A
0 1 2 3 4 5 6 7 8 9
0 0 0 0 0 0 0 0 0 0
Count

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

PASS 1: RADIX SORT


0 1 2 EXAMPLE
3 4 5 6 7 8
A 493 812 715 710 195 437 582 340 385

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

PASS 2: RADIX SORT


0 1 2
EXAMPLE
3 4 5 6 7 8

A 710 340 812 582 493 715 195 385 437

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

PASS 2: RADIX SORT


0 1 2 EXAMPLE
3 4 5 6 7 8
A 710 340 812 582 493 715 195 385 437

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

PASS 3: RADIX SORT


0 1 2
EXAMPLE
3 4 5 6 7 8

A 710 812 715 437 340 582 385 493 195

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

PASS 3: RADIX SORT


0 1 2
EXAMPLE
3 4 5 6 7 8
A 710 812 715 437 340 582 385 493 195

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

Countsort ( int a[] , int n , int pos )


{ int count [ 10 ] = { 0 } , b [ n ] ;

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

RADIX SORT: ANALYSIS


• Algorithm
Worst Case Time complexity: O (nd)
Average Case Time complexity:
O(nd) Best Case Time complexity:
O(nd)
Where d is number of digit in max
element = number of passes

Space Complexity: O(n+k)


Stable: Yes
103

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

Pass1 based on units


digit

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

Pass 2 based on tens


digit

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

Pass 3 based on hundreds


digit

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

You might also like