SlideShare une entreprise Scribd logo
1  sur  24
ADT(Abstract Data type) :- It represent the data and
set of operation on data.
Data:-
1. Array Space
2. Size
3. Length
Operation:-
1. Display()
2. Add(x)/append(x)
3. Insert(index x)
4. Delete(index)
5. Search(x)
6. Get(index)
7. Set(index x)
8. Max()/Min()
9. Reverse()
10. Shift()/rotate
2. Add(x)/append(x):-
8 3 7 12 6 9 10
A
0 1 2 3 4 5 6 7 8 9
Size = 10
Length = 6 7
A[length] = x;
Length ++;
f(n) = 2
f(n) = 2no
O(no) = O(1)
2. insert:-
8 3 7 12 6 9 10
A
0 1 2 3 4 5 6 7 8 9
Insert(4,15)
8 3 7 12 6 9 10
A
0 1 2 3 4 5 6 7 8 9
15
8 3 7 12 15 6 9 10
A
0 1 2 3 4 5 6 7 8 9
After Insertion
4. Delete(index) :-
8 3 7 15 6 9 10
A
0 1 2 3 4 5 6 7 8 9
8 3 7 15 6 9 10
A
0 1 2 3 4 5 6 7 8 9
Delete(3);
8 3 7 6 9 10
A
0 1 2 3 4 5 6 7 8 9
4. Delete(index) :-
Code:-
x = A[index];
If(index>=0 && index<length)
{
for(i=index ;i<length-1 ;i++)
{
A[i] = A[i+1];
}
length--;
}
4. Delete(index) :-
Linear search:-
• Start from the leftmost element of arr[] and one by
one compare x with each element of arr[]
• If x matches with an element, return the index.
• If x doesn’t match with any of elements, return -1.
• Time complexity is O(n).
Improve linear search:-
A linear search or sequential search is a method for finding an
element within a list. It sequentially checks each element of
the list until a match is found or the whole list has been
searched. It is observed that when searching for a key
element, then there is a possibility for searching the same
key element again and again.
The goal is that if the same element is searched again then
the operation must take lesser time. Therefore, in such a
case, Linear Search can be improved by using the following
two methods:
1. Transposition
2. Move to Front
Transposition:
In transposition, if the key element is found, it is swapped to the
element an index before to increase in a number of search count for
a particular key, the search operation also optimizes and keep
moving the element to the starting of the array where the searching
time complexity would be of constant time.
For Example: If the array arr[] is {2, 5, 7, 1, 6, 4, 5, 8, 3, 7} and let
the key to be searched is 4, then below are the steps:
• After searching for key 4, the element is found at index 5 of the given array
after 6 comparisons. Now after transposition, the array becomes {2, 5, 7, 1, 4,
6, 5, 8, 3, 7} i.e., the key with value 4 comes at index 4.
• Again after searching for key 4, the element is found at index 4 of the given
array after 6 comparisons. Now after transposition, the array becomes {2, 5, 7,
4, 1, 6, 5, 8, 3, 7} i.e., the key with value 4 comes at index 3.
• The above process will continue until any key reaches the front of the array if
the element to be found is not at the first index
Move to Front/Head:
In this method, if the key element is found then it is directly swapped with the
index 0, so that the next consecutive time, search operation for the same key
element is of O(1), i.e., constant time.
For Example: If the array arr[] is {2, 5, 7, 1, 6, 4, 5, 8, 3, 7} and let the key to be
searched is 4, then below are the steps:
• After searching for key 4, the element is found at index 5 of the
given array after 6 comparisons. Now after moving to front
operation, the array becomes {4, 2, 5, 7, 1, 6, 5, 8, 3, 7} i.e., the
key with value 4 comes at index 0.
• Again after searching for key 4, the element is found at index 0 of
the given array which reduces the entire’s search space.
4 8 10 15 18 23 24 27 29 33 34 37 39 41 43
Binary search :-
Size =15
Length = 15
key = 18
L H mid=[(L+H)/2]
0 14 7
0 6 3
4 6 5
4 4 4(found)
key = 34
L H mid
0 14 7
8 14 11
8 10 9
10 10 10(found)
key = 25
L H mid
0 14 7
0 6 3
4 6 5
6 6 6
7 6 X(not found)
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
Binary Search algorithm:-
Algorithm BiSearch(l,h,key)
{
while(l<=h)
{
mid = [(l+h)/2];
if(key == A[mid])
return mid;
else if(key < A[mid])
h = mid-1;
else
l = mid + 1;
}
return -1;
}
Binary search Algorithm using recursion:-
Algorithm RBinSearch(l,h,key)
{
if(l <= h)
{
mid = [(l+h)/2];
if(key == A[mid])
return mid;
else if(key < A[mid])
return RBSearch(l,mid-1,key);
else
return RBSearch(mid+1,h,key)
}
return -1;
}
Analysis of Binary Search:-
4 8 10 15 18 23 24 27 29 33 34 37 39 41 43
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
7
27
3
15
11
37
1
8
5
21
9
33
13
41
0
4
2
10
4
18
6
24
8
29
10
34
12
39
14
43
Best min – O(1)
Worst max – O(logn)
6. get(index):- to get the element of given index
get(index)
{
if(index >=0 && index<length)
return A[index];
}
7. set(index):- to set the value of element of given index
set(index,x)
{
if(index >=0 && index<length)
A[index] = x;
}
8. max() & min():-
max()
{
1-------max = A[0];
n-------for(i=1;i<length;i++)
{
n-1 ---------if(A[i]>max)
1-----------------max = A[i];
}
return max;
}
f(n) = 2n+1
O(n)
min()
{
1-------min = A[0];
N-------for(i=1;i<length;i++)
{
n-1 ----------if(A[i]<min)
1-----------------min = A[i];
}
return min;
}
f(n) = 2n+1
O(n)
Sum():-
Sum()
{
total = 0;
for(i=0;i<length;i++)
{
total = total + A[i];
}
return total;
}
avg():-
Sum()
{
total = 0;
for(i=0;i<length;i++)
{
total = total + A[i];
}
return total/n ;
}
9. reverse()
8 3 7 15 6 9 10 2 12 4
0 1 2 3 4 5 6 7 8 9
A
4 12 2 10 9 6 15 7 3 8
0 1 2 3 4 5 6 7 8 9
B
reverse
4 12 2 10 9 6 15 7 3 8
0 1 2 3 4 5 6 7 8 9
A
for(i=length-1,j=0;i>=0;i--,j++)
{
B[j] = A[i];---------------n
}
for(i=0;i<length;i++)
{
A[i] = B[i];---------------n
} 2n O(n)
9. reverse()
8 3 7 15 6 9 10 2 12 4
0 1 2 3 4 5 6 7 8 9
A
4 12 2 10 9 6 15 7 3 8
0 1 2 3 4 5 6 7 8 9
A
for(i=0,j=length-1;i<j;i++,j--)
{
temp = A[i];
A[i] = A[j];
A[j] = temp;
}
10. shift/rotate:-
8 3 7 15 6
0 1 2 3 4
A
3 7 15 6 0
0 1 2 3 4
A
8
Left shift
8 3 7 15 6
0 1 2 3 4
A
0 8 3 7 15
0 1 2 3 4
A 8 Right shift
8 3 7 15 6
0 1 2 3 4
A
Rotate:-
3 7 15 6 8
0 1 2 3 4
A
Insert An element in sorted array :-
4 8 13 16 20 25 28 33
0 1 2 3 4 5 6 7 8 9
A
Insert – 18
x = 18;
I = length-1;
while(A[i]>x)
{
A[i+1] = A[i];
}
A[i+1] = x;
Array is sorted or not :-
4 8 13 26 20 25 28 33
0 1 2 3 4 5 6 7 8 9
A
Algorithm isSorted(A,n)
{
for(i=0;i<n-1;i++)
{
if(A[i]>A[i+1])
return false;
}
return true;
}
Arrange –ve number on Left side:-
-6 3 -8 10 5 -7 -9 12 -4 2
0 1 2 3 4 5 6 7 8 9
A
i=0;
j = length-1;
While(i<j)
{
while(A[i]<0){i++;}
while(A[i]>=0){i++;}
if(i<j)
swap(A[i],A[j]);
}
-6 -4 -8 -9 -7 5 10 12 3 2
0 1 2 3 4 5 6 7 8 9
A
merging:-
Merging can be done only on sorted array.
In merging we have combine two sorted array and combine them to make a single
sorted array.
2 4 6 8 10
0 1 2 3 4
A
3 5 7 9 11
0 1 2 3 4
B
2 3 4 5 6 7 8 9 10 11
0 1 2 3 4 5 6 7 8 9
C
After merging:-

