SlideShare a Scribd company logo
1 of 81
Prof. Sonali
Mhatre
DATA STRUCTURES AND
ANALYSIS
SORTING METHODS
APPLICATIONS
APPLICATIONS
APPLICATIONS
APPLICATIONS
select * from employee where city=‘Mumbai’ order by salary;
APPLICATIONS
order by
APPLICATIONS
98 87 64 75 25 12 45 33
BUBBLE SORT
Pass 1 i =1
j=0
87 98 64 75 25 12 45 33
j=1
87 64 98 75 25 12 45 33
j=2
87 64 75 98 25 12 45 33
j=3
SIZE = 8
87 64 75 25 98 12 45 33
BUBBLE SORT
87 64 75 25 12 98 45 33
j=5
87 64 75 25 12 45 98 33
j=6
87 64 75 25 12 45 33 98
j=4
SIZE = 8
Pass 1 i =1
87 64 75 25 12 45 33 98
BUBBLE SORT
Pass 2 i = 2 SIZE = 8
64 87 75 25 12 45 33 98
j=0
j=1
64 75 87 25 12 45 33 98
j=2
64 75 25 87 12 45 33 98
j=3
BUBBLE SORT
SIZE = 8
64 75 25 12 87 45 33 98
j=4
64 75 25 12 45 87 33 98
j=5
64 75 25 12 45 33 87 98
Pass 2 i = 2
64 75 25 12 45 33 87 98
BUBBLE SORT
SIZE = 8
64 75 25 12 45 33 87 98
j=0
j=1
64 25 75 12 45 33 87 98
j=2
64 25 12 75 45 33 87 98
j=3
Pass 3 i = 3
BUBBLE SORT
SIZE = 8
64 25 12 45 75 33 87 98
j=4
64 25 12 45 33 75 87 98
Pass 3 i = 3
64 25 12 45 33 75 87 98
BUBBLE SORT
Pass 4 i=4 SIZE = 8
25 64 12 45 33 75 87 98
j=0
j=1
25 12 64 45 33 75 87 98
j=2
25 12 45 64 33 75 87 98
j=3
BUBBLE SORT
SIZE = 8
25 12 45 33 64 75 87 98
Pass 4 i=4
25 12 45 33 64 75 87 98
BUBBLE SORT
Pass 5 i=5 SIZE = 8
12 25 45 33 64 75 87 98
j=0
j=1
12 25 45 33 64 75 87 98
j=2
12 25 33 45 64 75 87 98
12 25 33 45 64 75 87 98
BUBBLE SORT
Pass 6 i=6 SIZE = 8
12 25 33 45 64 75 87 98
j=0
j=1
12 25 33 45 64 75 87 98
12 25 33 45 64 75 87 98
BUBBLE SORT
Pass 7 i=7 SIZE = 8
12 25 33 45 64 75 87 98
j=0
 Adjacent elements are compared
 For N number of elements (8 elements)
 N-1 number of passes (7 elements)
 In Pass 1, Number of comparisons N-1 (7)
 In Pass 2, Number of comparisons N-2 (6)
 In Pass 3, Number of comparisons N-3 (5)
 ……
 In Pass N-3 (5), Number of comparisons 3
 In Pass N-2 (6), Number of comparisons 2
 In Pass N-1 (7), Number of comparisons 1
BUBBLE SORT
 Total Number of comparisons for 8 elements
 7+6+5+4+3+2+1 = (8 * 7)/2
 Total Number of comparisons for N elements
 (N * N-1)/2
BUBBLE SORT
for(i=1;i<SIZE;i++)
{
for(j=0;j<SIZE-i;j++)
{
if(a[j]>a[j+1])
{
int tmp=a[j];
a[j]=a[j+1];
a[j+1]=tmp;
}
}
printf("nPass %d:",pass++);
display(a);
}
98 87 64 75 25 12 45 33
SELECTION SORT
Pass 1
j=7
SIZE = 8
max = 98
maxi = 0
j=1 j=2 j=3 j=4 j=5 j=6
33 87 64 75 25 12 45 98
i =7
33 87 64 75 25 12 45 98
SELECTION SORT
Pass 2 SIZE = 8
max = 33
maxi = 0
j=1 j=2 j=3 j=4 j=5 j=6
33 45 64 75 25 12 87 98
i = 6
max = 87
maxi = 1
33 45 64 75 25 12 87 98
SELECTION SORT
Pass 3 SIZE = 8
max = 33
maxi = 0
j=1 j=2 j=3 j=4 j=5
33 45 64 12 25 75 87 98
i = 5
max = 45
maxi = 1
max = 64
maxi = 2 max = 75
maxi = 3
33 45 64 12 25 75 87 98
SELECTION SORT
Pass 4 SIZE = 8
max = 33
maxi = 0
j=1 j=2 j=3 j=4
33 45 25 12 64 75 87 98
i = 4
max = 45
maxi = 1
max = 64
maxi = 2
33 45 25 12 64 75 87 98
SELECTION SORT
Pass 5 SIZE = 8
max = 33
maxi = 0
j=1 j=2 j=3
33 12 25 45 64 75 87 98
i = 3
max = 45
maxi = 1
33 12 25 45 64 75 87 98
SELECTION SORT
Pass 6 SIZE = 8
max = 33
maxi = 0
j=1 j=2
25 12 33 45 64 75 87 98
i = 2
25 12 33 45 64 75 87 98
SELECTION SORT
Pass 7 SIZE = 8
max = 25
maxi = 0
j=1
12 25 33 45 64 75 87 98
i = 1
 Select the maximum element and place it at the end
 max – is the Max element seen so far
 maxi – is the index of the same
 In Pass 1, Number of comparisons N-1 (7)
 In Pass 2, Number of comparisons N-2 (6)
 In Pass 3, Number of comparisons N-3 (5)
 ……
 In Pass N-3 (5), Number of comparisons 3
 In Pass N-2 (6), Number of comparisons 2
 In Pass N-1 (7), Number of comparisons 1
SELECTION SORT
 Total Number of comparisons for 8 elements
 7+6+5+4+3+2+1 = (8 * 7)/2
 Total Number of comparisons for N elements
 (N * N-1)/2
for(i=SIZE-1;i>0;i--)
{
max=a[0];
maxi=0;
for(j=1;j<=i;j++)
{
if(a[j]>max)
{
max=a[j];
maxi=j;
}
}
a[maxi]=a[i];
a[i]=max;
printf("nPass %d",pass++);
display(a);
}
SELECTION SORT
 Select the minimum element and place it at the beginning
 min – is the Min element seen so far
 mini – is the index of the same
 In Pass 1, Number of comparisons N-1 (7)
 In Pass 2, Number of comparisons N-2 (6)
 In Pass 3, Number of comparisons N-3 (5)
 ……
 In Pass N-3 (5), Number of comparisons 3
 In Pass N-2 (6), Number of comparisons 2
 In Pass N-1 (7), Number of comparisons 1
