searching and sorting techniques 1. to learn and appreciate the following concepts searching...
TRANSCRIPT
2
To learn and appreciate the following concepts
Searching Technique Linear Search
Sorting Technique Bubble Sort
Objectives
3
• Searching refers to finding whether a data item is
present in the set of items or not.
• Sorting refers to the arrangement of data in a particular
order.
• Sorting and searching have many applications in the
area of computers.
Searching & Sorting
4
Linear search
• The Linear Search is applied on the set of items that are
not arranged in any particular order.
• In linear search , the searching process starts from the
first item.
• The searching is continued till either the item is found or
the end of the list is reached indicating that the item is
not found.
7
void main(){int a[100],i,n,key,found=0;
cout<<"enter no of elements";cin>>n;
for(i=0;i<n;i++)cin>>a[i];
cout<<"enter the element to be searched";cin>>key;
WAP to search an element in an array using linear search
for(i=0; i<n; i++){ if(a[i]==key) {
found=1; break; }}
if(found==1) cout<<“Element is found ;else cout<<“Element is not found“;}
11
int main(){ int a[10],i,j,temp,n;
cout<<"Enter no of elements\n"; cin>>n;
cout<<"Enter array elements\n"; for(i=0;i<n;i++) { cin>>a[i]; }
cout<<"The elements of array are:\n"; for(i=0;i<n;i++) cout<<a[i]<<"\t";
To arrange the array elements in ascending/descending order using Bubble sort
for(i=0;i<n-1;i++){ for(j=0;j<n-i-1;j++) { if(a[j]>a[j+1]) // a[j]<a[j+1] { temp=a[j]; a[j]=a[j+1]; a[j+1]=temp; } }}
cout<<"\nThe elements of array after sorting are:\n";
for(i=0;i<n;i++) cout<<a[i]<<"\t";
}