MUTHAYAMMAL ENGINEERING COLLEGE
DEPARTMENT OF INFORMATION TECHNOLOGY
Subject Code:19ITC01
Subject Name: Data Structures
1
2
UNIT- 5
HASHING,SEARCHING,SORTING
Hashing Techniques:
Hashing:
• Hashing is a technique used to Performing
Insertion, deletion & search operations in the
constant average time by implementing Hash
table Data Structure .
• It is used to Index and Retrieve Items in a
Database.
3
Two Types of Hashing
1. Static Hashing .
2. Dynamic Hashing .
Static Hashing :
• It is the hash function maps search key value
to a fixed set of locations.
Dynamic Hashing :
• The Hash Table can grow to handle more
Items.The associated Hash Function must
Change as the table grows.
Hash Table :
• The Hash Table data structure is a array
of some fixed size table containing the Keys.
• A Key is a values associated with each record.
• A Hash table is partition into array of size.
• Each Bucket has many slots and each
slots holds one records.
Hash Function :
• A Hashing Function is a key to address
Transformation which acts upon a given Key to
complete the Relative Position of the Key in a
array 4
• A Key can be a member of a String etc..
• A Hash Function Formula is
• Hash (Key Value ) =(Key Values % Table Size)
• Hash (Key Value)=Key Values % Table Size
• Hash(10) =10 % 5=0
• Hash(33) =33 % 5 =3
• Hash(11)=11 % 5=1
Hash(21)= 21 % 5=1
5
6
A Good Hashing consist of
• Minimum Collision.
• Be easy and quick to complete.
• Distribute Key Value Every in the Hash Table.
• Use all the Information Provided in the Key.
Application of Hash Table:
• Database Systems
• Symbol Tables .
• Data Dictionaries
• Network Processing Algorithm
• Browse Casher.
Collision :
• Collision occurs when a hash values of a
records being Inserted hashes to an Address
That already contains a difference Record.
“ When Two Key values hash to the position”.
Insert 11,21 in hash table
11 =Hash(11) =11%5 =1
21= Hash (21) =21%5 =1
(collision occur)
7
8
Collision Resolution :
• The Process of finding another Position for
The Collide Record Is said to be collision
Resolution Strategy.
Two categories of Hashing .
1. Open Hashing
eg: Separate Chaining.
2. Closed Hashing .
eg : Open Addressing ,Rehashing and
Extendable hashing.
9
Open Hashing :
• Each Bucket in the Hash table is the head of a
Linked List.
• All Elements that hash to a Particular Bucket
are Placed on the Buckets Linked List .
Closed Hashing:
• Ensures that all elements are stored directly in
to the Hash Table.
10
Separate Chaining :
• Separate Chaining is an open hashing
Technique
• A Pointer fields is added to each record
Location.
• In this method the table can never overflow
since the linked are only extended upon or
New Keys.
Example:
10 , 11 , 81 , 10 , 7 , 34 , 94 , 17 , 29 , 89 , 99
11
12
The element 81 collides to the same of the hash
value to place the value 81 at this position perform
the following.
1. Traverse the list to check whether it is
already present.
2. Since, it is not already present, insert at end of the
list similarly, the rest of the elements are inserted.
Advantages:
 More number of elements can be inserted as it uses
of linked list.
 Collision resolution is simple and efficient.
