Sorting and Searching
Sorting and Searching
a) Write a C program that uses non-recursive function to search for a Key value in a
given list of integers using linear search method.
#include<stdio.h>
void main()
{
int i, a[20], n, key, flag = 0;
printf(“Enter the size of an array \n”);
scanf(“%d”, &n);
printf(“Enter the array elements”);
for(i = 0; i < n; i++)
{
scanf(“%d”, &a[i]);
}
printf(“Enter the key elements”);
scanf(“%d”, &key);
for(i = 0; i < n; i++)
{
if(a[i] == key)
{
flag = 1;
break;
}
}
if(flag == 1)
printf(“The key elements is found at location %d”, i + 1);
else
printf(“The key element is not found in the array”);
getch();
}
b) Write a C program that uses non recursive function to search for a Key value in a
given d. sorted list of integers using binary search method.
#include<stdio.h>
void main()
{
int a[20], i, n, key, low, high, mid;
printf("Enter the size of array");
scanf("%d",&n);
printf("Enter the array elements in ascending order");
for(i = 0; i < n; i++)
{
scanf("%d", &a[i]);
}
printf("Enter the key element\n");
scanf("%d", &key);
low = 0;
high = n - 1;
while(high >= low)
{
mid = (low + high) / 2;
if(key == a[mid])
break;
else
{
if(key > a[mid])
low = mid + 1;
else
high = mid - 1;
}
}
if(key == a[mid])
printf("The key element is found at location %d", mid + 1);
else
printf("the key element is not found");
getchar();
}
c) Write a C program that implements the Bubble sort method to sort a given list
of integers in ascending order.
#include <stdio.h>
int main()
{
int array[100], n, c, d, swap;
printf("Enter number of elements\n");
scanf("%d", &n);
printf("Enter %d integers\n", n);
for (c = 0; c < n; c++)
scanf("%d", &array[c]);
for (c = 0 ; c < n - 1; c++)
{
for (d = 0 ; d < n - c - 1; d++)
{
if (array[d] > array[d+1])
{
swap = array[d];
array[d] = array[d+1];
array[d+1] = swap;
}
}
}
return 0;
}
d) Write a C program that sorts the given array of integers using selection sort in
descending order
#include <stdio.h>
int main(){
int i, j, n,temp, arr[30];
printf("Enter number of elements\n");
scanf("%d", &n);
printf("Enter %d integers\n", n);
for(i=0;i<n;i++)
{
Scanf(“%d”,&a[i]);
e) Write a C program that sorts the given array of integers using insertion sort in
ascending order
#include <stdio.h>
int main(void)
{
int n, i, j, temp;
int arr[64];
printf("Enter number of elements\n");
scanf("%d", &n);
printf("Enter %d integers\n", n);
for (i = 0; i < n; i++)
{
scanf("%d", &arr[i]);
}
for (i = 1; i < n; i++)
{
j = i;
while (j > 0 && arr[j - 1] > arr[j])
{
temp = arr[j];
arr[j] = arr[j - 1];
arr[j - 1] = temp;
j--;
}
}
printf("Sorted list in ascending order:\n");
for (i = 0; i < n; i++)
{
printf("%d\n", arr[i]);
}
return 0;
}
#include<stdio.h>
#include<string.h>
int main(){
int i,j,n;
char str[100][100],s[100];
printf("Enter number of names :");
scanf("%d",&n);
printf("Enter names in any order:");
for(i=0;i<n;i++)
{
scanf("%s",str[i]);
}
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(strcmp(str[i],str[j])>0)
{
strcpy(s,str[i]);
strcpy(str[i],str[j]);
strcpy(str[j],s);
}
}
}
printf("The sorted order of names are:");
for(i=0;i<n;i++)
{
printf("\n%s",str[i]);
}
return 0;
}
8. Miscellaneous:
a) Write a menu driven C program that allows a user to enter n numbers and
then choose between finding the smallest, largest, sum, or average. The
menu and all the choices are to be functions. Use a switch statement to
determine what action to take. Display an error message if an invalid
choice is entered.
#include <stdio.h>
#include<stdlib.h>
void get_numbers(int arr[], int n);
int main() {
int n, choice;
scanf("%d", &n);
int arr[n];
get_numbers(arr, n);
while(1)
printf("\n*********");
printf("\n*Menu:*");
printf("\n*********\n");
scanf("%d", &choice);
switch (choice) {
case 1:
break;
case 2:
break;
case 3:
break;
case 4:
break;
default:
exit(0);
}}
return 0;
smallest = arr[i];
return smallest;
largest = arr[i];
return largest;
}
int find_sum(int arr[], int n) {
int sum = 0;
sum += arr[i];
return sum;
int main() {
int i, j, rows;
printf("\n");
return 0;
*
* *
* * *
* * * *
* * * * *
#include <stdio.h>
int main() {
int i, j, rows;
scanf("%d", &rows);
printf("* ");
printf("\n");
}
return 0;
1
2 3
4 5 6
7 8 9 10
#include <stdio.h>
int main() {
scanf("%d", &rows);
++number;
printf("\n");
return 0;
}
Write a program to print the following pattern
#include <stdio.h>
void main()
printf("\n");
}
for (i = num-1; i >= 1; i--)
printf("\n");
getch();