Contenu connexe

Tendances

Tendances (20)

BINARY TREE REPRESENTATION.ppt
BINARY TREE REPRESENTATION.pptBINARY TREE REPRESENTATION.ppt
BINARY TREE REPRESENTATION.ppt
 
Bfs and Dfs
Bfs and DfsBfs and Dfs
Bfs and Dfs
 
Introduction to data structure
Introduction to data structure Introduction to data structure
Introduction to data structure
 
Sorting
SortingSorting
Sorting
 
Linked List - Insertion & Deletion
Linked List - Insertion & DeletionLinked List - Insertion & Deletion
Linked List - Insertion & Deletion
 
Insertion sort
Insertion sortInsertion sort
Insertion sort
 
Binary search tree(bst)
Binary search tree(bst)Binary search tree(bst)
Binary search tree(bst)
 
Data Structures - Lecture 9 [Stack & Queue using Linked List]
 Data Structures - Lecture 9 [Stack & Queue using Linked List] Data Structures - Lecture 9 [Stack & Queue using Linked List]
Data Structures - Lecture 9 [Stack & Queue using Linked List]
 
Linear Search Presentation
Linear Search PresentationLinear Search Presentation
Linear Search Presentation
 
Searching
SearchingSearching
Searching
 
Quick sort-Data Structure
Quick sort-Data StructureQuick sort-Data Structure
Quick sort-Data Structure
 
