Module-3(Arrays-2.2(searching-and-sorting(lab-program-6-and-11)).-16857900840505.pdf
Module-3(Arrays-2.2(searching-and-sorting(lab-program-6-and-11)).-16857900840505.pdf
(18CPS13)
MODULE 3:
Arrays
/*Program to search key element using linear search*/
Searching Techniques: #include<stdio.h>
Include<process.h>
Finding an element in a given set of array elements is known as void main()
searching. {
There are two types of searching techniques: int a[30],n,i,key;
1. Linear search clrscr()
printf(“enter the number of elements\n”);//INPUT
2. Binary search
scanf(“%d”,&n); 5
Linear search: printf(“Enter the array elements”); //50 40 30 60 10
• It is a searching technique which is used to search a key for(i=0;i<n;i++)
element in a given set of array elements in a sequential scanf(“%d”,&a[i]);
manner. printf(“Enter the key element to be searched\n”);
• The searching begins from first element and goes on till last scanf(“%d”,&key); //10
element in an array. for(i=0;i<n;i++)
• In the course of searching if the key element matches with {
if(key==a[i]) //OUTPUT
any of the array element then we print “Successful search”
{
or “Element found” otherwise “Unsuccessful search” or printf(“Successful search\n”);
“Element not found”. printf(“The element found at %d position\n”,i)
50 40 30 60 10 getch(); //5
a[0] a[1] a[2] a[3] a[4] i=0,1,2,3,4 exit(0);
for(i=0;i<5;i++) }
Key =10 “Successful search” if key==a*0+ , }
Key=70 “Unsuccessful search” key==a*1+ if(key==a*i]) printf(“Unsuccessful search\n”);
key==a[2] { getch();
key==a[3] printf(“Successful”);
}
key==a[4] successful }
Binary search:It is simple searching technique which can be Alogorithm Binary_Search: This algorithm is used to
applied if the items are either in ascending or descending order. search for an item in a given sorted list
Step 1: [input the number of items]
The pictorial representation is given below: Read n
Step 2: [Read N elements]
for i = 0 to n-1
read a[i]
end for
Step 3: [Input the item to be searched]
if(key==a[mid]) if(key<a[mid]) if(key>a[mid])
Read key
successful high=mid-1 low=mid+1 Step 4: [Initialization]
low = 0
high = n-1
Step 5: [Search using binary search]
Trace: Example 1 while low != high
Array a Let key=20 low high mid a[mid] Iteration mid = (low + high) / 2
10 low 0 4 2 a[2]=30 1 if key = =a[mid]
20 Since 30 ≠ 20 and key =20 < 30 print “Successful search”
30 so high =mid-1 is executed stop
40 0 1 0 a[0]=10 2 else if key < a[mid]
50 high Since 10 ≠ 20 and 20>10 hence high = mid – 1
low=mid+1 is executed else
1 1 1 a[1]=20 3 low = mid + 1
Since 20 = 20 key element is found end if
end while
Example 2: print “Unsuccessful Search”
Key=70 Key element is not found Step 6: [Finished]
Stop
/* Lab program 6: Introduce 1D Array manipulation and
implement Binary search. */ if(key > a[mid])
#include<stdio.h> low = mid +1;
int main( ) }
{
int n, a[20], key, low, high, mid,flag=0; if(flag==1)
printf(“Enter the value of n \n”); {
scanf(“%d”, &n); printf(“SUCCESSFUL SEARCH\n” );
printf(“Enter the elements in ASCENDING order \n”); printf(“Element found at %d location\n”, mid + 1 );
for( i = 0; i < n ; i++ ) }
{ else
scanf(“%d”, &a[i] ); printf(“UNSUCCESSFUL SEARCH\n” );
} return 0;
printf(“Enter the key element to be searched\n”); }
OUTPUT:
scanf(“%d”, &key);
RUN 1:
low = 0; Enter the value of n:
high = n-1; 5
Enter the elements in ASCENDING order
while( low < = high )
12345
{ Enter the key element to be searched
mid = (low + high) / 2; 4
Successful search
if(key = = a[mid])
Element found at Location 4
{ RUN 2:
flag=1; Enter the value of n:
6
break;
Enter the elements in ASCENDING order
} 123456
if(key < a[mid]) Enter the key element to be searched
7
high = mid – 1;
Key Element not found
Sorting techniques: Algorithm Bubble_sort: This algorithm is used to sort the
Sorting: The process of arraigning the given elements in given N elements.
ascending or descending order is called sorting.
Bubble sort: In this sorting technique the lighter element will Step 1: [Input the number of items]
Read n
be bubble to top and heavier element will be pushed down to
bottom. Step 2: [Read N elements]
Example for i = 0 to n-1
N=5 read a[i]
We need n-1 pass to complete sorting, here (5-1)=4 pass end for
required.
Step 3: [Sort the elements]
for i= 1to n – 1
for j = 0 to n - i
if a[j] > a[j+1]
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
end if
50 sinks to bottom after pass1 40 sinks to bottom after pass2 end for
end for