Sortheoryppt
Sortheoryppt
B SIVA JYOTHI
SORTING THEORY
The basic problem in sorting is as follows:
Given an array that contains n elements, your task is to
sort the elements in increasing order.
For example, the array
1 2 3 4 5 6 7 8
1 3 8 2 9 2 5 6
3
Introduct
ion
• Sorting is among the most basic problems in
algorithm design.
• External Sorting
When the data to be sorted can’t be accommodated in
the memory at the same time and some has to be
kept in auxiliary memory, then external sorting
methods are used.
5
Stable and Not Stable
Sorting
• If a sorting algorithm, after sorting the contents,
does not change the sequence of similar content
in which they appear, it is called stable sorting.
6
Efficiency of Sorting
Algorithm
• The complexity of a sorting algorithm measures the
running time of a function in which n number of items
are to be sorted.
7
Efficiency of Sorting
Algorithm
• Various sorting methods are analyzed in the
cases like – best case, worst case or average
case.
10
Things to
remember
• An ideal sort is an in-place sort where the
algorithm uses no additional array storage, and
hence it is possible to sort very large lists without
the need to allocate additional working storage.
12
Inversions
A useful concept when analyzing sortingalgorithms is
an inversion: a pair of elements(t[a],t[b])in the array
such that a<b and t[a]>t[b], i.e., the elements are in
the wrong order. For example, in the array
1 2 3 4 5 6 7 8
1 2 2 6 3 5 8 6
For example, in the array the inversions are (6,3), (6,5)
and (6,8). The number of inversions tells us how much
work is needed to sort the array.
O(n2) algorithms Simple algorithms for sorting an
array work in O(n2) time.
Such algorithms are short and usually consist of two
nested loops. A famous O(n2) time sorting
algorithm is bubble sort where the elements ”bubble”
in the array according to their values.
O(nlogn) algorithms
It is possible to sort an array efficiently in O(nlogn) time using algorithms
that are not limited to swapping consecutive elements. One such algorithm
is merge sort1, which is based on recursion.
Merge sort sorts the subarray t[a,b] as follows:
1. If a=b, do not do anything, because the subarray is already sorted.
2. Calculate the position of the middle element: k=b(a+b)/2c.
3. Recursively sort the subarray t[a,k].
4. Recursively sort the subarray t[k+1,b].
5. Merge the sorted subarrays t[a,k] and t[k+1,b] into a sorted subarray t[a,b].
Merge sort is an efficient algorithm, because it halves the size of the subarray at
each step. The recursion consists of O(logn) levels, and processing each level
takes O(n) time. Merging the subarrays t[a,k] and t[k+1,b] is possible in
linear time, because they are already sorted.
BUBBLE
SORT
• In bubble sort, each element is compared with its
adjacent element.
15
TIME
COMPLEXITY
6 5 1 3 8 4 7 9 2
curre piv
wa ot
ll nt
6 5 1 3 8 4 7 9 2
piv
wa curre
ot
ll nt
6 5 1 3 8 4 7 9 2
piv
curre
wa ot
nt
ll
1 5 6 3 8 4 7 9 2
piv
curre
wa ot
nt
ll
1 2 6 3 8 4 7 9 5
47
Algorithm
• Choosing a pivot
• To partition the list we first choose a pivot element
• Partitioning
• Then we partition the elements so that all those with
values less than pivot are placed on the left side and
the higher vale on the right
• Check if the current element is less than the pivot.
• If lesser replace it with the current element and move the wall
up one position
• else move the pivot element to current element and vice
versa
• Recur 4
8
• Repeat the same partitioning step unless all elements
Analysis of Quick Sort
• Best case
• The best case analysis assumes that the pivot is always
in the middle
• To simplify the math, we assume that the two sublists
are each exactly half the size of the original
T(N)=T(N/2)+T(N/2)….+1 leads to T(N)=O(nlogn)
• Average case
• T(N)=O(nlogn)
• Worst case
• When we pick minimum or maximum as pivot then we
have to go through
each and every element so
• T(N) = O(nAshim
2) Lamichhane 50
Radix Sort
1 2 3 4 5 6
A .74 .17 .26 .72 .39 .21
Bucket: Loop 1
n=6
1 2 3 4 5 6
A .74 .17 .26 .72 .39 .21
0 1 2 3 4 5
B
Bucket: Loop 2
FOR n=6, i=1
1 2 3 4 5 6
A .74 .17 .26 .72 .39 .21
B[ n A[i] ] = B[ 6X.74 ]=B[ 4.44 ]=B[4]
0 1 2 3 4 5
B
Bucket: Loop 2
FOR n=6, i=2
1 2 3 4 5
6
A .74 .17
.26 .72 .39 .21
B[ n A[i] ] = B[ 6X.17 ]=B[ 1.02 ]=B[1]
.74
0 1 2 3 4 5
B
Bucket: Loop 2
FOR n=6, i=3
1 2 3 4 5
6
A .74 .17 .26
.72 .39 .21
B[ n A[i] ] = B[ 6X.26 ]=B[ 1.56 ]=B[1]
.74
.17
0 1 2 3 4 5
B
Bucket: Loop 2
FOR n=6, i=4
1 2 3 4 5
6
A .74 .17 .26 .72
.39 .21
B[ n A[i] ] = B[ 6X.72 ]=B[ 4.32 ]=B[4]
.26 .74
.17
0 1 2 3 4 5
B
Bucket: Loop 2
FOR n=6, i=5
1 2 3 5 6
4
A .39 .21
.74 .17 .26 .72
B[ n A[i] ] = B[ 6X.39 ]=B[ 2.34 ]=B[2]
.26 .74
.17 .72
0 1 2 3 4 5
B
Bucket: Loop 2
FOR n=6, i=6
1 2 3 4 5 6
A .74 .17 .26 .72 .39 .94
.26 .74
.17 .39
.72
0 1 2 3 4 5
B
Bucket: End of Loop 2
1 2 3 4 5 6
A .74 .17 .26 .72 .39 .94
.26 .74
.39 .94
.17 .72
0 1 2 3 4 5
B
Bucket: Loop 3
Apply insertion sort
on each bucket
1 2 3 4 5 6
A .74 .17 .26 .72 .39 .94
0 1 2 3 4 5
B
Bucket
Concatenate the
buckets in order
1 2 3 4 5 6
A .74 .17 .26 .72 .39 .94
Sorted
0 1 2 3 4 5
B output
0 1 2 3 4 5
B
Example - Bucket Sort
A 1 .78 B 0 /
5 .72 4 / Distribute
6 .94 5 / Into buckets
7 .21 6 .68 /
9 .23
8 /
10 .68 9 .94 /
Example - Bucket Sort
0 /
1 .12 .17 /
3 .39 /
Sort within each
4 / bucket
5 /
6 .68 /
7 .72 .78 /
8 /
9 .94 /
Example - Bucket Sort
.12 .17 .21 .23 .26 .39 .68 .72 .78 .94 /
Analysis of Bucket Sort
Bucket-Sort(A)
• Closest pair
– Given a set of n numbers
– Find the pair of numbers that have the smallest difference
between them
– After the numbers are sorted, the closest pair of numbers
will lie next to each other somewhere in sorted order.
• Element uniqueness
– Are there any duplicates in a given a set of n items?
– The most efficient algorithm is to sort them and then do a
linear scan though them checking all adjacent pairs.
Application contd..
• Selection
– What is the kth largest item in the set?
– If the keys are placed in sorted order in an array,
the kth largest can be found in constant time by
simply looking at the kth position of the array
• Government organizations, financial institutions,
and commercial enterprises
– Organize much of this information by sorting it
• Load-balancing problem
– M identical processors and N jobs to complete
– Goal is to schedule all of the jobs on the processors
so that the time when the last job completes as early
as possible.