SELECTION SORT
for(i=0;i<SIZE-1;i++)
{
min=a[i];
mini=i;
for(j=i+1;j<SIZE;j++)
{
if(a[j]<min)
{
min=a[j];
mini=j;
}
}
a[mini]=a[i];
a[i]=min;
printf("nPass %d",pass++);
display(a);
}
SELECTION SORT
INSERTION SORT
98 87 64 75 25 12 45 33
Pass 1
i=1
SIZE = 8
y = 87
j=0
98 87 64 75 25 12 45 33
i=1
j=0
98 98 64 75 25 12 45 33
i=1
j=-1
87 98 64 75 25 12 45 33
i=1
j=0
INSERTION SORT
87 98 64 75 25 12 45 33
Pass 2
i=2
SIZE = 8
y = 64
j=1
87 98 98 75 25 12 45 33
i=2
j=0
87 87 98 75 25 12 45 33
i=2
j=-1
64 87 98 75 25 12 45 33
i=2
j=0
INSERTION SORT
64 87 98 75 25 12 45 33
Pass 3
i=3
SIZE = 8
y = 75
j=2
64 87 98 98 25 12 45 33
i=3
j=1
64 87 87 98 25 12 45 33
i=3
j=0
64 75 87 98 25 12 45 33
i=3
j=1
INSERTION SORT
64 75 87 98 25 12 45 33
Pass 4
i=4
SIZE = 8
y = 25
j=3
64 75 87 98 98 12 45 33
i=4
j=2
64 75 87 87 98 12 45 33
i=4
j=1
64 75 75 87 98 12 45 33
i=4
j=0
INSERTION SORT
Pass 4 SIZE = 8
y = 25
64 64 75 87 98 12 45 33
i=4
j=-1
25 64 75 87 98 12 45 33
i=4
j=0
INSERTION SORT
Pass 5 SIZE = 8
y = 12
25 64 75 87 98 12 45 33
i=5
j=4
25 64 75 87 98 98 45 33
i=5
j=3
25 64 75 87 87 98 45 33
i=5
j=2
25 64 75 75 87 98 45 33
i=5
j=1
25 64 64 75 87 98 45 33
i=5
j=0
INSERTION SORT
25 25 64 75 87 98 45 33
i=5
j=-1
12 25 64 75 87 98 45 33
i=5
j=0
Pass 5 SIZE = 8
y = 12
Pass 6 SIZE = 8
y = 45
12 25 64 75 87 98 45 33
i=6
j=5
INSERTION SORT
12 25 64 75 87 98 98 33
i=6
j=4
12 25 64 75 87 87 98 33
i=6
j=3
12 25 64 75 75 87 98 33
i=6
j=2
Pass 6 SIZE = 8
y = 45
INSERTION SORT
12 25 64 64 75 87 98 33
i=6
j=1
12 25 45 64 75 87 98 33
i=6
j=2
INSERTION SORT
12 25 45 64 75 87 98 33
i=7
j=6
Pass 7 SIZE = 8
y = 33
12 25 45 64 75 87 98 98
i=7
j=5
12 25 45 64 75 87 87 98
i=7
j=4
12 25 45 64 75 75 87 98
i=7
j=3
INSERTION SORT
12 25 45 64 64 75 87 98
i=7
j=2
Pass 7 SIZE = 8
y = 33
12 25 45 45 64 75 87 98
i=7
j=1
12 25 33 45 64 75 87 98
i=7
j=2
for(i=1;i<SIZE;i++)
{
y=a[i];
for(j=i-1;j>=0 && a[j]>y;j--)
{
a[j+1]=a[j];
}
a[j+1]=y;
printf("nPass %d",(pass++));
display(a);
}
INSERTION SORT
 Array is divided into sorted and unsorted part
 Pick the first element from the unsorted array and find its
place it in the sorted array and place it there
 In Pass 1, Number of comparisons 1
 In Pass 2, Number of comparisons 2
 In Pass 3, Number of comparisons 3
 ……
 In Pass N-3 (5), Number of comparisons N-3 (5)
 In Pass N-2 (6), Number of comparisons N-2 (6)
 In Pass N-1 (7), Number of comparisons N-3 (7)
 Total Number of comparisons for 8 elements
 1+2+3+4+5+6+7 = (8 * 7)/2
 Total Number of comparisons for N elements
 (N * N-1)/2
INSERTION SORT
 Searching is the process used to find the location of a target
among a list of objects.
SEARCHING
APPLICATIONS
APPLICATIONS
LINEAR SEARCH
 For 1 elements, No. of comparisons = 1
 For 2 elements, No. of comparisons = 2
 …..
 For N-1 elements, No. of comparisons = N-1
 For N elements, No. of comparisons = N
LINEAR SEARCH
int el,i;
printf("nEnter element to be searched? ");
scanf("%d",&el);
for(i=0;i<SIZE;i++)
{
if(a[i]==el)
return i;
}
return -1;
LINEAR SEARCH
BINARY SEARCH
 Input array must be a sorted array with elements in ascending
order
BINARY SEARCH
 For 16 elements
 1 Comparison will divide the array in two parts of size 8
 2 Comparisons will divide the array in four parts of size 4
 3 Comparisons will divide the array in eight parts of size 2
 4 Comparisons will divide the array in sixteen parts of size 1
 For 32 elements
 1 Comparison will divide the array in two parts of size 16
 2 Comparisons will divide the array in four parts of size 8
 3 Comparisons will divide the array in eight parts of size 4
 4 Comparisons will divide the array in sixteen parts of size 2
 5 Comparisons will divide the array in thirty two parts of size 1
 8 elements -> 3 comparisons
 16 elements -> 4 comparisons
 32 elements -> 5 comparisons
 64 elements -> 6 comparisons
 ……
 In general for 2N elements there will be N comparisons
BINARY SEARCH
 N elements -> Log2N comparisons
low=0;
high=SIZE-1;
while(low<=high)
{
mid=(low+high)/2;
if(el==a[mid])
return mid;
else if(el>a[mid])
low=mid+1;
else
high=mid-1;
}
return -1;
BINARY SEARCH
 In Linear search, search complexity is O(n)
 In Binary Search, search complexity is O(log2n)
HASHING
Hashing makes it possible in O(1)
 A hash table is a collection of elements which are stored in
such a way as to make it easy to find them later.
 Each position of the hash table, often called a slot, can hold
an element and is named by an integer value starting at 0.
 The mapping between an element and the slot where that
element belongs in the hash table is called the hash function.
HASH FUNCTION
Division method
Multiplication method
Mid-square method
Folding method
Truncation method
HASH FUNCTION
HASH FUNCTION (DIVISION)
0 1 2 3 4 5 6 7 8 9 10 11
Element: 68 68 Modulo N
N = 12
8
68
Element: 72 72 Modulo N 0
72
Element: 122 122 Modulo N 2
122
Element: 46 46 Modulo N 10
46
Element: 15 15 Modulo N 3
15
Element: 50 50 Modulo N 2
50
 The value of N is chosen to be a Prime Number
 Given a collection of items, a hash function that maps each