Insertion sort
Insertion sort Insertion sort
Insertion sort
 
Data Structures - Lecture 7 [Linked List]
Data Structures - Lecture 7 [Linked List]Data Structures - Lecture 7 [Linked List]
Data Structures - Lecture 7 [Linked List]
 
Breadth first search and depth first search
Breadth first search and  depth first searchBreadth first search and  depth first search
Breadth first search and depth first search
 
Linear search-and-binary-search
Linear search-and-binary-searchLinear search-and-binary-search
Linear search-and-binary-search
 
Queue ppt
Queue pptQueue ppt
Queue ppt
 
Hashing
HashingHashing
Hashing
 
Data Structures - Searching & sorting
Data Structures - Searching & sortingData Structures - Searching & sorting
Data Structures - Searching & sorting
 
Data Structures (CS8391)
Data Structures (CS8391)Data Structures (CS8391)
Data Structures (CS8391)
 
Insertion sort bubble sort selection sort
Insertion sort bubble sort  selection sortInsertion sort bubble sort  selection sort
Insertion sort bubble sort selection sort
 

Similaire à Array ADT(Abstract Data Type)|Data Structure

An Experiment to Determine and Compare Practical Efficiency of Insertion Sort...
An Experiment to Determine and Compare Practical Efficiency of Insertion Sort...An Experiment to Determine and Compare Practical Efficiency of Insertion Sort...
An Experiment to Determine and Compare Practical Efficiency of Insertion Sort...
Tosin Amuda
 