13
Disadvantages:
It requires pointers, which
occupies more memory space.
Closed hashing:
• Collide elements are stored at another slot in
the table. Ensures that all elements stored
directly in the hash table.
Eg: Open addressing
Rehashing and extendable hashing.
MUTHAYAMMAL ENGINEERING COLLEGE
DEPARTMENT OF INFORMATION TECHNOLOGY
14
MUTHAYAMMAL ENGINEERING COLLEGE
DEPARTMENT OF INFORMATION TECHNOLOGY
Subject Code:19ITC01
Subject Name: Data Structures
1
2
Open Addressing:
•
• Open addressing also called closed
hashing which is an attentive to resolve the
collision with linked list.
If a collision occurs, alternative cells are tried
until an empty cell is found (i.e) cells ho(x), h1(x),
h2(x) are tried in succession.
There are three common collision strategies,
there are
1. Linear Probing
2. Quadratic Probing
3. Double Hashing
1.Linear Probing:
Key value :18,70,65,51,13
Table size : 7
3
4
5
6
Rehashing:
•
• If the table get’s to full. Then the
rehashing method new tables that is about twice as
with and scan down the entire original hash table.
The entire original hash table, computing the
new hash value for each element and in sorting it in
the new table.
• Rehashing is very expensive operations,
the running time is O(N), Rehashing can be
implements is several ways with Quadratic probing
such as
i). Rehash as soon as the table is half full.
7
• A new table is created as table so full. The size of the
table so full. The size of the table is 17, as this to the
first prime (i.e) a) twice as large as the old table size.
• The new hash function is h(x) = Xmod 17 the old
table is scanned and the elements, 6,15,24,23 and 13
are inserted into the new table.
8
9
Advantages:
Programmer does not about the table size.
Simple to implement.
It can be used in order data structure as well.
ii) Rehash only when an insertion fails. Suppose
the elements 13,15 ,23 ,24 & 6 are insert into
an open addressing hash table of size.
Hash function h(x) = x mod 7
10
If 23 is inserted into the Table ,the Resulting
Table Will be over 70% Full.
1) Insert 23:
Hash(23)=23%7
=2
Extendable Hashing:
• Extendable Hashing Allows a find to be
Performed in two disk accesses. Insertion
Also Requires few Disk accesses.
• Let us support consider our data consist of
determined by the leading two bits are the
data . 11
• In each leaf has upto m=4 elements ,identified
This is indicated by the number in Parenthesis.
12
13
14
Advantages:
• Provides quick access times for insert an find
operation on large database.
Disadvantages:
• This algorithm does not work if there are
more then m duplicates.
MUTHAYAMMAL ENGINEERING COLLEGE
DEPARTMENT OF INFORMATION TECHNOLOGY
15
MUTHAYAMMAL ENGINEERING COLLEGE
DEPARTMENT OF INFORMATION TECHNOLOGY
Subject Code:19ITC01
Subject Name: Data Structures
1
2
Searching Algorithm:
Searching is method to search a data item in
the given set. There are two types of search.
There are
1.Linear Search
2. Binary Search
3
Linear Search:
• Linear search is used to search and data item
in the given set in the sequential manner
Starting from the first element it is also called
as Sequential Search.
Routine for Linear Search:
void linear search(int x, int a[], int n)
{
int flag=0,i;
for(i=0;i<n;i++)
{
4
if(x==a[i])
{
flag=1
break;
}
}
if(flag
==1)
print f(“the element is found”);
else
print f(“the element is not
found”);
Analysis of Linear Search :
• BEST CASE ANALYSIS:0(1)
• AVERAGE CASE ANALYSIS:0(N)
• WORST CASE ANALYSIS: 0(N)
Binary Search:
• Binary Search is used to Search an Item in a
Sorted list . In this Method ,Initialize the lower
Limit as 1 And Upper Limit as N(N-1)
• The middle position is computed as (Lower
+Upper)/2 and check the element in the
Middle Position with the Data item to be
Searched. 5
• If the data item is greater then are Middle
Value then the Lower limit Is adjusted to one
Greater then the middle value.
• Otherwise , The Upper Limit is adjusted to
One Less then the Middle Value Ex: X=25.
6
7
Routine For Binary search:
void binary search(int x,int a[],int n);
{
int lower,upper,mid ;
lower=1;
upper=n;
while(lower<upper)
{
mid=(lower+upper)/2;
if(x>a[mid])
lower=mid+1;
8
else if(x<a[mid])
upper=mid-1;
else
{
printf(“element is found”);
break;
}
}
}
9
Analysis;
• BEST CASE ANALYSIS: 0(1)
• AVERAGE CASE ANALYSIS:0(logN)
• WORST CASE ANALYSIS: 0(LogN)
MUTHAYAMMAL ENGINEERING COLLEGE
DEPARTMENT OF INFORMATION TECHNOLOGY
10
MUTHAYAMMAL ENGINEERING COLLEGE
DEPARTMENT OF INFORMATION TECHNOLOGY
Subject Code:19ITC01
Subject Name: Data Structures
1
2
Def: Sorting is a
SORTING
Process (or) Technique
of
Arranging a group or a Sequence of Data
Elements in an order either in ascending or
descending.
Two Types of Sorting:
1.Internal Sorting
2.External Sorting.
3
Internal Sorting:
• A sorting in which all records of the file to be
sorted should be within the main memory at
the time of sorting.
External Sorting:
• Sorting is which at the time of Sorting Some
Record of the file to be Sorted can be in the
secondary memory.
Internal Sorting:
1.Insertion Sort
2.Shell sort
4
3.Heap Sort
4.Quick Sort
5.Radix Sort
6.Bubble Sort
7.Selection Sort
External Sorting
1.Merge Sort
2.Two Way Sort
3.Multiple Way Merge Sort
4.Polyphase merge sort.
5
Insertion Sort:
• Insertion Sort Works by tasking element from
the list one by one and inserting them in their
current position into a new sorted list.
• Insertion sort consists of N-1 Passes Where N is
the number of element to be sorted .
• The ith Pass of insertion sort will insert the ith
• Element A*1+ , A*2+ ,….A*i-1] .After Doing this
Insertion the Record occupying A*1+,….A*i+ are
in sorted order.
Insertion and Routine:
void insertion sort (element type a[], int N )
{
int j, p ;
element type tmp ;
for(p=1; p<N ; p++)
{
tmp =a[p];
for (j=p ; j>0&& a[j-1]>tmp ;j--)
a[j] =a[j-1];
a[j]=tmp ;
6
Example:
Consider an Unsorted array as Follows :
20, 10, 60, 40, 30, 15
Passes of Insertion Sort
7
8
Analysis of insertion Sort :
• WORST CASE ANALYSIS -
O(N^2)
• BEST CASE ANALYSIS - O(N)
• AVERAGE CASE ANALYSIS -
O(N^2)
9
Shell Sort :
• This method
is
an Improvement over the
Simple Insertion Sort .In this Method The
Element at Fixed Distance K (K is Preferably
prime Number ) or compared .
• The distance will then be decremented by
Some fixed amount and again the Comparison
will be made . Finally Individuals elements will
be compared.
10
11
12
Quick Sort [Partition Exchange Sort]:
• The idea Behind This Sorting is Much easier In
the Two Sort List Rather Then One Long list.
• All The Element on the Left Side Of Pivot
Should Be Smaller Or Equal To The Pivot.
• All The Element On the Right Side Of Pivot
Should Should Be Greater Then Or Equal To
Pivot.
13
The Process for Sorting the Element Quick Sort
is as:
I. Take the First Element of List as Pivot .
II. Place At The Proper Place In List So one
Element of The List (i.e) Pivot will be at its
Proper Place.
III. Create two Sublist’s Left Child, Right Child of
Pivot.
IV. Repeat the same Process Until all element of
List are at Proper Position in List.
14
For Placing the Pivot at Proper Place we have a
Need to do The Following Process:
I. Compare the Pivot Element One by One From
Right to Left For Getting The Element Which
Has Value Less Then Pivot Element
.Interchange The Element With Pivot
Element.
II. Now the Comparison will start from the
Interchanged element position from left to
right for getting the element which has higher
Value then Pivot.
III. Repeat the same process Until Process is at its
Proper position.
15
16
Now Combine 2 Sub list
8 19 29 42 48 59 65 68 72 82 88 95
All The Elements Are New Sorted.
Analysis of Quick Sort :
• WORST CASE ANALYSIS - O[N^2]
• BEST CASE ANALYSIS - O[N log N ]
• AVERAGE CASE ANALYSIS – O[N log N]
Advantage :
• It is Faster than other O(N log N) algorithm.
• It has better cache Performance and High
Speed.
Limitations: Requires More Memory Space 17
18
Heap Sort :
• In heap sort the array of Interpret as a binary
Tree. This Method pass 2 Phases.
• In Phase 1: Binary heap is Constructed.
• In Phase 2: Delete min Routine is Performed.
Phase 1: Two Properties of Binary Heap :
 Structure Property .
 Heap order Property.
Structure Property :
• For Any Element in Array Position i , The Left
child is in 2i+1 (i.e) The Cell after the Left Child
19
Heap Order Property:
• The key Values in the parent Node is Smaller
then or equal to the key Value of any in its
Child node.
• To Build the Heap , apply the heap order
Property Starting from the Right Most Non –
Leaf Node at the Bottom level.
Phase 2:
The Array Element are Stored using Deletion
Operation.
Example :
20
21
22
23
Routine for Heap Sort :
#define left child [i] (2*(i)+1)
void perdown (element type A[] , int i , int N)
{
int child ;
element type tmp ;
for(tmp =A[i] , left child (i)<N , i=child)
{
child =left child (i);
if (child!=N-1&&A[child +]>A[child])
child ++ ;
24
if (tmp<A[child])
A[i]=A[child]);
else
Break ;
}
A[i]
=tm
p;
}
void
hea
p
25
perdown (A ,i , N);
for(i=N-1; i>0; i--)
{
swap (&A[0] ,&A[i]);
per down (A,o,i);
}
}
}
Analysis of Heap Sort :
 Worst Case Analysis =O(N log N)
 Best case Analysis = O(N log N)
 Arg Case Analysis = O(N log N)
