Upload
karan-roy
View
227
Download
0
Embed Size (px)
Citation preview
8/13/2019 Sorting in data structure
1/79
SORTING
Sorting is the process of arranging the elements insome logical order.
Sorting are classified into following categories:
External sorting:
deals with sorting of the data stored in data files.This method is used when the volume of data isvery large and cannot be held in computer mainmemory.
Internal sorting:deals with sorting the data held in memory of thecomputer
8/13/2019 Sorting in data structure
2/79
SORTING METHODS
1. Bubble sort
2. Selection sort
3. Insertion sort
4. Bucket sort5. Merge sort
6. Quick sort
7. Heap sort8. Tree sort
9. Shell sort
8/13/2019 Sorting in data structure
3/79
BUBBLE SORT
It requires n-1 passes to sort an array.
In each pass every element a[i] is compared witha[i+1], for i=0 to (n-k), where k is the passnumber and if they are out of order i. e. if
a[i]>a[i+1], they are swapped. This will cause the largest element move up or
bubble up.
Thus after the end of the first pass the largest
element in the array will be placed in the nthposition and on each successive pass, the nextlargest element is placed at position (n-1),(n-2).,2 respectively
8/13/2019 Sorting in data structure
4/79
BUBBLE SORT
Pass1.
Step1. if a[0]>a[1] then swap a[0] and a[1].
Step2. if a[1]>a[2] then swap a[1] and a[2].
Stepn-1. if a[n-2]>a[n-1] then swap a[n-2] and a[n-1].
Pass2.
Step1. if a[0]>a[1] then swap a[0] and a[1].
Step2. if a[1]>a[2] then swap a[1] and a[2].
Stepn-2. if a[n-3]>a[n-2] then swap a[n-3] and a[n-2].
8/13/2019 Sorting in data structure
5/79
BUBBLE SORT
.
.
Pass k.
Step1. if a[0]>a[1] then swap a[0] and a[1].Step2. if a[1]>a[2] then swap a[1] and a[2].
Step n-k. if a[n-k+1]>a[n-k] then swap a[n-k+1] and
a[n-k].Pass n-1
Step 1 if a[0]>a[1] then swap a[0] and a[1].
8/13/2019 Sorting in data structure
6/79
BUBBLE SORT
Example: 12 40 3 2 15
12
40
3
2
15
1240
3
215
123
40
215
123
2
4015
12
3
2
15
40
Given
array
Pass 1
8/13/2019 Sorting in data structure
7/79
BUBBLE SORT
3
12
2
15
40
3
2
12
15
40
3
2
12
15
40
Pass 2
8/13/2019 Sorting in data structure
8/79
BUBBLE SORT
Pass 3 pass 4
2
3
12
15
40
2
3
12
15
40
2
3
12
15
40
8/13/2019 Sorting in data structure
9/79
ALGORITHM
Bubblesort(a,n)
for k=1 to (n-1) by 1 do
for j=0 to (n-k-1) by 1 do
if(a[j]>a[j+1]) then
set temp=[j]
set a[j]=a[j+1]
set a[j]=temp
endif
endfor
Endfor
end
8/13/2019 Sorting in data structure
10/79
ANALYSIS OF BUBBLE SORT
First pass require n-1 comparison
Second pass requires n-2 comparison
Kth pass requires n-k comparisons
Last pass requires only one comparison
Therefore total comparisons are:
F(n)=(n-1)+(n-2)++(n-k)+3+2+1
=n(n-1)/2
=O(n2)
8/13/2019 Sorting in data structure
11/79
SELECTION SORT
The selection sort also requires (n-1) passes
to sort an array.
In the first pass, find the smallest element
from elements a[0], a[1], a[2],.., a[n-1]
and swap with the first element, i.e. a[0].
In the second pass, find the smallest
element from elements a[1], a[2], a[3]..
a[n-1] and swap with a[1] and so on.
8/13/2019 Sorting in data structure
12/79
SELECTION SORT
Pass1.
Find the location loc of the smallest element in the entire array, i.e.a[0],[1],a[2]a[n-1]
Interchange a[0] & a[loc]. Then a[0] is trivially sorted.
Pass2.
Find the location loc of the smallest element in the entire array, i.e.a[1],a[2]a[n-1]
Interchange a[1] & a[loc]. Then a[0], a[1] are sorted.
Passk.
Find the location loc of the smallest element in the entire array, i.e.a[k],a[k+1],a[k+2]a[n-1]
Interchange a[k] & a[loc]. Then a[0],a[1],a[2],a[k] are sorted.Passn-1.
Find the location loc of the smaller of the element a[n-2],a[n-1]
Interchange a[n-2] & a[loc]. Then elements a[0],a[1],a[2].a[n-1].
8/13/2019 Sorting in data structure
13/79
EXAMPLE
Given array: 20 35 40 100 3 10 15
a[0] a[1] a[2] a[3] a[4] a[5] a[6]
20 35 40 100 3 10 15
Pass 1:
a[0] a[1] a[2] a[3] a[4] a[5] a[6]
20 35 40 100 3 10 15
Loc=4
Interchange elements a[0] & a[4] i.e. 20 and 3
8/13/2019 Sorting in data structure
14/79
SELECTION SORT
a[0] a[1] a[2] a[3] a[4] a[5] a[6]
3 35 40 100 20 10 15
a[0] a[1] a[2] a[3] a[4] a[5] a[6]
3 10 40 100 20 35 15
Pass 2Loc=5
Interchange elements a[1] & a[5] i.e. 35 and 10
a[0] a[1] a[2] a[3] a[4] a[5] a[6]
3 10 15 100 20 35 15
Pass3
Pass 4
Interchange elements a[2] & a[6] i.e. 40 and 15
Interchange elements a[3] & a[4] i.e. 100 and 20
Loc=6
Loc=4
8/13/2019 Sorting in data structure
15/79
SELECTION SORT
a[0] a[1] a[2] a[3] a[4] a[5] a[6]3 10 15 20 100 35 40
a[0] a[1] a[2] a[3] a[4] a[5] a[6]
3 10 15 20 35 100 40
a[0] a[1] a[2] a[3] a[4] a[5] a[6]
3 10 15 20 35 40 100
Pass 5
Pass 6
Loc=5
Loc=6
Interchange elements a[4] & a[5] i.e. 100 and 35
Interchange elements a[5] & a[6] i.e. 100 and 40
8/13/2019 Sorting in data structure
16/79
ALGORITHM
Smallestelement(a,n,k,loc)
Here a is linear array of size n. this sub algorithm finds the location locof smallest element among a[k-1],a[k+1],a[k+2]a[n-1]. Temporaryvariable small is used to hold the current smllest element nd j isused loop control variable.
Begin
set small=a[k-1]
set loc=k-1
for j=k to (n-1) by 1 do
if(a[j]
8/13/2019 Sorting in data structure
17/79
ALGORITHM
Selectionsort(a,n)
Here a is the linear array with n elements in memory. This algorithmsorts elements into ascending order. It uses a temporary variabletemp to facilitate the exchange of two values and variable I is usedloop control variable
Begin
for i=1 to (n-1) by 1 do
call smllest element(a,n,I,loc)
set temp=a[i-1]
set a[i-1]=a[loc]
set a[loc]=temp
endforend
8/13/2019 Sorting in data structure
18/79
ANALYSIS OF SELECTION SORT
First pass require n-1 comparison to find thelocation loc of smallest element
Second pass requires n-2 comparison
Kth pass requires n-k comparisons Last pass requires only one comparison
Therefore total comparisons are:
F(n)=(n-1)+(n-2)++(n-k)+3+2+1
=n(n-1)/2
=O(n2)
8/13/2019 Sorting in data structure
19/79
INSERTION SORT
This algorithm is very popular with bridge
players when they sort their cards. In this
procedure, we pick up a particular value
and then insert it at the appropriate placein the sorted sub list.
This algorithm also requires n-1 passes
8/13/2019 Sorting in data structure
20/79
INSERTION SORT
Pass1: a[1] is inserted either before or after a[0] sothat a[0] and a[1] are sorted.
Pass2: a[2] is inserted either before a[0] or betweena[0] and a[1] or after a[1] so that the elements a[0], a[1],a[2] are sorted.
Pass3: a[3] is inserted either before a[0] or betweena[0] and a[1] or between a[1] and a[2] or after a[2] sothat the elements a[0], a[1], a[2], a[3] are sorted.
Passk: a[k] is inserted in proper place in the sortedsub array a[0], a[1], a[2],a[k-1] so that the elementsa[0], a[1], a[2],a[k-1],a[k] are sorted.
Passn-1: a[n-1] is inserted in proper place in the sortedsub array a[0], a[1], a[2],a[n-2] so that the elementsa[0], a[1], a[2],a[n-1] are sorted.
8/13/2019 Sorting in data structure
21/79
EXAMPLE
Given array: 35 20 40 100 3 10 15
a[0] a[1] a[2] a[3] a[4] a[5] a[6]
35 20 40 100 3 10 15
Pass 1:
a[0] a[1] a[2] a[3] a[4] a[5] a[6]
35 20 40 100 3 10 15
Since a[1]< a[0] insert element a[1] before a[0]
8/13/2019 Sorting in data structure
22/79
SELECTION SORT
a[0] a[1] a[2] a[3] a[4] a[5] a[6]
20 35 40 100 3 10 15
a[0] a[1] a[2] a[3] a[4] a[5] a[6]
20 35 40 100 3 10 15
Pass 2
Since a[2]>a[1] no action is performed
a[0] a[1] a[2] a[3] a[4] a[5] a[6]
20 35 40 100 3 10 15
Pass3
Pass 4
Since a[3]>a[2] no action is performed
Since a[4] is less than a[3], a[2], a[1] as well as a[0] thereforeinsert a[4] before a[0]
8/13/2019 Sorting in data structure
23/79
SELECTION SORT
a[0] a[1] a[2] a[3] a[4] a[5] a[6]3 20 35 40 100 10 15
a[0] a[1] a[2] a[3] a[4] a[5] a[6]
3 10 20 35 40 100 15
a[0] a[1] a[2] a[3] a[4] a[5] a[6]
3 10 15 20 35 40 100
Pass 5
Pass 6
Since a[5] is less than a[4], a[3], a[2] as well as a[1]
therefore insert a[5] before a[1]
Since a[6] is less than a[5], a[4], a[3] as well as a[2] therefore
insert a[6] before a[2]
8/13/2019 Sorting in data structure
24/79
ALGORITHM
insertionsort(a,n)
Here a is the linear array with n elements in memory. This algorithm sortselements into ascending order. It uses a temporary variable temp tofacilitate the exchange of two values and variable j and k are used loopcontrol variables.
Begin
for k=1 to (n-1) by 1 doset temp=a[k]
set a[j]=k-1
while((temp=0) do
set a[j+1]=a[j]
set j=j-1
endwhileset a[j+1]=temp
endfor
end
8/13/2019 Sorting in data structure
25/79
ANALYSIS OF INSERTION SORT
The worst case performance occurs when the elementsof the input array are in descending order
First pass require 1 comparison to find the location loc ofsmallest element
Second pass requires 2 comparison Kth pass requires k-1 comparisons
Last pass requires (n-1) comparison
Therefore total comparisons are:
F(n)=1+2+3+..+(n-k)+.+(n-3)+(n-2)+(n-1)=n(n-1)/2
=O(n2)
8/13/2019 Sorting in data structure
26/79
Bucket/Radix sort
This is used by most of the people when sorting a list ofnames in alphabetical order. The procedure is:
First, the names are grouped according to the first letter,thus the names are arranged in 26 classes, one for each
letter of alphabet. first class consists of those names thatbegin with letter A, the second class consists of thosenames that begins with letter B, and so on.
Next, the names are grouped according to the secondletter. After this step, the list of name will be sorted on
first two letter. This process is continued for number of times depending
on the length of the names with maximum letters.
B k t/R di t
8/13/2019 Sorting in data structure
27/79
Bucket/Radix sort Since there are 26 letter of alphabet, we make
use of 26 buckets, one for each letter of thealphabet.
After grouping these names according to their
specific letter, we collect them according to order
of bucket. This new list becomes input for the next pass i.e
to separate them on the next letter from left.
To sort decimal number where base (radix) is10, we need 10 buckets are numbered from 0-9.
Unlike sorting names, decimal numbers are
sorted from right to left i.e. first on unit digits,
then on ten digit and so on.
example
8/13/2019 Sorting in data structure
28/79
example321, 150, 235, 65, 573, 789, 928, 542
321
150
235
65
573
789
928
542
65150 321 542 573 235 928 789
0 1 2 3 4 5 6 7 8 9Input
Pass 1
8/13/2019 Sorting in data structure
29/79
150
321
542
573
235
65
928
789
928
321 235 542 150 65 573 789
0 1 2 3 4 5 6 7 8 9Input
Pass 2
8/13/2019 Sorting in data structure
30/79
321
928
235
542
150
65
573
789
573
65 150 235 321 542 789 928
0 1 2 3 4 5 6 7 8 9Input
Pass 3
8/13/2019 Sorting in data structure
31/79
Bucket/Radix sort
After pass three, when the numbers are
collected, they re in following order
65, 150, 235, 321, 542, 573, 789, 928 thus
the numbers are sorted
Algorithm
8/13/2019 Sorting in data structure
32/79
AlgorithmBucketsort(a,n)
Here a is linear array of integer with n elements, the variable digitcount is usedto store the number of digits in the largest number in order to control the
number of passes to be performed.Begin
find the largest number of the array
set digitcount=no. of digits of the largest no.
for pass=1 to digitcount by 1 do
initialize bucketsfor i=1 to n-1 by 1 do
set digit=obtain digit no. pass of a[i]
put a[i] in bucket no. digit
increment bucket count for bucket no. digit
endfor
collect all the numbers from buckets in order
endfor
end
8/13/2019 Sorting in data structure
33/79
Analysis of bucket sort Let us suppose the number of digits in the
largest element of the given array is S.
the number of passes to be performed is n. Then , the umber of comparisons, f(n), needed
to sort the given array are
f(n)=
8/13/2019 Sorting in data structure
34/79
Merge Sort
8/13/2019 Sorting in data structure
35/79
Divide and Conquer
Divide-and-conquer method for algorithmdesign:
Divide: If the input size is too large to deal
with in a straightforward manner, divide theproblem into two or more disjoint subproblems
Conquer: Use divide and conquer recursivelyto solve the subproblems
Combine: Take the solutions to thesubproblems and merge these solutions intoa solution for the original problem
8/13/2019 Sorting in data structure
36/79
Merge Sort Algorithm
Divide: If S has at least two elements (nothingneeds to be done if S has zero or one elements),remove all the elements from S and put theminto two sequences, S1and S2, each containing
about half of the elements of S. (i.e. S1containsthe firstn/2 elements and S2contains theremaining n/2 elements).
Conquer: Sort sequences S1and S2using
Merge Sort.
Combine: Put back the elements into S bymerging the sorted sequences S1and S2intoone sorted sequence
8/13/2019 Sorting in data structure
37/79
Merge Sort: Algorithm
Merge-Sort(A, p, r)
if p < r then
q(p+r)/2Merge-Sort(A, p, q)
Merge-Sort(A, q+1, r)
Merge(A, p, q, r)
Merge(A, p, q, r)
Take the smallest of the two topmost elements of
sequences A[p..q] and A[q+1..r] and put into the
resulting sequence. Repeat this, until both sequences
are empty. Copy the resulting sequence into A[p..r].
8/13/2019 Sorting in data structure
38/79
MergeSort (Example) - 1
8/13/2019 Sorting in data structure
39/79
MergeSort (Example) - 2
8/13/2019 Sorting in data structure
40/79
MergeSort (Example) - 3
8/13/2019 Sorting in data structure
41/79
MergeSort (Example) - 4
8/13/2019 Sorting in data structure
42/79
MergeSort (Example) - 5
8/13/2019 Sorting in data structure
43/79
MergeSort (Example) - 6
8/13/2019 Sorting in data structure
44/79
MergeSort (Example) - 7
8/13/2019 Sorting in data structure
45/79
MergeSort (Example) - 8
8/13/2019 Sorting in data structure
46/79
MergeSort (Example) - 9
8/13/2019 Sorting in data structure
47/79
MergeSort (Example) - 10
8/13/2019 Sorting in data structure
48/79
MergeSort (Example) - 11
8/13/2019 Sorting in data structure
49/79
MergeSort (Example) - 12
8/13/2019 Sorting in data structure
50/79
MergeSort (Example) - 13
8/13/2019 Sorting in data structure
51/79
MergeSort (Example) - 14
8/13/2019 Sorting in data structure
52/79
MergeSort (Example) - 15
8/13/2019 Sorting in data structure
53/79
MergeSort (Example) - 16
8/13/2019 Sorting in data structure
54/79
MergeSort (Example) - 17
8/13/2019 Sorting in data structure
55/79
MergeSort (Example) - 18
8/13/2019 Sorting in data structure
56/79
MergeSort (Example) - 19
8/13/2019 Sorting in data structure
57/79
MergeSort (Example) - 20
8/13/2019 Sorting in data structure
58/79
MergeSort (Example) - 21
8/13/2019 Sorting in data structure
59/79
MergeSort (Example) - 22
8/13/2019 Sorting in data structure
60/79
Merge Sort Revisited
To sort n numbers if n=1 done!
recursively sort 2 lists ofnumbers n/2and n/2elements
merge 2 sorted lists in Q(n)time
Strategy break problem into similar
(smaller) subproblems
recursively solvesubproblems
combine solutions to answer
8/13/2019 Sorting in data structure
61/79
Analysis of merge Sort
The major work is done in the merge procedure, which isan O(n) operation.
Merge procedure is called from merge sort procedureafter the array is divided into two halves, and each
halves has been sorted. In each recursive calls, one for the left half and one for
the right half, array is divided into four segments.
At each level number of segments doubles, therefore thetotal division are log2n , hence total number of
comparisonsf(n)= n*log2n
=O(nlog2n)
8/13/2019 Sorting in data structure
62/79
Quick-Sort
8/13/2019 Sorting in data structure
63/79
Another divide-and-conquer sorting algorihm
To understand quick-sort, lets look at a high-level
description of the algorithm1)Divide: If the sequence S has 2 or more elements,
select an element x from S to be your pivot. Anyarbitrary element, like the last, will do. Remove all theelements of S and divide them into 3 sequences:
L, holds Ss elements less than x
E, holds Ss elements equal to x
G, holds Ss elements greater than x
2) Recurse: Recursively sort L and G3) Conquer: Finally, to put elements back into S in order,
first inserts the elements of L, then those of E, and thoseof G.
Here are some diagrams....
f Q S
8/13/2019 Sorting in data structure
64/79
Idea of Quick Sort
1) Select: pick an element
2) Divide: rearrangeelements so that x goes
to itsfinal position E
3) Recurse and Conquer:recursively sort
I Pl Q i k S t
8/13/2019 Sorting in data structure
65/79
In-Place Quick-Sort
Divide step: l scans the sequence from the left, and r from the right.
A swap is performed when l is at an element larger than the pivot and r is at onesmaller than the pivot.
8/13/2019 Sorting in data structure
66/79
In Place Quick Sort (contd)
A final swap with the pivot completes the divide step
8/13/2019 Sorting in data structure
67/79
Algorithm
quicksort(a , l, r)Begin
If(l
8/13/2019 Sorting in data structure
68/79
Analysis of the quick sort
To find the location of the element that splits thearray into two sections is an O(n) operation,because every element in the array is comparedto the dividing element.
After the division each section is examinedseparately.
If the array is split approximately in half (which isno usually), then there will be log2n splits.
therefore the total comparisons are:
f(n)=n*log2n = O(nlog2n)
8/13/2019 Sorting in data structure
69/79
Tree Sort
Tree sort method, in order to sort an array of
size n in ascending order, works in
following phases:
1. Build a binary search tree by using ncalls to insert operation
2. Print elements using inorder traversal.
8/13/2019 Sorting in data structure
70/79
Tree Sort
Array : 50, 60, 40, 45, 31, 75, 53, 65
Solution:
When these elements are inserted in binary
search tree one by one, the final binary
search tree looks like in next slide;
8/13/2019 Sorting in data structure
71/79
Given Array: 50, 60, 40, 45, 31, 75, 53, 65
50
40 60
75534531
65
Inorder traversal produces the following listing of elements:
31, 40, 45, 50, 53, 60, 65, 75
8/13/2019 Sorting in data structure
72/79
Tree Sort v/s Quick Sort
In tree sort , the first item is inserted into the rootof the tree and all subsequent elements arepartitioned to the left or right depending on theirrelation to the first element. This is analogous to
quick sort, if the first element in the array is usedas the pivot element for partitioned.
Further in tree sort, second element becomesthe root of the sub tree. It becomes the pivot
element to partition all subsequent elements inthat subtree. This is analogous to quick sort forpartitioning of one of the sublist.
8/13/2019 Sorting in data structure
73/79
Analysis of Tree Sort
All the comparisons for tree sort are done
during insert() calls. The insert() function
does the same number of comparisons as
quick sort. Therefore , tree sort has thesame running time as quick sort, i.e.
average case complexity is O(nlogn) and
worst case complexity is O(n2
)
8/13/2019 Sorting in data structure
74/79
Tree Sort
It does not require that all the elements to
be present in the array at the beginning of
sorting
Elements can be added gradually as theybecome available
It also works on a linked structure that
allows easier insertions and deletions thana contiguous list
Shell sort
8/13/2019 Sorting in data structure
75/79
Invented by Donald Shell in 1959, the shell sort is themost efficient of the O(n2) class of sorting algorithms. Ofcourse shell sort is also the most complex of the O(n2)
algorithms. This algorithm is similar to bubble sort in the sense it
also moves elements by exchanges.
It begins by comparing elements that are at a distance d.
With this the elements that are quite away from their
place will move rapidly than the simple bubble sort. In each pass, the value of d is reduced to half i.e.
di+1=(di+1)/2
In each pass, each element is compared with elementthat is located d position away from it, and exchange ismade if required.
The next iteration starts with new value of d.
The algorithm terminates when d=1
Example
8/13/2019 Sorting in data structure
76/79
12,9,-10,22,2,35,40
Starting value of d=n/2=7/2=3
12
9
-10
22
2
35
40
12
9
-10
22
2
35
40
12
2
-10
22
9
35
40
12
2
-10
22
9
35
40
12
2
-10
22
9
35
40
Given
array Pass 1
12
9
-10
22
2
35
40
Example
8/13/2019 Sorting in data structure
77/79
Starting value of di+1=(di+1)/2=(3+1)/2=2
12
2
-10
22
9
35
40
-10
2
12
22
9
35
40
-10
2
9
22
12
35
40
-10
2
9
22
12
35
40
-10
2
9
22
12
35
40
Pass 2
-10
2
12
22
9
35
40
Example
8/13/2019 Sorting in data structure
78/79
Starting value of di+1=(di+1)/2=(2+1)/2=1
-10
2
9
22
12
35
40
-10
2
9
22
12
35
40
-10
2
9
22
12
35
40
-10
2
9
12
22
35
40
-10
2
9
22
12
35
40
Pass 3
-10
2
9
22
12
35
40
A l i f h ll
8/13/2019 Sorting in data structure
79/79
Analysis of shell sort
It is difficult to predict the complexity of shellsort, as it is very difficult to show the effect of
one pass on another pass.
One thing is very clear that if the new distance dis computed using above formula, then the
number of passes will approximately be log2d
since d=1 will complete the sort.
Empirical studies have shown that the worstcase complexity of shell sort is O(n2)