item into a unique slot is referred to as a perfect hash
function.
HASH FUNCTION (DIVISION)
 Step 1: Choose a constant A such that 0 < A < 1
 Step 2: Multiply the element k by A.
 Step 3: Extract the fractional part of k*A
 Step 4: Multiply Extracted fractional by the size of hash table
 The multiplication method works with any value of A, but
Knuth has suggested that the best choice of A is
0.6180339887
HASH FUNCTION (MULTIPLICATION)
 The value of N is chosen to be some power of 2
HASH FUNCTION (MULTIPLICATION)
N = 16
23
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
key key*A fraction (key*A) Fraction*N floor
23 14.214782 0.214782 3.436508 3.000000
677 418.409010 0.409010 6.544166 6.000000
677
1234 762.653942 0.653942 10.463073 10.000000
1234
A = 0.6180339887
445 275.025125 0.025125 0.402000 0.000000
445
56 34.609903 0.609903 9.758454 9.000000
56
912 563.646998 0.646998 10.351963 10.000000
912
564 348.571170 0.571170 9.138714 9.000000
564
 Step 1: Square the value of the key
 Step 2: Extract middle R digits
 The value of R depends on the size of the hash table
HASH FUNCTION (MID SQUARE)
 Key=116
 Square=13456
 For R=1, Hash value=4
 For R=2, Hash value=34 or 45
 For R=3, Hash value=345
 R=1 if Hash Table size is 10 then 0-9
 R=2 if Hash Table size is 100 then 0-99
 R=3 if Hash Table size is 1000 then 0-999
 ……
 The Truncation Method truncates a part of the given keys,
depending upon the size of the hash table.
HASH FUNCTION (TRUNCATION)
 R=1 if Hash Table size is 10 then 0-9
 R=2 if Hash Table size is 100 then 0-99
 R=3 if Hash Table size is 1000 then 0-999
 ……
 Key=2643
 For R=1, Hash value=2 or 3
 For R=2, Hash value=26 or 43
 For R=3, Hash value=264 or 643
 Any part of the key can be truncated
 It should be Consistent across all keys
 The folding method for constructing hash functions begins by
dividing the item into equal-size pieces (the last piece may
not be of equal size).
 These pieces are then added together to give the resulting
hash value.
HASH FUNCTION (FOLDING)
 Key= 4357821235
 4+3+5+7+8+2+1+2+3+5 = 40
 Hash table size = 10 [0-9] Hash key = 40%10=0
 Hash table size = 100 [0-99] Hash key = 40%100=40
HASH FUNCTION (FOLDING)
 Key= 4357821235
 43+57+82+12+35 = 229
 Key= 4357821235
 435+782+123+5 = 1345
 Hash table size = 10 [0-9] Hash key = 229%10=9
 Hash table size = 100 [0-99] Hash key = 229%100=29
 Hash table size = 1000 [0-999] Hash key = 229%1000=229
 Hash table size = 10 [0-9] Hash key = 1345%10=5
 Hash table size = 100 [0-99] Hash key = 1345%100=45
 Hash table size = 1000 [0-999] Hash key = 1345%1000=345
 Collision?????
 With Limited size of the Hash table, multiple keys map to the same
slot
COLLISION RESOLUTION
 Collision Avoidance
 Size of the Hash table must be greater than or at least equal to the
total number of keys
 Size of the Hash table, if it is a Prime Number, then there will be
comparatively less collisions
COLLISION RESOLUTION
 Open addressing
 Separate Chaining
 Bucket Hashing
COLLISION HANDLING TECHNIQUES
 Linear Probing
 Quadratic Probing
 Double Hashing
.
OPEN ADDRESSING
 Linearly probe for next available/free slot
LINEAR PROBING
0 1 2 3 4 5 6 7 8 9 10 11 12
Key : 68 68 Modulo N 3
68
N=13
Key : 100 100 Modulo N 9
100
Key : 56 56 Modulo N 4
56
Key : 35 35 Modulo N 9
35
35
Key : 41 41 Modulo N 2
41
Key : 106 106 Modulo N 2
106
106
Key : 151 151 Modulo N 8
151
10
3 4 5
Key : 49 49 Modulo N 10 11
49
49
Slot = Hash(key)
If Slot is Full then
Slot = (Hash(key) + 1) % N
If Slot is Full then
Slot = (Hash(key) + 2) % N
If Slot is Full then
Slot = (Hash(key) + 3) % N
If Slot is Full then
Slot = (Hash(key) + 4) % N
………
……..
Till there is free slot available
Else Error
LINEAR PROBING
Slot = Hash(key)
If Slot is Full then
Slot = Hash(key) + 12 % N OR Slot = Hash(key) + 1*1 % N ..... i=1
If Slot is Full then
Slot = Hash(key) + 22 % N OR Slot = Hash(key) + 2*2 % N ..... i=2
If Slot is Full then
Slot = Hash(key) + 32 % N OR Slot = Hash(key) + 3*3 % N ..... i=3
If Slot is Full then
Slot = Hash(key) + 42 % N OR Slot = Hash(key) + 4*4 % N ..... i=4
………
……..
Till there is free slot available
Else Error
QUADRATIC PROBING
 Search for i2 th Slot in ith Iteration
QUADRATIC PROBING
0 1 2 3 4 5 6 7 8 9 10 11 12
Key : 68 68 Modulo N 3
68
N=13
Key : 100 100 Modulo N 9
100
Key : 56 56 Modulo N 4
56
Key : 35 35 Modulo N 9
35
35
Key : 41 41 Modulo N 2
41
Key : 106 106 Modulo N 2
106
106
Key : 151 151 Modulo N 8
151
i=1 9 +12 % N = 10
i=1 2 +12 % N = 3
Key : 93 93 Modulo N 2
93
93
i=2 2 +22 % N = 6
i=1 2 +12 % N = 3 i=2 2 +22 % N = 6
i=3 2 +32 % N = 11
DOUBLE HASHING
 Two different hash functions
 (hash(key) + i * hash1(key)) % N
Slot = Hash(key)
If Slot is Full then
Slot = (Hash(key) + 1 * Hash1(key)) % N
If Slot is Full then
Slot = (Hash(key) + 2 * Hash1(key)) % N
If Slot is Full then
Slot = (Hash(key) + 3 * Hash1(key)) % N
………
……..
Till there is free slot available
Else Error
 Hash(): Division Hash1(): Multiplication
0 1 2 3 4 5 6 7 8 9 10 11 12
Key : 68 68 Modulo N 3
N=13
Key : 94 94 Modulo N 3
Key : 29 29 Modulo N 3
i=1 3 +1*1 % N = 4
DOUBLE HASHING
68
94
94
i=1 3 +1*11 % N = 1
29
29
Key : 120 120 Modulo N 3 i=1 3 +1*2 % N = 5
120
120
 Linear probing is simple but starts forming clusters in initial
stages only
 Double hashing and Quadratic probing create relatively
uniform distribution of keys but are having complex
calculations
ANALYSIS OF OPEN ADDRESSING
TECHNIQUES
 The M slots of the hash table are divided into B buckets, with
each bucket consisting of M/B slots.
 The hash function assigns each record to the first slot within