Advantages:
• It is efficient for Sorting Large number
of Element.
• It has the Add of Worst case.
Limitation :
• It is Not a stable Sort .
• It Require Most Processing Time .
27
Radix Sort :
• Radix Sort is one of the Linear Sorting
algorithm for Integers.
• It is generated from radix Sort.
• It can be performed using Bucket 0 to
9.
• It is also called as Binsort.
• In First Pass all element arranged according to
the least Significant digit. In Second Pass ,the
element are arranged according to the next
least significant digit and so on.
28
29
30
External Sorting
Merge Sort :
• The most common algorithm used in external
Sorting is the merge sort this algorithm
follows Divide and Conquer strategy.
Merge Sort Routine :
void Msort ( element type A[], element type
tmparray [],int left ,int right)
int center ;
if (left<right)
{
31
center =(left +right)/2;
msort(A, tmp array, left ,center ) ;
msort (A,tmp array , center , right ) ;
merge (A , tmp
array ,left ,center+1,right);
}
}
void merge sort (element type
A[],int N))
{
element type *tmparray ;
32
if (tmparray!=NULL)
{
msort (A,tmparray,0,N++);
free(tmp array);
}
else
{
fatal error (“no sapce for
tmp array”)
}
}
33
Merge Routine:
void merge(element type A[], element type
tmparray [], int LPOS , int RPOS , int rightend)
{
int I, leftend ,numelement , tmppos;
left end =RPOS-1;
TMPPOS =LPOS ;
num element = rightend –LPOS
+1;
while (Lpos<=leftend && RPOS<=rightend)
if (A[LPOS] <=A[RPOS])
34
else
tmp array [tmppos++]=A[RPOS++];
while(LPOS<=leftend)
tmparray [tmppos++] =A[LPOS++];
while(RPOS< =rightend)
tmparray [tmppos++] =A[RPOS++];
for(i=0; i<numelements; i++; rightend--)
A[rightend ]=tmparray [rightend];
}
35
36
37
MUTHAYAMMAL ENGINEERING COLLEGE
DEPARTMENT OF INFORMATION TECHNOLOGY
38

