0% found this document useful (0 votes)
14 views13 pages

Sorting and Searching

Searching sorting example

Uploaded by

benhurriderfan
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
0% found this document useful (0 votes)
14 views13 pages

Sorting and Searching

Searching sorting example

Uploaded by

benhurriderfan
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 13

7.

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;
}
}
}

printf("Sorted list in ascending order:\n");

for (c = 0; c < n; c++)


printf("%d\n", array[c]);

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]);

// Logic of selection sort algorithm


for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(arr[i]< arr[j])
{
temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
}
}
printf("Sorted elements: ");
for(i=0;i<n;i++)
printf(" %d",arr[i]);
return 0;
}

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;
}

f. Write a C program that sorts a given array of names

#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 find_smallest(int arr[], int n);

int find_largest(int arr[], int n);

int find_sum(int arr[], int n);

float find_average(int arr[], int n);

int main() {

int n, choice;

printf("Enter the number of elements: ");

scanf("%d", &n);

int arr[n];

get_numbers(arr, n);

while(1)

printf("\n*********");

printf("\n*Menu:*");

printf("\n*********\n");

printf("\t1. Find the smallest number\n");

printf("\t2. Find the largest number\n");

printf("\t3. Find the sum of all numbers\n");

printf("\t4. Find the average of all numbers\n");

printf("\tEnter your choice (1-4): ");

scanf("%d", &choice);
switch (choice) {

case 1:

printf("The smallest number is %d\n", find_smallest(arr, n));

break;

case 2:

printf("The largest number is %d\n", find_largest(arr, n));

break;

case 3:

printf("The sum of all numbers is %d\n", find_sum(arr, n));

break;

case 4:

printf("The average of all numbers is %.2f\n", find_average(arr, n));

break;

default:

printf("Error: Invalid choice\n");

exit(0);

}}

return 0;

void get_numbers(int arr[], int n) {

printf("Enter %d numbers:\n", n);

for (int i = 0; i < n; i++) {


scanf("%d", &arr[i]);

int find_smallest(int arr[], int n) {

int smallest = arr[0];

for (int i = 1; i < n; i++) {

if (arr[i] < smallest) {

smallest = arr[i];

return smallest;

int find_largest(int arr[], int n) {

int largest = arr[0];

for (int i = 1; i < n; i++) {

if (arr[i] > largest) {

largest = arr[i];

return largest;

}
int find_sum(int arr[], int n) {

int sum = 0;

for (int i = 0; i < n; i++) {

sum += arr[i];

return sum;

float find_average(int arr[], int n) {

int sum = find_sum(arr, n);

return (float) sum / n;


}

b) Write a C program to construct a pyramid of numbers as follows:


a. 1
12
123
#include <stdio.h>

int main() {

int i, j, rows;

printf("Enter the number of rows: ");


scanf("%d", &rows);

for (i = 1; i <= rows; ++i) {

for (j = 1; j <= i; ++j) {

printf("%d ", j);

printf("\n");

return 0;

Write a program to print the following pattern

*
* *
* * *
* * * *
* * * * *

#include <stdio.h>

int main() {

int i, j, rows;

printf("Enter the number of rows: ");

scanf("%d", &rows);

for (i = 1; i <= rows; ++i) {

for (j = 1; j <= i; ++j) {

printf("* ");

printf("\n");

}
return 0;

Write a program to print the following pattern

1
2 3
4 5 6
7 8 9 10

#include <stdio.h>

int main() {

int rows, i, j, number = 1;

printf("Enter the number of rows: ");

scanf("%d", &rows);

for (i = 1; i <= rows; i++) {

for (j = 1; j <= i; ++j) {

printf("%d ", number);

++number;

printf("\n");

return 0;

}
Write a program to print the following pattern

#include <stdio.h>

void main()

int num, i, j, m = 1; // declare local variables

printf (" Enter the number to define the columns: \n");

scanf ("%d", & num);

for (i = 1; i <= num; i++)

for (j = 1; j <= i; j++)

printf( "* ");

printf("\n");

}
for (i = num-1; i >= 1; i--)

for (j = 1; j <= i; j++)

printf ("* ");

printf("\n");

getch();

You might also like