Similaire à Array ADT(Abstract Data Type)|Data Structure (20)

DS Unit 1.pptx
DS Unit 1.pptxDS Unit 1.pptx
DS Unit 1.pptx
 
Sorting techniques
Sorting techniques Sorting techniques
Sorting techniques
 
Unit 8 searching and hashing
Unit   8 searching and hashingUnit   8 searching and hashing
Unit 8 searching and hashing
 
Data structures arrays
Data structures   arraysData structures   arrays
Data structures arrays
 
Chapter 8 advanced sorting and hashing for print
Chapter 8 advanced sorting and hashing for printChapter 8 advanced sorting and hashing for print
Chapter 8 advanced sorting and hashing for print
 
Array 2
Array 2Array 2
Array 2
 
Unit 6 dsa SEARCHING AND SORTING
Unit 6 dsa SEARCHING AND SORTINGUnit 6 dsa SEARCHING AND SORTING
Unit 6 dsa SEARCHING AND SORTING
 
Unit viii searching and hashing
Unit   viii searching and hashing Unit   viii searching and hashing
Unit viii searching and hashing
 
Sorting and hashing concepts
Sorting and hashing conceptsSorting and hashing concepts
Sorting and hashing concepts
 
Sorting and hashing concepts
Sorting and hashing conceptsSorting and hashing concepts
Sorting and hashing concepts
 
An Experiment to Determine and Compare Practical Efficiency of Insertion Sort...
An Experiment to Determine and Compare Practical Efficiency of Insertion Sort...An Experiment to Determine and Compare Practical Efficiency of Insertion Sort...
An Experiment to Determine and Compare Practical Efficiency of Insertion Sort...
 
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
 
IRJET- A Survey on Different Searching Algorithms
IRJET- A Survey on Different Searching AlgorithmsIRJET- A Survey on Different Searching Algorithms
IRJET- A Survey on Different Searching Algorithms
 
Searching
SearchingSearching
Searching
 
DSA - Array.pptx
DSA - Array.pptxDSA - Array.pptx
DSA - Array.pptx
 
16-sorting.ppt
16-sorting.ppt16-sorting.ppt
16-sorting.ppt
 
Chapter 4: basic search algorithms data structure
Chapter 4: basic search algorithms data structureChapter 4: basic search algorithms data structure
Chapter 4: basic search algorithms data structure
 
Array data structure
Array data structureArray data structure
Array data structure
 
Array data structure
Array data structureArray data structure
Array data structure
 
Unit 7 sorting
Unit 7   sortingUnit 7   sorting
Unit 7 sorting
 

Dernier

Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
ZurliaSoop
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
KarakKing
 

Dernier (20)

How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POS
 
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxHMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
 
How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
How to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxHow to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptx
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdf
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
 
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptxOn_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
 
REMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxREMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptx
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
Interdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxInterdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptx
 
Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
 
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptxCOMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
 

