0% found this document useful (0 votes)
2 views10 pages

Module-3(Arrays-2.2(searching-and-sorting(lab-program-6-and-11)).-16857900840505.pdf

The document covers searching and sorting techniques in C programming, specifically focusing on linear and binary search methods, as well as bubble sort and selection sort algorithms. It provides code examples for implementing these techniques, including input handling and output results. Additionally, it explains the algorithms step-by-step for better understanding.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views10 pages

Module-3(Arrays-2.2(searching-and-sorting(lab-program-6-and-11)).-16857900840505.pdf

The document covers searching and sorting techniques in C programming, specifically focusing on linear and binary search methods, as well as bubble sort and selection sort algorithms. It provides code examples for implementing these techniques, including input handling and output results. Additionally, it explains the algorithms step-by-step for better understanding.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

C Programming For Problem Solving

(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

Step 4: [Print sorted elements]


for i = 0 to n – 1
print a[i]
end for
30 sinks to bottom after pass3 20 sinks to bottom Step 5: [Finished]
Stop
/*Lab program 11:Program to sort given array of elements using Bubble printf("The sorted array elements are:\n");
sort technique*/ for(i=0;i<n;i++)
#include<stdio.h> {
int main() printf("%d\n",a[i]);
{ }
int a[20],n,i,j,temp; getch();
clrscr(); return 0;
printf("TO SORT ELEMENTS IN ASCENDING ORDER\n"); }
printf("----------------------------------------------------------\n");
printf("Enter the value of n\n"); TO SORT ELEMENTS IN ASCENDING ORDER
scanf("%d",&n);
---------------------------------------------------------
printf("Enter the array elements\n");
for(i=0;i<n;i++) Enter the value of n
{ 5
scanf("%d",&a[i]); Enter the array elements
}
printf("The Elements before sorting:\n"); 12
for(i=0;i<n;i++) 21
{ 31
printf("%d\n",a[i]);
13
}
for(i=1;i<n;i++) 15
{ The sorted array is:
for(j=0;j<n-i;j++) 12
{
if(a[j]>=a[j+1]) 13
{ 15
temp=a[j]; 21
a[j]=a[j+1];
a[j+1]=temp;
31
}
Selection Sort in C
Selection sort is another Example of Selection Sort
algorithm that is used for Consider the array:
sorting. This sorting algorithm, [10,5,2,1]
iterates through the array and The first element is 10. The next part we must
finds the smallest number in find the smallest number from the remaining
array. The smallest number from 5 2 and 1 is 1.
the array and swaps it with
So, we replace 10 by 1.
the first element if it is
The new array is [1,5,2,10] Again, this process
smaller than the first element. is repeated.
Next, it goes on to the second Finally, we get the sorted array as [1,2,5,10].
element and so on until all
elements are sorted.
Algorithm for Selection Sort:
Step 1 − Set min to the first location
Step 2 − Search the minimum element in the array
Step 3 – swap the first location with the minimum value in the array
Step 4 – assign the second element as min.
Step 5 − Repeat the process until we get a sorted array.
/*Program to sort given array of elements using selection sort technique*/ printf("The sorted array elements are:\n");
#include<stdio.h> for(i=0;i<n;i++)
int main() {
{ printf("%d\n",a[i]);
int i,j,n,a[10],pos,temp; }
printf("PROGRAM TO SORT THE ELEMENTS USING SELECTION getch();
SORT TECHNIQUE USING FUNCTIONS\n"); return 0;
printf("------------------------------------------------------------------\n"); }
printf("Enter the total number of elements\n");
scanf("%d",&n);
printf("Enter the array elements\n");
for(i=0;i<n;i++) TO SORT ELEMENTS IN ASCENDING ORDER
{ ---------------------------------------------------------
scanf("%d",&a[i]);
Enter the value of n
}
for(i=0;i<n;i++) 5
{ Enter the array elements
pos=i; 12
for(j=i+1;j<n;j++)
{
21
if(a[j]<a[pos]) 31
{ 13
pos=j;
15
}
} The sorted array is:
temp=a[pos]; 12
a[pos]=a[i]; 13
a[i]=temp;
}
15
21
31

You might also like