one of the buckets. If this slot is already occupied, then the
bucket slots are searched sequentially until an open slot is
found.
 If a bucket is entirely full, then the record is stored in
an overflow bucket of infinite capacity at the end of the table.
 All buckets share the same overflow bucket.
 A good implementation will use a hash function that
distributes the records evenly among the buckets so that as
few records as possible go into the overflow bucket.
BUCKET HASHING
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
Bucket 0 Bucket 1 Bucket 2 Bucket 3 Bucket 4
BUCKET HASHING
Key : 60 Bucket : 0 Slot : 0
60
Key : 132 Bucket : 2 Slot : 8
132
Key : 820 Bucket : 0 Slot : 1
820
Key : 654 Bucket : 4 Slot : 16
654
Key : 918 Bucket : 3 Slot : 12
918
Key : 80 Bucket : 0 Slot : 2
80
Key : 96 Bucket : 1 Slot : 4
96
Key : 321 Bucket : 1 Slot : 5
Key : 111 Bucket : 1 Slot : 6
321 111
…. ….
Key : 645 Bucket : 0 Slot : 3
Key : 40 Bucket : 0 Slot : ?
Key : 31 Bucket : 1 Slot : 7
Key : 51 Bucket : 1 Slot : ?
645 31
40 51
Overflow Bucket
 Creates a linked list to the slot for which collision occurs.
 The new key is then inserted in the linked list.
 These linked lists to the slots appear like chains.
SEPARATE CHAINING
 No idea about the number of keys
SEPARATE CHAINING
0 1 2 3 4 5 6 7 8 9
N=10
25
25
113
113 153
153
79
79
73
73
165
165
NULL
NULL
NULL
NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL
NULL
NULL
NULL

More Related Content

What's hot

Skip list vinay khimsuriya_200430723005
Skip list vinay khimsuriya_200430723005Skip list vinay khimsuriya_200430723005
Skip list vinay khimsuriya_200430723005vinaykhimsuriya1
 
It elective cs366 barizo radix.docx
It elective cs366 barizo radix.docxIt elective cs366 barizo radix.docx
It elective cs366 barizo radix.docxChristianBarizo
 
linked lists in data structures
linked lists in data structureslinked lists in data structures
linked lists in data structuresDurgaDeviCbit
 
Ppt on Linked list,stack,queue
Ppt on Linked list,stack,queuePpt on Linked list,stack,queue
Ppt on Linked list,stack,queueSrajan Shukla
 
Stack Data Structure
Stack Data StructureStack Data Structure
Stack Data StructureRabin BK
 
Sequential & binary, linear search
Sequential & binary, linear searchSequential & binary, linear search
Sequential & binary, linear searchmontazur420
 
BCA DATA STRUCTURES SEARCHING AND SORTING MRS.SOWMYA JYOTHI
BCA DATA STRUCTURES SEARCHING AND SORTING MRS.SOWMYA JYOTHIBCA DATA STRUCTURES SEARCHING AND SORTING MRS.SOWMYA JYOTHI
BCA DATA STRUCTURES SEARCHING AND SORTING MRS.SOWMYA JYOTHISowmya Jyothi
 
Java Stack Data Structure.pptx
Java Stack Data Structure.pptxJava Stack Data Structure.pptx
Java Stack Data Structure.pptxvishal choudhary
 
Introduction to data structure
Introduction to data structure Introduction to data structure
Introduction to data structure NUPOORAWSARMOL
 
Linked list
Linked listLinked list
Linked listVONI
 
Selection sort(sorting algorithm in data structure) and its time complexity
Selection sort(sorting algorithm in data structure) and its time complexitySelection sort(sorting algorithm in data structure) and its time complexity
Selection sort(sorting algorithm in data structure) and its time complexityComputer_ at_home
 

What's hot (20)

Skip list vinay khimsuriya_200430723005
Skip list vinay khimsuriya_200430723005Skip list vinay khimsuriya_200430723005
Skip list vinay khimsuriya_200430723005
 
It elective cs366 barizo radix.docx
It elective cs366 barizo radix.docxIt elective cs366 barizo radix.docx
It elective cs366 barizo radix.docx
 
Queue
QueueQueue
Queue
 
linked lists in data structures
linked lists in data structureslinked lists in data structures
linked lists in data structures
 
Queues in C++
Queues in C++Queues in C++
Queues in C++
 
Ppt on Linked list,stack,queue
Ppt on Linked list,stack,queuePpt on Linked list,stack,queue
Ppt on Linked list,stack,queue
 
Stack Data Structure
Stack Data StructureStack Data Structure
Stack Data Structure
 
Sequential & binary, linear search
Sequential & binary, linear searchSequential & binary, linear search
Sequential & binary, linear search
 
Selection sort algorithm presentation, selection sort example using power point
Selection sort algorithm presentation, selection sort example using power point Selection sort algorithm presentation, selection sort example using power point
Selection sort algorithm presentation, selection sort example using power point
 
Singly & Circular Linked list
Singly & Circular Linked listSingly & Circular Linked list
Singly & Circular Linked list
 
Linked list
Linked listLinked list
Linked list
 
BCA DATA STRUCTURES SEARCHING AND SORTING MRS.SOWMYA JYOTHI
BCA DATA STRUCTURES SEARCHING AND SORTING MRS.SOWMYA JYOTHIBCA DATA STRUCTURES SEARCHING AND SORTING MRS.SOWMYA JYOTHI
BCA DATA STRUCTURES SEARCHING AND SORTING MRS.SOWMYA JYOTHI
 
Java Stack Data Structure.pptx
Java Stack Data Structure.pptxJava Stack Data Structure.pptx
Java Stack Data Structure.pptx
 
Introduction to data structure
Introduction to data structure Introduction to data structure
Introduction to data structure
 
Linked list
Linked listLinked list
Linked list
 
Queue
QueueQueue
Queue
 
Queue Data Structure
Queue Data StructureQueue Data Structure
Queue Data Structure
 
Selection sort(sorting algorithm in data structure) and its time complexity
Selection sort(sorting algorithm in data structure) and its time complexitySelection sort(sorting algorithm in data structure) and its time complexity
Selection sort(sorting algorithm in data structure) and its time complexity
 
Data Structures Chapter-4
Data Structures Chapter-4Data Structures Chapter-4
Data Structures Chapter-4
 
Linked list
Linked listLinked list
Linked list
 

Similar to Sorting and Searching Methods

Insert Sort & Merge Sort Using C Programming
Insert Sort & Merge Sort Using C ProgrammingInsert Sort & Merge Sort Using C Programming
Insert Sort & Merge Sort Using C Programmingchandankumar364348
 
Sorting and Hashing Algorithm full pdfs.
Sorting and Hashing Algorithm full pdfs.Sorting and Hashing Algorithm full pdfs.
Sorting and Hashing Algorithm full pdfs.NikhilSoni177492
 
Introduction to Algorithms
Introduction to AlgorithmsIntroduction to Algorithms
Introduction to Algorithmspppepito86
 