Array ADT(Abstract Data Type)|Data Structure

  • 1. ADT(Abstract Data type) :- It represent the data and set of operation on data. Data:- 1. Array Space 2. Size 3. Length Operation:- 1. Display() 2. Add(x)/append(x) 3. Insert(index x) 4. Delete(index) 5. Search(x) 6. Get(index) 7. Set(index x) 8. Max()/Min() 9. Reverse() 10. Shift()/rotate
  • 2. 2. Add(x)/append(x):- 8 3 7 12 6 9 10 A 0 1 2 3 4 5 6 7 8 9 Size = 10 Length = 6 7 A[length] = x; Length ++; f(n) = 2 f(n) = 2no O(no) = O(1)
  • 3. 2. insert:- 8 3 7 12 6 9 10 A 0 1 2 3 4 5 6 7 8 9 Insert(4,15) 8 3 7 12 6 9 10 A 0 1 2 3 4 5 6 7 8 9 15 8 3 7 12 15 6 9 10 A 0 1 2 3 4 5 6 7 8 9 After Insertion
  • 4. 4. Delete(index) :- 8 3 7 15 6 9 10 A 0 1 2 3 4 5 6 7 8 9 8 3 7 15 6 9 10 A 0 1 2 3 4 5 6 7 8 9 Delete(3); 8 3 7 6 9 10 A 0 1 2 3 4 5 6 7 8 9
  • 5. 4. Delete(index) :- Code:- x = A[index]; If(index>=0 && index<length) { for(i=index ;i<length-1 ;i++) { A[i] = A[i+1]; } length--; }
  • 6. 4. Delete(index) :- Linear search:- • Start from the leftmost element of arr[] and one by one compare x with each element of arr[] • If x matches with an element, return the index. • If x doesn’t match with any of elements, return -1. • Time complexity is O(n).
  • 7. Improve linear search:- A linear search or sequential search is a method for finding an element within a list. It sequentially checks each element of the list until a match is found or the whole list has been searched. It is observed that when searching for a key element, then there is a possibility for searching the same key element again and again. The goal is that if the same element is searched again then the operation must take lesser time. Therefore, in such a case, Linear Search can be improved by using the following two methods: 1. Transposition 2. Move to Front
  • 8. Transposition: In transposition, if the key element is found, it is swapped to the element an index before to increase in a number of search count for a particular key, the search operation also optimizes and keep moving the element to the starting of the array where the searching time complexity would be of constant time. For Example: If the array arr[] is {2, 5, 7, 1, 6, 4, 5, 8, 3, 7} and let the key to be searched is 4, then below are the steps: • After searching for key 4, the element is found at index 5 of the given array after 6 comparisons. Now after transposition, the array becomes {2, 5, 7, 1, 4, 6, 5, 8, 3, 7} i.e., the key with value 4 comes at index 4. • Again after searching for key 4, the element is found at index 4 of the given array after 6 comparisons. Now after transposition, the array becomes {2, 5, 7, 4, 1, 6, 5, 8, 3, 7} i.e., the key with value 4 comes at index 3. • The above process will continue until any key reaches the front of the array if the element to be found is not at the first index
  • 9. Move to Front/Head: In this method, if the key element is found then it is directly swapped with the index 0, so that the next consecutive time, search operation for the same key element is of O(1), i.e., constant time. For Example: If the array arr[] is {2, 5, 7, 1, 6, 4, 5, 8, 3, 7} and let the key to be searched is 4, then below are the steps: • After searching for key 4, the element is found at index 5 of the given array after 6 comparisons. Now after moving to front operation, the array becomes {4, 2, 5, 7, 1, 6, 5, 8, 3, 7} i.e., the key with value 4 comes at index 0. • Again after searching for key 4, the element is found at index 0 of the given array which reduces the entire’s search space.
  • 10. 4 8 10 15 18 23 24 27 29 33 34 37 39 41 43 Binary search :- Size =15 Length = 15 key = 18 L H mid=[(L+H)/2] 0 14 7 0 6 3 4 6 5 4 4 4(found) key = 34 L H mid 0 14 7 8 14 11 8 10 9 10 10 10(found) key = 25 L H mid 0 14 7 0 6 3 4 6 5 6 6 6 7 6 X(not found) 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
  • 11. Binary Search algorithm:- Algorithm BiSearch(l,h,key) { while(l<=h) { mid = [(l+h)/2]; if(key == A[mid]) return mid; else if(key < A[mid]) h = mid-1; else l = mid + 1; } return -1; }
  • 12. Binary search Algorithm using recursion:- Algorithm RBinSearch(l,h,key) { if(l <= h) { mid = [(l+h)/2]; if(key == A[mid]) return mid; else if(key < A[mid]) return RBSearch(l,mid-1,key); else return RBSearch(mid+1,h,key) } return -1; }
  • 13. Analysis of Binary Search:- 4 8 10 15 18 23 24 27 29 33 34 37 39 41 43 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 7 27 3 15 11 37 1 8 5 21 9 33 13 41 0 4 2 10 4 18 6 24 8 29 10 34 12 39 14 43 Best min – O(1) Worst max – O(logn)
  • 14. 6. get(index):- to get the element of given index get(index) { if(index >=0 && index<length) return A[index]; } 7. set(index):- to set the value of element of given index set(index,x) { if(index >=0 && index<length) A[index] = x; }
  • 15. 8. max() & min():- max() { 1-------max = A[0]; n-------for(i=1;i<length;i++) { n-1 ---------if(A[i]>max) 1-----------------max = A[i]; } return max; } f(n) = 2n+1 O(n) min() { 1-------min = A[0]; N-------for(i=1;i<length;i++) { n-1 ----------if(A[i]<min) 1-----------------min = A[i]; } return min; } f(n) = 2n+1 O(n)
  • 16. Sum():- Sum() { total = 0; for(i=0;i<length;i++) { total = total + A[i]; } return total; } avg():- Sum() { total = 0; for(i=0;i<length;i++) { total = total + A[i]; } return total/n ; }
  • 17. 9. reverse() 8 3 7 15 6 9 10 2 12 4 0 1 2 3 4 5 6 7 8 9 A 4 12 2 10 9 6 15 7 3 8 0 1 2 3 4 5 6 7 8 9 B reverse 4 12 2 10 9 6 15 7 3 8 0 1 2 3 4 5 6 7 8 9 A for(i=length-1,j=0;i>=0;i--,j++) { B[j] = A[i];---------------n } for(i=0;i<length;i++) { A[i] = B[i];---------------n } 2n O(n)
  • 18. 9. reverse() 8 3 7 15 6 9 10 2 12 4 0 1 2 3 4 5 6 7 8 9 A 4 12 2 10 9 6 15 7 3 8 0 1 2 3 4 5 6 7 8 9 A for(i=0,j=length-1;i<j;i++,j--) { temp = A[i]; A[i] = A[j]; A[j] = temp; }
  • 19. 10. shift/rotate:- 8 3 7 15 6 0 1 2 3 4 A 3 7 15 6 0 0 1 2 3 4 A 8 Left shift 8 3 7 15 6 0 1 2 3 4 A 0 8 3 7 15 0 1 2 3 4 A 8 Right shift
  • 20. 8 3 7 15 6 0 1 2 3 4 A Rotate:- 3 7 15 6 8 0 1 2 3 4 A
  • 21. Insert An element in sorted array :- 4 8 13 16 20 25 28 33 0 1 2 3 4 5 6 7 8 9 A Insert – 18 x = 18; I = length-1; while(A[i]>x) { A[i+1] = A[i]; } A[i+1] = x;
  • 22. Array is sorted or not :- 4 8 13 26 20 25 28 33 0 1 2 3 4 5 6 7 8 9 A Algorithm isSorted(A,n) { for(i=0;i<n-1;i++) { if(A[i]>A[i+1]) return false; } return true; }
  • 23. Arrange –ve number on Left side:- -6 3 -8 10 5 -7 -9 12 -4 2 0 1 2 3 4 5 6 7 8 9 A i=0; j = length-1; While(i<j) { while(A[i]<0){i++;} while(A[i]>=0){i++;} if(i<j) swap(A[i],A[j]); } -6 -4 -8 -9 -7 5 10 12 3 2 0 1 2 3 4 5 6 7 8 9 A
  • 24. merging:- Merging can be done only on sorted array. In merging we have combine two sorted array and combine them to make a single sorted array. 2 4 6 8 10 0 1 2 3 4 A 3 5 7 9 11 0 1 2 3 4 B 2 3 4 5 6 7 8 9 10 11 0 1 2 3 4 5 6 7 8 9 C After merging:-