Working with python Nice PPT must try very good

  • 1.
    MUTHAYAMMAL ENGINEERING COLLEGE DEPARTMENTOF INFORMATION TECHNOLOGY Subject Code:19ITC01 Subject Name: Data Structures 1
  • 2.
    2 UNIT- 5 HASHING,SEARCHING,SORTING Hashing Techniques: Hashing: •Hashing is a technique used to Performing Insertion, deletion & search operations in the constant average time by implementing Hash table Data Structure . • It is used to Index and Retrieve Items in a Database.
  • 3.
    3 Two Types ofHashing 1. Static Hashing . 2. Dynamic Hashing . Static Hashing : • It is the hash function maps search key value to a fixed set of locations. Dynamic Hashing : • The Hash Table can grow to handle more Items.The associated Hash Function must Change as the table grows.
  • 4.
    Hash Table : •The Hash Table data structure is a array of some fixed size table containing the Keys. • A Key is a values associated with each record. • A Hash table is partition into array of size. • Each Bucket has many slots and each slots holds one records. Hash Function : • A Hashing Function is a key to address Transformation which acts upon a given Key to complete the Relative Position of the Key in a array 4
  • 5.
    • A Keycan be a member of a String etc.. • A Hash Function Formula is • Hash (Key Value ) =(Key Values % Table Size) • Hash (Key Value)=Key Values % Table Size • Hash(10) =10 % 5=0 • Hash(33) =33 % 5 =3 • Hash(11)=11 % 5=1 Hash(21)= 21 % 5=1 5
  • 6.
    6 A Good Hashingconsist of • Minimum Collision. • Be easy and quick to complete. • Distribute Key Value Every in the Hash Table. • Use all the Information Provided in the Key. Application of Hash Table: • Database Systems • Symbol Tables . • Data Dictionaries • Network Processing Algorithm • Browse Casher.
  • 7.
    Collision : • Collisionoccurs when a hash values of a records being Inserted hashes to an Address That already contains a difference Record. “ When Two Key values hash to the position”. Insert 11,21 in hash table 11 =Hash(11) =11%5 =1 21= Hash (21) =21%5 =1 (collision occur) 7
  • 8.
    8 Collision Resolution : •The Process of finding another Position for The Collide Record Is said to be collision Resolution Strategy. Two categories of Hashing . 1. Open Hashing eg: Separate Chaining. 2. Closed Hashing . eg : Open Addressing ,Rehashing and Extendable hashing.
  • 9.
    9 Open Hashing : •Each Bucket in the Hash table is the head of a Linked List. • All Elements that hash to a Particular Bucket are Placed on the Buckets Linked List . Closed Hashing: • Ensures that all elements are stored directly in to the Hash Table.
  • 10.
    10 Separate Chaining : •Separate Chaining is an open hashing Technique • A Pointer fields is added to each record Location. • In this method the table can never overflow since the linked are only extended upon or New Keys. Example: 10 , 11 , 81 , 10 , 7 , 34 , 94 , 17 , 29 , 89 , 99
  • 11.
  • 12.
    12 The element 81collides to the same of the hash value to place the value 81 at this position perform the following. 1. Traverse the list to check whether it is already present. 2. Since, it is not already present, insert at end of the list similarly, the rest of the elements are inserted. Advantages:  More number of elements can be inserted as it uses of linked list.  Collision resolution is simple and efficient.
  • 13.
    13 Disadvantages: It requires pointers,which occupies more memory space. Closed hashing: • Collide elements are stored at another slot in the table. Ensures that all elements stored directly in the hash table. Eg: Open addressing Rehashing and extendable hashing.
  • 14.
    MUTHAYAMMAL ENGINEERING COLLEGE DEPARTMENTOF INFORMATION TECHNOLOGY 14
  • 15.
    MUTHAYAMMAL ENGINEERING COLLEGE DEPARTMENTOF INFORMATION TECHNOLOGY Subject Code:19ITC01 Subject Name: Data Structures 1
  • 16.
    2 Open Addressing: • • Openaddressing also called closed hashing which is an attentive to resolve the collision with linked list. If a collision occurs, alternative cells are tried until an empty cell is found (i.e) cells ho(x), h1(x), h2(x) are tried in succession. There are three common collision strategies, there are 1. Linear Probing 2. Quadratic Probing 3. Double Hashing
  • 17.
    1.Linear Probing: Key value:18,70,65,51,13 Table size : 7 3
  • 18.
  • 19.
  • 20.
    6 Rehashing: • • If thetable get’s to full. Then the rehashing method new tables that is about twice as with and scan down the entire original hash table. The entire original hash table, computing the new hash value for each element and in sorting it in the new table. • Rehashing is very expensive operations, the running time is O(N), Rehashing can be implements is several ways with Quadratic probing such as i). Rehash as soon as the table is half full.
  • 21.
    7 • A newtable is created as table so full. The size of the table so full. The size of the table is 17, as this to the first prime (i.e) a) twice as large as the old table size. • The new hash function is h(x) = Xmod 17 the old table is scanned and the elements, 6,15,24,23 and 13 are inserted into the new table.
  • 22.
  • 23.
    9 Advantages: Programmer does notabout the table size. Simple to implement. It can be used in order data structure as well. ii) Rehash only when an insertion fails. Suppose the elements 13,15 ,23 ,24 & 6 are insert into an open addressing hash table of size. Hash function h(x) = x mod 7
  • 24.
  • 25.
    If 23 isinserted into the Table ,the Resulting Table Will be over 70% Full. 1) Insert 23: Hash(23)=23%7 =2 Extendable Hashing: • Extendable Hashing Allows a find to be Performed in two disk accesses. Insertion Also Requires few Disk accesses. • Let us support consider our data consist of determined by the leading two bits are the data . 11
  • 26.
    • In eachleaf has upto m=4 elements ,identified This is indicated by the number in Parenthesis. 12
  • 27.
  • 28.
    14 Advantages: • Provides quickaccess times for insert an find operation on large database. Disadvantages: • This algorithm does not work if there are more then m duplicates.
  • 29.
    MUTHAYAMMAL ENGINEERING COLLEGE DEPARTMENTOF INFORMATION TECHNOLOGY 15
  • 30.
    MUTHAYAMMAL ENGINEERING COLLEGE DEPARTMENTOF INFORMATION TECHNOLOGY Subject Code:19ITC01 Subject Name: Data Structures 1
  • 31.
    2 Searching Algorithm: Searching ismethod to search a data item in the given set. There are two types of search. There are 1.Linear Search 2. Binary Search
  • 32.
    3 Linear Search: • Linearsearch is used to search and data item in the given set in the sequential manner Starting from the first element it is also called as Sequential Search. Routine for Linear Search: void linear search(int x, int a[], int n) { int flag=0,i; for(i=0;i<n;i++) {
  • 33.
    4 if(x==a[i]) { flag=1 break; } } if(flag ==1) print f(“the elementis found”); else print f(“the element is not found”);
  • 34.
    Analysis of LinearSearch : • BEST CASE ANALYSIS:0(1) • AVERAGE CASE ANALYSIS:0(N) • WORST CASE ANALYSIS: 0(N) Binary Search: • Binary Search is used to Search an Item in a Sorted list . In this Method ,Initialize the lower Limit as 1 And Upper Limit as N(N-1) • The middle position is computed as (Lower +Upper)/2 and check the element in the Middle Position with the Data item to be Searched. 5
  • 35.
    • If thedata item is greater then are Middle Value then the Lower limit Is adjusted to one Greater then the middle value. • Otherwise , The Upper Limit is adjusted to One Less then the Middle Value Ex: X=25. 6
  • 36.
    7 Routine For Binarysearch: void binary search(int x,int a[],int n); { int lower,upper,mid ; lower=1; upper=n; while(lower<upper) { mid=(lower+upper)/2; if(x>a[mid]) lower=mid+1;
  • 37.
  • 38.
    9 Analysis; • BEST CASEANALYSIS: 0(1) • AVERAGE CASE ANALYSIS:0(logN) • WORST CASE ANALYSIS: 0(LogN)
  • 39.
    MUTHAYAMMAL ENGINEERING COLLEGE DEPARTMENTOF INFORMATION TECHNOLOGY 10
  • 40.
    MUTHAYAMMAL ENGINEERING COLLEGE DEPARTMENTOF INFORMATION TECHNOLOGY Subject Code:19ITC01 Subject Name: Data Structures 1
  • 41.
    2 Def: Sorting isa SORTING Process (or) Technique of Arranging a group or a Sequence of Data Elements in an order either in ascending or descending. Two Types of Sorting: 1.Internal Sorting 2.External Sorting.
  • 42.
    3 Internal Sorting: • Asorting in which all records of the file to be sorted should be within the main memory at the time of sorting. External Sorting: • Sorting is which at the time of Sorting Some Record of the file to be Sorted can be in the secondary memory. Internal Sorting: 1.Insertion Sort 2.Shell sort
  • 43.
    4 3.Heap Sort 4.Quick Sort 5.RadixSort 6.Bubble Sort 7.Selection Sort External Sorting 1.Merge Sort 2.Two Way Sort 3.Multiple Way Merge Sort 4.Polyphase merge sort.
  • 44.
    5 Insertion Sort: • InsertionSort Works by tasking element from the list one by one and inserting them in their current position into a new sorted list. • Insertion sort consists of N-1 Passes Where N is the number of element to be sorted . • The ith Pass of insertion sort will insert the ith • Element A*1+ , A*2+ ,….A*i-1] .After Doing this Insertion the Record occupying A*1+,….A*i+ are in sorted order.
  • 45.
    Insertion and Routine: voidinsertion sort (element type a[], int N ) { int j, p ; element type tmp ; for(p=1; p<N ; p++) { tmp =a[p]; for (j=p ; j>0&& a[j-1]>tmp ;j--) a[j] =a[j-1]; a[j]=tmp ; 6
  • 46.
    Example: Consider an Unsortedarray as Follows : 20, 10, 60, 40, 30, 15 Passes of Insertion Sort 7
  • 47.
    8 Analysis of insertionSort : • WORST CASE ANALYSIS - O(N^2) • BEST CASE ANALYSIS - O(N) • AVERAGE CASE ANALYSIS - O(N^2)
  • 48.
    9 Shell Sort : •This method is an Improvement over the Simple Insertion Sort .In this Method The Element at Fixed Distance K (K is Preferably prime Number ) or compared . • The distance will then be decremented by Some fixed amount and again the Comparison will be made . Finally Individuals elements will be compared.
  • 49.
  • 50.
  • 51.
    12 Quick Sort [PartitionExchange Sort]: • The idea Behind This Sorting is Much easier In the Two Sort List Rather Then One Long list. • All The Element on the Left Side Of Pivot Should Be Smaller Or Equal To The Pivot. • All The Element On the Right Side Of Pivot Should Should Be Greater Then Or Equal To Pivot.
  • 52.
    13 The Process forSorting the Element Quick Sort is as: I. Take the First Element of List as Pivot . II. Place At The Proper Place In List So one Element of The List (i.e) Pivot will be at its Proper Place. III. Create two Sublist’s Left Child, Right Child of Pivot. IV. Repeat the same Process Until all element of List are at Proper Position in List.
  • 53.
    14 For Placing thePivot at Proper Place we have a Need to do The Following Process: I. Compare the Pivot Element One by One From Right to Left For Getting The Element Which Has Value Less Then Pivot Element .Interchange The Element With Pivot Element. II. Now the Comparison will start from the Interchanged element position from left to right for getting the element which has higher Value then Pivot. III. Repeat the same process Until Process is at its Proper position.
  • 54.
  • 55.
  • 56.
    Now Combine 2Sub list 8 19 29 42 48 59 65 68 72 82 88 95 All The Elements Are New Sorted. Analysis of Quick Sort : • WORST CASE ANALYSIS - O[N^2] • BEST CASE ANALYSIS - O[N log N ] • AVERAGE CASE ANALYSIS – O[N log N] Advantage : • It is Faster than other O(N log N) algorithm. • It has better cache Performance and High Speed. Limitations: Requires More Memory Space 17
  • 57.
    18 Heap Sort : •In heap sort the array of Interpret as a binary Tree. This Method pass 2 Phases. • In Phase 1: Binary heap is Constructed. • In Phase 2: Delete min Routine is Performed. Phase 1: Two Properties of Binary Heap :  Structure Property .  Heap order Property. Structure Property : • For Any Element in Array Position i , The Left child is in 2i+1 (i.e) The Cell after the Left Child
  • 58.
    19 Heap Order Property: •The key Values in the parent Node is Smaller then or equal to the key Value of any in its Child node. • To Build the Heap , apply the heap order Property Starting from the Right Most Non – Leaf Node at the Bottom level. Phase 2: The Array Element are Stored using Deletion Operation. Example :
  • 59.
  • 60.
  • 61.
  • 62.
    23 Routine for HeapSort : #define left child [i] (2*(i)+1) void perdown (element type A[] , int i , int N) { int child ; element type tmp ; for(tmp =A[i] , left child (i)<N , i=child) { child =left child (i); if (child!=N-1&&A[child +]>A[child]) child ++ ;
  • 63.
  • 64.
    25 perdown (A ,i, N); for(i=N-1; i>0; i--) { swap (&A[0] ,&A[i]); per down (A,o,i); } } }
  • 65.
    Analysis of HeapSort :  Worst Case Analysis =O(N log N)  Best case Analysis = O(N log N)  Arg Case Analysis = O(N log N) Advantages: • It is efficient for Sorting Large number of Element. • It has the Add of Worst case. Limitation : • It is Not a stable Sort . • It Require Most Processing Time .
  • 66.
    27 Radix Sort : •Radix Sort is one of the Linear Sorting algorithm for Integers. • It is generated from radix Sort. • It can be performed using Bucket 0 to 9.
  • 67.
    • It isalso called as Binsort. • In First Pass all element arranged according to the least Significant digit. In Second Pass ,the element are arranged according to the next least significant digit and so on. 28
  • 68.
  • 69.
    30 External Sorting Merge Sort: • The most common algorithm used in external Sorting is the merge sort this algorithm follows Divide and Conquer strategy. Merge Sort Routine : void Msort ( element type A[], element type tmparray [],int left ,int right) int center ; if (left<right) {
  • 70.
    31 center =(left +right)/2; msort(A,tmp array, left ,center ) ; msort (A,tmp array , center , right ) ; merge (A , tmp array ,left ,center+1,right); } } void merge sort (element type A[],int N)) { element type *tmparray ;
  • 71.
    32 if (tmparray!=NULL) { msort (A,tmparray,0,N++); free(tmparray); } else { fatal error (“no sapce for tmp array”) } }
  • 72.
    33 Merge Routine: void merge(elementtype A[], element type tmparray [], int LPOS , int RPOS , int rightend) { int I, leftend ,numelement , tmppos; left end =RPOS-1; TMPPOS =LPOS ; num element = rightend –LPOS +1; while (Lpos<=leftend && RPOS<=rightend) if (A[LPOS] <=A[RPOS])
  • 73.
    34 else tmp array [tmppos++]=A[RPOS++]; while(LPOS<=leftend) tmparray[tmppos++] =A[LPOS++]; while(RPOS< =rightend) tmparray [tmppos++] =A[RPOS++]; for(i=0; i<numelements; i++; rightend--) A[rightend ]=tmparray [rightend]; }
  • 74.
  • 75.
  • 76.
  • 77.
    MUTHAYAMMAL ENGINEERING COLLEGE DEPARTMENTOF INFORMATION TECHNOLOGY 38