search_sort Search sortSearch sortSearch sortSearch sort
search_sort Search sortSearch sortSearch sortSearch sortsearch_sort Search sortSearch sortSearch sortSearch sort
search_sort Search sortSearch sortSearch sortSearch sortShanmuganathan C
 
Decimal Long Double Double Double. Represents double-precision floating-point...
Decimal Long Double Double Double. Represents double-precision floating-point...Decimal Long Double Double Double. Represents double-precision floating-point...
Decimal Long Double Double Double. Represents double-precision floating-point...Anwar Patel
 
lecture7.ppt
lecture7.pptlecture7.ppt
lecture7.pptEdFeranil
 
UNIT I_PSPP - Illustrative Problems (1).pptx
UNIT I_PSPP - Illustrative Problems (1).pptxUNIT I_PSPP - Illustrative Problems (1).pptx
UNIT I_PSPP - Illustrative Problems (1).pptxRSathyaPriyaCSEKIOT
 
sorting and searching.pptx
sorting and searching.pptxsorting and searching.pptx
sorting and searching.pptxParagAhir1
 
Chapter 11 - Sorting and Searching
Chapter 11 - Sorting and SearchingChapter 11 - Sorting and Searching
Chapter 11 - Sorting and SearchingEduardo Bergavera
 
Data Structures - Lecture 8 [Sorting Algorithms]
Data Structures - Lecture 8 [Sorting Algorithms]Data Structures - Lecture 8 [Sorting Algorithms]
Data Structures - Lecture 8 [Sorting Algorithms]Muhammad Hammad Waseem
 
14-sorting (3).ppt
14-sorting (3).ppt14-sorting (3).ppt
14-sorting (3).pptyasser3omr
 
2.2 add real numbers day 1-2
2.2 add real numbers   day 1-22.2 add real numbers   day 1-2
2.2 add real numbers day 1-2bweldon
 

Similar to Sorting and Searching Methods (20)

Insert Sort & Merge Sort Using C Programming
Insert Sort & Merge Sort Using C ProgrammingInsert Sort & Merge Sort Using C Programming
Insert Sort & Merge Sort Using C Programming
 
Sorting
SortingSorting
Sorting
 
Sorting
SortingSorting
Sorting
 
Sorting and Hashing Algorithm full pdfs.
Sorting and Hashing Algorithm full pdfs.Sorting and Hashing Algorithm full pdfs.
Sorting and Hashing Algorithm full pdfs.
 
Introduction to Algorithms
Introduction to AlgorithmsIntroduction to Algorithms
Introduction to Algorithms
 
search_sort Search sortSearch sortSearch sortSearch sort
search_sort Search sortSearch sortSearch sortSearch sortsearch_sort Search sortSearch sortSearch sortSearch sort
search_sort Search sortSearch sortSearch sortSearch sort
 
Sortings .pptx
Sortings .pptxSortings .pptx
Sortings .pptx
 
Decimal Long Double Double Double. Represents double-precision floating-point...
Decimal Long Double Double Double. Represents double-precision floating-point...Decimal Long Double Double Double. Represents double-precision floating-point...
Decimal Long Double Double Double. Represents double-precision floating-point...
 
lecture7.ppt
lecture7.pptlecture7.ppt
lecture7.ppt
 
Arrays
ArraysArrays
Arrays
 
UNIT I_PSPP - Illustrative Problems (1).pptx
UNIT I_PSPP - Illustrative Problems (1).pptxUNIT I_PSPP - Illustrative Problems (1).pptx
UNIT I_PSPP - Illustrative Problems (1).pptx
 
sorting and searching.pptx
sorting and searching.pptxsorting and searching.pptx
sorting and searching.pptx
 
Chapter 11 - Sorting and Searching
Chapter 11 - Sorting and SearchingChapter 11 - Sorting and Searching
Chapter 11 - Sorting and Searching
 
Aslam
AslamAslam
Aslam
 
Data Structures - Lecture 8 [Sorting Algorithms]
Data Structures - Lecture 8 [Sorting Algorithms]Data Structures - Lecture 8 [Sorting Algorithms]
Data Structures - Lecture 8 [Sorting Algorithms]
 
14-sorting.ppt
14-sorting.ppt14-sorting.ppt
14-sorting.ppt
 
14-sorting (3).ppt
14-sorting (3).ppt14-sorting (3).ppt
14-sorting (3).ppt
 
14-sorting.ppt
14-sorting.ppt14-sorting.ppt
14-sorting.ppt
 
14-sorting.ppt
14-sorting.ppt14-sorting.ppt
14-sorting.ppt
 
2.2 add real numbers day 1-2
2.2 add real numbers   day 1-22.2 add real numbers   day 1-2
2.2 add real numbers day 1-2
 

Recently uploaded

Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Dr.Costas Sachpazis
 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSAPPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSKurinjimalarL3
 
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)Suman Mia
 
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝soniya singh
 
the ladakh protest in leh ladakh 2024 sonam wangchuk.pptx
the ladakh protest in leh ladakh 2024 sonam wangchuk.pptxthe ladakh protest in leh ladakh 2024 sonam wangchuk.pptx
the ladakh protest in leh ladakh 2024 sonam wangchuk.pptxhumanexperienceaaa
 
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxDecoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxJoão Esperancinha
 
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...Call Girls in Nagpur High Profile
 
Processing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxProcessing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxpranjaldaimarysona
 
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130Suhani Kapoor
 
(TARA) Talegaon Dabhade Call Girls Just Call 7001035870 [ Cash on Delivery ] ...
(TARA) Talegaon Dabhade Call Girls Just Call 7001035870 [ Cash on Delivery ] ...(TARA) Talegaon Dabhade Call Girls Just Call 7001035870 [ Cash on Delivery ] ...
(TARA) Talegaon Dabhade Call Girls Just Call 7001035870 [ Cash on Delivery ] ...ranjana rawat
 
Coefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxCoefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxAsutosh Ranjan
 
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...ranjana rawat
 
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Serviceranjana rawat
 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVRajaP95
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Christo Ananth
 
Microscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxMicroscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxpurnimasatapathy1234
 
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service NashikCall Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service NashikCall Girls in Nagpur High Profile
 

Recently uploaded (20)

Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSAPPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
 
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)
 
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
 
the ladakh protest in leh ladakh 2024 sonam wangchuk.pptx
the ladakh protest in leh ladakh 2024 sonam wangchuk.pptxthe ladakh protest in leh ladakh 2024 sonam wangchuk.pptx
the ladakh protest in leh ladakh 2024 sonam wangchuk.pptx
 
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxDecoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
 
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
 
Processing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxProcessing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptx
 
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
 
(TARA) Talegaon Dabhade Call Girls Just Call 7001035870 [ Cash on Delivery ] ...
(TARA) Talegaon Dabhade Call Girls Just Call 7001035870 [ Cash on Delivery ] ...(TARA) Talegaon Dabhade Call Girls Just Call 7001035870 [ Cash on Delivery ] ...
(TARA) Talegaon Dabhade Call Girls Just Call 7001035870 [ Cash on Delivery ] ...
 
Coefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxCoefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptx
 
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
 
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
 
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCRCall Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
 
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
 
Microscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxMicroscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptx
 
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service NashikCall Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
 

Sorting and Searching Methods

  • 7. select * from employee where city=‘Mumbai’ order by salary; APPLICATIONS order by
  • 9. 98 87 64 75 25 12 45 33 BUBBLE SORT Pass 1 i =1 j=0 87 98 64 75 25 12 45 33 j=1 87 64 98 75 25 12 45 33 j=2 87 64 75 98 25 12 45 33 j=3 SIZE = 8
  • 10. 87 64 75 25 98 12 45 33 BUBBLE SORT 87 64 75 25 12 98 45 33 j=5 87 64 75 25 12 45 98 33 j=6 87 64 75 25 12 45 33 98 j=4 SIZE = 8 Pass 1 i =1
  • 11. 87 64 75 25 12 45 33 98 BUBBLE SORT Pass 2 i = 2 SIZE = 8 64 87 75 25 12 45 33 98 j=0 j=1 64 75 87 25 12 45 33 98 j=2 64 75 25 87 12 45 33 98 j=3
  • 12. BUBBLE SORT SIZE = 8 64 75 25 12 87 45 33 98 j=4 64 75 25 12 45 87 33 98 j=5 64 75 25 12 45 33 87 98 Pass 2 i = 2
  • 13. 64 75 25 12 45 33 87 98 BUBBLE SORT SIZE = 8 64 75 25 12 45 33 87 98 j=0 j=1 64 25 75 12 45 33 87 98 j=2 64 25 12 75 45 33 87 98 j=3 Pass 3 i = 3
  • 14. BUBBLE SORT SIZE = 8 64 25 12 45 75 33 87 98 j=4 64 25 12 45 33 75 87 98 Pass 3 i = 3
  • 15. 64 25 12 45 33 75 87 98 BUBBLE SORT Pass 4 i=4 SIZE = 8 25 64 12 45 33 75 87 98 j=0 j=1 25 12 64 45 33 75 87 98 j=2 25 12 45 64 33 75 87 98 j=3
  • 16. BUBBLE SORT SIZE = 8 25 12 45 33 64 75 87 98 Pass 4 i=4
  • 17. 25 12 45 33 64 75 87 98 BUBBLE SORT Pass 5 i=5 SIZE = 8 12 25 45 33 64 75 87 98 j=0 j=1 12 25 45 33 64 75 87 98 j=2 12 25 33 45 64 75 87 98
  • 18. 12 25 33 45 64 75 87 98 BUBBLE SORT Pass 6 i=6 SIZE = 8 12 25 33 45 64 75 87 98 j=0 j=1 12 25 33 45 64 75 87 98
  • 19. 12 25 33 45 64 75 87 98 BUBBLE SORT Pass 7 i=7 SIZE = 8 12 25 33 45 64 75 87 98 j=0
  • 20.  Adjacent elements are compared  For N number of elements (8 elements)  N-1 number of passes (7 elements)  In Pass 1, Number of comparisons N-1 (7)  In Pass 2, Number of comparisons N-2 (6)  In Pass 3, Number of comparisons N-3 (5)  ……  In Pass N-3 (5), Number of comparisons 3  In Pass N-2 (6), Number of comparisons 2  In Pass N-1 (7), Number of comparisons 1 BUBBLE SORT  Total Number of comparisons for 8 elements  7+6+5+4+3+2+1 = (8 * 7)/2  Total Number of comparisons for N elements  (N * N-1)/2
  • 22. 98 87 64 75 25 12 45 33 SELECTION SORT Pass 1 j=7 SIZE = 8 max = 98 maxi = 0 j=1 j=2 j=3 j=4 j=5 j=6 33 87 64 75 25 12 45 98 i =7
  • 23. 33 87 64 75 25 12 45 98 SELECTION SORT Pass 2 SIZE = 8 max = 33 maxi = 0 j=1 j=2 j=3 j=4 j=5 j=6 33 45 64 75 25 12 87 98 i = 6 max = 87 maxi = 1
  • 24. 33 45 64 75 25 12 87 98 SELECTION SORT Pass 3 SIZE = 8 max = 33 maxi = 0 j=1 j=2 j=3 j=4 j=5 33 45 64 12 25 75 87 98 i = 5 max = 45 maxi = 1 max = 64 maxi = 2 max = 75 maxi = 3
  • 25. 33 45 64 12 25 75 87 98 SELECTION SORT Pass 4 SIZE = 8 max = 33 maxi = 0 j=1 j=2 j=3 j=4 33 45 25 12 64 75 87 98 i = 4 max = 45 maxi = 1 max = 64 maxi = 2
  • 26. 33 45 25 12 64 75 87 98 SELECTION SORT Pass 5 SIZE = 8 max = 33 maxi = 0 j=1 j=2 j=3 33 12 25 45 64 75 87 98 i = 3 max = 45 maxi = 1
  • 27. 33 12 25 45 64 75 87 98 SELECTION SORT Pass 6 SIZE = 8 max = 33 maxi = 0 j=1 j=2 25 12 33 45 64 75 87 98 i = 2
  • 28. 25 12 33 45 64 75 87 98 SELECTION SORT Pass 7 SIZE = 8 max = 25 maxi = 0 j=1 12 25 33 45 64 75 87 98 i = 1
  • 29.  Select the maximum element and place it at the end  max – is the Max element seen so far  maxi – is the index of the same  In Pass 1, Number of comparisons N-1 (7)  In Pass 2, Number of comparisons N-2 (6)  In Pass 3, Number of comparisons N-3 (5)  ……  In Pass N-3 (5), Number of comparisons 3  In Pass N-2 (6), Number of comparisons 2  In Pass N-1 (7), Number of comparisons 1 SELECTION SORT  Total Number of comparisons for 8 elements  7+6+5+4+3+2+1 = (8 * 7)/2  Total Number of comparisons for N elements  (N * N-1)/2
  • 31.  Select the minimum element and place it at the beginning  min – is the Min element seen so far  mini – is the index of the same  In Pass 1, Number of comparisons N-1 (7)  In Pass 2, Number of comparisons N-2 (6)  In Pass 3, Number of comparisons N-3 (5)  ……  In Pass N-3 (5), Number of comparisons 3  In Pass N-2 (6), Number of comparisons 2  In Pass N-1 (7), Number of comparisons 1 SELECTION SORT
  • 33. INSERTION SORT 98 87 64 75 25 12 45 33 Pass 1 i=1 SIZE = 8 y = 87 j=0 98 87 64 75 25 12 45 33 i=1 j=0 98 98 64 75 25 12 45 33 i=1 j=-1 87 98 64 75 25 12 45 33 i=1 j=0
  • 34. INSERTION SORT 87 98 64 75 25 12 45 33 Pass 2 i=2 SIZE = 8 y = 64 j=1 87 98 98 75 25 12 45 33 i=2 j=0 87 87 98 75 25 12 45 33 i=2 j=-1 64 87 98 75 25 12 45 33 i=2 j=0
  • 35. INSERTION SORT 64 87 98 75 25 12 45 33 Pass 3 i=3 SIZE = 8 y = 75 j=2 64 87 98 98 25 12 45 33 i=3 j=1 64 87 87 98 25 12 45 33 i=3 j=0 64 75 87 98 25 12 45 33 i=3 j=1
  • 36. INSERTION SORT 64 75 87 98 25 12 45 33 Pass 4 i=4 SIZE = 8 y = 25 j=3 64 75 87 98 98 12 45 33 i=4 j=2 64 75 87 87 98 12 45 33 i=4 j=1 64 75 75 87 98 12 45 33 i=4 j=0
  • 37. INSERTION SORT Pass 4 SIZE = 8 y = 25 64 64 75 87 98 12 45 33 i=4 j=-1 25 64 75 87 98 12 45 33 i=4 j=0
  • 38. INSERTION SORT Pass 5 SIZE = 8 y = 12 25 64 75 87 98 12 45 33 i=5 j=4 25 64 75 87 98 98 45 33 i=5 j=3 25 64 75 87 87 98 45 33 i=5 j=2 25 64 75 75 87 98 45 33 i=5 j=1
  • 39. 25 64 64 75 87 98 45 33 i=5 j=0 INSERTION SORT 25 25 64 75 87 98 45 33 i=5 j=-1 12 25 64 75 87 98 45 33 i=5 j=0 Pass 5 SIZE = 8 y = 12
  • 40. Pass 6 SIZE = 8 y = 45 12 25 64 75 87 98 45 33 i=6 j=5 INSERTION SORT 12 25 64 75 87 98 98 33 i=6 j=4 12 25 64 75 87 87 98 33 i=6 j=3 12 25 64 75 75 87 98 33 i=6 j=2
  • 41. Pass 6 SIZE = 8 y = 45 INSERTION SORT 12 25 64 64 75 87 98 33 i=6 j=1 12 25 45 64 75 87 98 33 i=6 j=2
  • 42. INSERTION SORT 12 25 45 64 75 87 98 33 i=7 j=6 Pass 7 SIZE = 8 y = 33 12 25 45 64 75 87 98 98 i=7 j=5 12 25 45 64 75 87 87 98 i=7 j=4 12 25 45 64 75 75 87 98 i=7 j=3
  • 43. INSERTION SORT 12 25 45 64 64 75 87 98 i=7 j=2 Pass 7 SIZE = 8 y = 33 12 25 45 45 64 75 87 98 i=7 j=1 12 25 33 45 64 75 87 98 i=7 j=2
  • 45.  Array is divided into sorted and unsorted part  Pick the first element from the unsorted array and find its place it in the sorted array and place it there  In Pass 1, Number of comparisons 1  In Pass 2, Number of comparisons 2  In Pass 3, Number of comparisons 3  ……  In Pass N-3 (5), Number of comparisons N-3 (5)  In Pass N-2 (6), Number of comparisons N-2 (6)  In Pass N-1 (7), Number of comparisons N-3 (7)  Total Number of comparisons for 8 elements  1+2+3+4+5+6+7 = (8 * 7)/2  Total Number of comparisons for N elements  (N * N-1)/2 INSERTION SORT
  • 46.  Searching is the process used to find the location of a target among a list of objects. SEARCHING
  • 50.  For 1 elements, No. of comparisons = 1  For 2 elements, No. of comparisons = 2  …..  For N-1 elements, No. of comparisons = N-1  For N elements, No. of comparisons = N LINEAR SEARCH
  • 51. int el,i; printf("nEnter element to be searched? "); scanf("%d",&el); for(i=0;i<SIZE;i++) { if(a[i]==el) return i; } return -1; LINEAR SEARCH
  • 53.  Input array must be a sorted array with elements in ascending order BINARY SEARCH  For 16 elements  1 Comparison will divide the array in two parts of size 8  2 Comparisons will divide the array in four parts of size 4  3 Comparisons will divide the array in eight parts of size 2  4 Comparisons will divide the array in sixteen parts of size 1  For 32 elements  1 Comparison will divide the array in two parts of size 16  2 Comparisons will divide the array in four parts of size 8  3 Comparisons will divide the array in eight parts of size 4  4 Comparisons will divide the array in sixteen parts of size 2  5 Comparisons will divide the array in thirty two parts of size 1
  • 54.  8 elements -> 3 comparisons  16 elements -> 4 comparisons  32 elements -> 5 comparisons  64 elements -> 6 comparisons  ……  In general for 2N elements there will be N comparisons BINARY SEARCH  N elements -> Log2N comparisons
  • 56.  In Linear search, search complexity is O(n)  In Binary Search, search complexity is O(log2n) HASHING Hashing makes it possible in O(1)
  • 57.  A hash table is a collection of elements which are stored in such a way as to make it easy to find them later.  Each position of the hash table, often called a slot, can hold an element and is named by an integer value starting at 0.  The mapping between an element and the slot where that element belongs in the hash table is called the hash function. HASH FUNCTION
  • 58. Division method Multiplication method Mid-square method Folding method Truncation method HASH FUNCTION
  • 59. HASH FUNCTION (DIVISION) 0 1 2 3 4 5 6 7 8 9 10 11 Element: 68 68 Modulo N N = 12 8 68 Element: 72 72 Modulo N 0 72 Element: 122 122 Modulo N 2 122 Element: 46 46 Modulo N 10 46 Element: 15 15 Modulo N 3 15 Element: 50 50 Modulo N 2 50
  • 60.  The value of N is chosen to be a Prime Number  Given a collection of items, a hash function that maps each item into a unique slot is referred to as a perfect hash function. HASH FUNCTION (DIVISION)
  • 61.  Step 1: Choose a constant A such that 0 < A < 1  Step 2: Multiply the element k by A.  Step 3: Extract the fractional part of k*A  Step 4: Multiply Extracted fractional by the size of hash table  The multiplication method works with any value of A, but Knuth has suggested that the best choice of A is 0.6180339887 HASH FUNCTION (MULTIPLICATION)  The value of N is chosen to be some power of 2
  • 62. HASH FUNCTION (MULTIPLICATION) N = 16 23 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 key key*A fraction (key*A) Fraction*N floor 23 14.214782 0.214782 3.436508 3.000000 677 418.409010 0.409010 6.544166 6.000000 677 1234 762.653942 0.653942 10.463073 10.000000 1234 A = 0.6180339887 445 275.025125 0.025125 0.402000 0.000000 445 56 34.609903 0.609903 9.758454 9.000000 56 912 563.646998 0.646998 10.351963 10.000000 912 564 348.571170 0.571170 9.138714 9.000000 564
  • 63.  Step 1: Square the value of the key  Step 2: Extract middle R digits  The value of R depends on the size of the hash table HASH FUNCTION (MID SQUARE)  Key=116  Square=13456  For R=1, Hash value=4  For R=2, Hash value=34 or 45  For R=3, Hash value=345  R=1 if Hash Table size is 10 then 0-9  R=2 if Hash Table size is 100 then 0-99  R=3 if Hash Table size is 1000 then 0-999  ……
  • 64.  The Truncation Method truncates a part of the given keys, depending upon the size of the hash table. HASH FUNCTION (TRUNCATION)  R=1 if Hash Table size is 10 then 0-9  R=2 if Hash Table size is 100 then 0-99  R=3 if Hash Table size is 1000 then 0-999  ……  Key=2643  For R=1, Hash value=2 or 3  For R=2, Hash value=26 or 43  For R=3, Hash value=264 or 643  Any part of the key can be truncated  It should be Consistent across all keys
  • 65.  The folding method for constructing hash functions begins by dividing the item into equal-size pieces (the last piece may not be of equal size).  These pieces are then added together to give the resulting hash value. HASH FUNCTION (FOLDING)  Key= 4357821235  4+3+5+7+8+2+1+2+3+5 = 40  Hash table size = 10 [0-9] Hash key = 40%10=0  Hash table size = 100 [0-99] Hash key = 40%100=40
  • 66. HASH FUNCTION (FOLDING)  Key= 4357821235  43+57+82+12+35 = 229  Key= 4357821235  435+782+123+5 = 1345  Hash table size = 10 [0-9] Hash key = 229%10=9  Hash table size = 100 [0-99] Hash key = 229%100=29  Hash table size = 1000 [0-999] Hash key = 229%1000=229  Hash table size = 10 [0-9] Hash key = 1345%10=5  Hash table size = 100 [0-99] Hash key = 1345%100=45  Hash table size = 1000 [0-999] Hash key = 1345%1000=345
  • 67.  Collision?????  With Limited size of the Hash table, multiple keys map to the same slot COLLISION RESOLUTION  Collision Avoidance  Size of the Hash table must be greater than or at least equal to the total number of keys  Size of the Hash table, if it is a Prime Number, then there will be comparatively less collisions
  • 69.  Open addressing  Separate Chaining  Bucket Hashing COLLISION HANDLING TECHNIQUES
  • 70.  Linear Probing  Quadratic Probing  Double Hashing . OPEN ADDRESSING
  • 71.  Linearly probe for next available/free slot LINEAR PROBING 0 1 2 3 4 5 6 7 8 9 10 11 12 Key : 68 68 Modulo N 3 68 N=13 Key : 100 100 Modulo N 9 100 Key : 56 56 Modulo N 4 56 Key : 35 35 Modulo N 9 35 35 Key : 41 41 Modulo N 2 41 Key : 106 106 Modulo N 2 106 106 Key : 151 151 Modulo N 8 151 10 3 4 5 Key : 49 49 Modulo N 10 11 49 49
  • 72. Slot = Hash(key) If Slot is Full then Slot = (Hash(key) + 1) % N If Slot is Full then Slot = (Hash(key) + 2) % N If Slot is Full then Slot = (Hash(key) + 3) % N If Slot is Full then Slot = (Hash(key) + 4) % N ……… …….. Till there is free slot available Else Error LINEAR PROBING
  • 73. Slot = Hash(key) If Slot is Full then Slot = Hash(key) + 12 % N OR Slot = Hash(key) + 1*1 % N ..... i=1 If Slot is Full then Slot = Hash(key) + 22 % N OR Slot = Hash(key) + 2*2 % N ..... i=2 If Slot is Full then Slot = Hash(key) + 32 % N OR Slot = Hash(key) + 3*3 % N ..... i=3 If Slot is Full then Slot = Hash(key) + 42 % N OR Slot = Hash(key) + 4*4 % N ..... i=4 ……… …….. Till there is free slot available Else Error QUADRATIC PROBING
  • 74.  Search for i2 th Slot in ith Iteration QUADRATIC PROBING 0 1 2 3 4 5 6 7 8 9 10 11 12 Key : 68 68 Modulo N 3 68 N=13 Key : 100 100 Modulo N 9 100 Key : 56 56 Modulo N 4 56 Key : 35 35 Modulo N 9 35 35 Key : 41 41 Modulo N 2 41 Key : 106 106 Modulo N 2 106 106 Key : 151 151 Modulo N 8 151 i=1 9 +12 % N = 10 i=1 2 +12 % N = 3 Key : 93 93 Modulo N 2 93 93 i=2 2 +22 % N = 6 i=1 2 +12 % N = 3 i=2 2 +22 % N = 6 i=3 2 +32 % N = 11
  • 75. DOUBLE HASHING  Two different hash functions  (hash(key) + i * hash1(key)) % N Slot = Hash(key) If Slot is Full then Slot = (Hash(key) + 1 * Hash1(key)) % N If Slot is Full then Slot = (Hash(key) + 2 * Hash1(key)) % N If Slot is Full then Slot = (Hash(key) + 3 * Hash1(key)) % N ……… …….. Till there is free slot available Else Error
  • 76.  Hash(): Division Hash1(): Multiplication 0 1 2 3 4 5 6 7 8 9 10 11 12 Key : 68 68 Modulo N 3 N=13 Key : 94 94 Modulo N 3 Key : 29 29 Modulo N 3 i=1 3 +1*1 % N = 4 DOUBLE HASHING 68 94 94 i=1 3 +1*11 % N = 1 29 29 Key : 120 120 Modulo N 3 i=1 3 +1*2 % N = 5 120 120
  • 77.  Linear probing is simple but starts forming clusters in initial stages only  Double hashing and Quadratic probing create relatively uniform distribution of keys but are having complex calculations ANALYSIS OF OPEN ADDRESSING TECHNIQUES
  • 78.  The M slots of the hash table are divided into B buckets, with each bucket consisting of M/B slots.  The hash function assigns each record to the first slot within one of the buckets. If this slot is already occupied, then the bucket slots are searched sequentially until an open slot is found.  If a bucket is entirely full, then the record is stored in an overflow bucket of infinite capacity at the end of the table.  All buckets share the same overflow bucket.  A good implementation will use a hash function that distributes the records evenly among the buckets so that as few records as possible go into the overflow bucket. BUCKET HASHING
  • 79. 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 Bucket 0 Bucket 1 Bucket 2 Bucket 3 Bucket 4 BUCKET HASHING Key : 60 Bucket : 0 Slot : 0 60 Key : 132 Bucket : 2 Slot : 8 132 Key : 820 Bucket : 0 Slot : 1 820 Key : 654 Bucket : 4 Slot : 16 654 Key : 918 Bucket : 3 Slot : 12 918 Key : 80 Bucket : 0 Slot : 2 80 Key : 96 Bucket : 1 Slot : 4 96 Key : 321 Bucket : 1 Slot : 5 Key : 111 Bucket : 1 Slot : 6 321 111 …. …. Key : 645 Bucket : 0 Slot : 3 Key : 40 Bucket : 0 Slot : ? Key : 31 Bucket : 1 Slot : 7 Key : 51 Bucket : 1 Slot : ? 645 31 40 51 Overflow Bucket
  • 80.  Creates a linked list to the slot for which collision occurs.  The new key is then inserted in the linked list.  These linked lists to the slots appear like chains. SEPARATE CHAINING  No idea about the number of keys
  • 81. SEPARATE CHAINING 0 1 2 3 4 5 6 7 8 9 N=10 25 25 113 113 153 153 79 79 73 73 165 165 NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL