Daa

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 18

SORTING IN LINEAR

TIME

Design and Analysis of Algorithms (DAA)


W H AT I S S O RTI N G I N
LINEAR TIME

Sorting is the process of arranging the


elements of an array so that they can be
placed either in ascending or descending
order.

For example :-

Consider an array A ={A1,


A2, A3, A4, ?? An }, the array is called to be
in ascending order if element of A are
arranged like A1 >A2 >A3 >A4 >A5 > ?> An.
There are some
types of Sorting
Linear time
TYPES :-

Counting Radix Bucket


sort sort sort
1:-COUNTING SORT

==> Counting sort is a linear sorting algorithm


with asymptotic complexity O(n + k). The
Counting Sort method is a fast and reliable
sorting algorithm.
For example :-
---- if the count of element 3 is 2 then, 2 is
stored in the 3rd position of count array. If
element "5" is not present in the array,
then 0 is stored in 5th position. Store
cumulative sum of the elements of the
count array.
• Counting sort is the only sorting
algorithm which performs in linear
time (for small range of elements).
CONTINUE........ There is no comparison between any
elements, so it is better than
comparison based sorting techniques.
WORKING OF COUNTING SORT

1 2 3 4
Find out the Initialize an array of Store the count of Store cumulative
maximum element length max+1 with each element at sum of the elements
(let it be max ) from all elements 0. ... their respective of the count array.
the given array. ... index in count array.
...
SHOWING HOW PERFORM
===> Radix Sort is a linear sorting
algorithm. Radix Sort's time
complexity of O(nd), where n is the
size of the array and d is the number
of digits in the largest number. It is not
an in-place sorting algorithm because
2:- RADIX SORT it requires extra space.

----FOR EXAMPLE :-

For the decimal system (the


most common system in use today)
the radix is ten, because it uses the
ten digits from 0 through 9.
CONITNUE.....

• Radix sort works on sorting based


on an individual digit or letter position. We
must start sorting from the rightmost
position and use a stable algorithm
Find the largest
element in the array, Now, go through each
i.e. max . Let X be the 1 significant place one by
number of digits in max one. ...
. ...
WORKING OF 2

RADIX SORT
Finally, sort the
Now, sort the elements
elements based on the
based on digits at tens 3
digits at hundreds
place. ...
place.
HOW DOES THE RADIX
SORT METHOD WORK?

• Radix sort is the linear sorting algorithm that is


used for integers. In Radix sort, there is digit by
digit sorting is performed that is started from
the least significant digit to the most
significant digit. The process of radix sort
works similar to the sorting of students names,
according to the alphabetical order.
SHOWING HOW
PERFORM
3:- BUCKET SORT
==> Bucket sort, or bin sort, is a sorting algorithm that
works by distributing the elements of an array into a
number of buckets. Each bucket is then sorted
individually, either using a different sorting algorithm,
or by recursively applying the bucket sorting
algorithm.
---- FOR EXAMPLE :-
The input array we consider as an
example is {426, 134, 891, 243, 620, 578, 655, 902,
319}. Since the largest elements contain three digits
the total pass in the algorithm will be
Scatter: Go over the
Set up an array of
original array, putting
initially empty 1
each object in its
"buckets".
bucket.
WORKING FOR 2

BUCKET SORT
Gather: Visit the
Sort each non-empty 3
buckets in order and
bucket. put all elements back
into the original array.
HOW DOES THE BUCKET SORT WORK?

• Bucket Sort is a sorting algorithm


that divides the unsorted array elements
into several groups called buckets. Each
bucket is then sorted by using any of the
suitable sorting algorithms or recursively
applying the same bucket algorithm.
Finally, the sorted buckets are combined
to form a final sorted array.

SHOWING HOW
PERFORM

You might also like