C Searching and Sorting Practice Questions with Solutions

Searching and Sorting are among the most important algorithms in C Programming and are frequently asked in coding interviews, placement tests, and competitive programming.

  • Searching is the process of finding a specific element within a collection of data.
  • Sorting is the process of arranging data in ascending or descending order.

Efficient searching and sorting algorithms improve program performance, reduce execution time, and optimize memory usage. These algorithms are widely used in databases, operating systems, search engines, inventory systems, banking software, and almost every software application.

Some commonly used searching and sorting algorithms include:

Searching Algorithms

  • Linear Search
  • Binary Search

Sorting Algorithms

  • Bubble Sort
  • Selection Sort
  • Insertion Sort
  • Merge Sort
  • Quick Sort

In this chapter, you’ll practice the most commonly used searching and sorting programs with complete solutions, sample outputs, explanations, and concepts covered. C Searching and Sorting practice questions with solutions help to build concepts.


1. C Program for Linear Search

Problem Statement

Write a C program to search an element in an array using the Linear Search algorithm.

C Solution

#include <stdio.h>

int main()
{
    int array[] = {12, 25, 30, 45, 60};
    int size = 5;
    int searchElement = 45;
    int i;

    for(i = 0; i < size; i++)
    {
        if(array[i] == searchElement)
        {
            printf("Element Found at Position %d", i + 1);
            return 0;
        }
    }

    printf("Element Not Found.");

    return 0;
}

Sample Output

Element Found at Position 4

Explanation

Linear Search checks every element one by one until the required element is found.

Concepts Covered

  • Linear Search
  • Arrays
  • Loops
  • Searching Algorithms

2. C Program for Binary Search

Problem Statement

Write a C program to search an element in a sorted array using the Binary Search algorithm.

C Solution

#include <stdio.h>

int main()
{
    int array[] = {10, 20, 30, 40, 50, 60};
    int low = 0;
    int high = 5;
    int middle;
    int searchElement = 40;

    while(low <= high)
    {
        middle = (low + high) / 2;

        if(array[middle] == searchElement)
        {
            printf("Element Found.");
            return 0;
        }

        if(array[middle] < searchElement)
        {
            low = middle + 1;
        }
        else
        {
            high = middle - 1;
        }
    }

    printf("Element Not Found.");

    return 0;
}

Sample Output

Element Found.

Explanation

Binary Search repeatedly divides the sorted array into two halves until the element is found.

Concepts Covered

  • Binary Search
  • Arrays
  • Divide and Conquer
  • Searching Algorithms

3. C Program for Bubble Sort

Problem Statement

Write a C program to sort an array using the Bubble Sort algorithm.

C Solution

#include <stdio.h>

int main()
{
    int array[] = {45, 12, 78, 25, 10};
    int size = 5;
    int i, j, temp;

    for(i = 0; i < size - 1; i++)
    {
        for(j = 0; j < size - i - 1; j++)
        {
            if(array[j] > array[j + 1])
            {
                temp = array[j];
                array[j] = array[j + 1];
                array[j + 1] = temp;
            }
        }
    }

    printf("Sorted Array:\n");

    for(i = 0; i < size; i++)
    {
        printf("%d ", array[i]);
    }

    return 0;
}

Sample Output

Sorted Array:
10 12 25 45 78

Explanation

Bubble Sort repeatedly compares adjacent elements and swaps them whenever they are in the wrong order.

Concepts Covered

  • Bubble Sort
  • Arrays
  • Nested Loops
  • Sorting Algorithms

4. C Program for Selection Sort

Problem Statement

Write a C program to sort an array using the Selection Sort algorithm.

C Solution

#include <stdio.h>

int main()
{
    int array[] = {64, 25, 12, 22, 11};
    int size = 5;
    int i, j, minimumIndex, temp;

    for(i = 0; i < size - 1; i++)
    {
        minimumIndex = i;

        for(j = i + 1; j < size; j++)
        {
            if(array[j] < array[minimumIndex])
            {
                minimumIndex = j;
            }
        }

        temp = array[i];
        array[i] = array[minimumIndex];
        array[minimumIndex] = temp;
    }

    printf("Sorted Array:\n");

    for(i = 0; i < size; i++)
    {
        printf("%d ", array[i]);
    }

    return 0;
}

Sample Output

Sorted Array:
11 12 22 25 64

Explanation

Selection Sort repeatedly selects the smallest element from the unsorted portion of the array and places it at the correct position.

Concepts Covered

  • Selection Sort
  • Arrays
  • Nested Loops
  • Sorting Algorithms

5. C Program for Insertion Sort

Problem Statement

Write a C program to sort an array using the Insertion Sort algorithm.

C Solution

#include <stdio.h>

int main()
{
    int array[] = {12, 11, 13, 5, 6};
    int size = 5;
    int i, key, j;

    for(i = 1; i < size; i++)
    {
        key = array[i];
        j = i - 1;

        while(j >= 0 && array[j] > key)
        {
            array[j + 1] = array[j];
            j--;
        }

        array[j + 1] = key;
    }

    printf("Sorted Array:\n");

    for(i = 0; i < size; i++)
    {
        printf("%d ", array[i]);
    }

    return 0;
}

Sample Output

Sorted Array:
5 6 11 12 13

Explanation

Insertion Sort builds the sorted array one element at a time by inserting each element into its correct position among the previously sorted elements.

Concepts Covered

  • Insertion Sort
  • Arrays
  • Sorting Algorithms
  • Loops

6. C Program to Sort an Array in Descending Order

Problem Statement

Write a C program to sort an array in descending order.

C Solution

#include <stdio.h>

int main()
{
    int array[] = {15, 42, 8, 23, 4};
    int size = 5;
    int i, j, temp;

    for(i = 0; i < size - 1; i++)
    {
        for(j = i + 1; j < size; j++)
        {
            if(array[i] < array[j])
            {
                temp = array[i];
                array[i] = array[j];
                array[j] = temp;
            }
        }
    }

    printf("Descending Order:\n");

    for(i = 0; i < size; i++)
    {
        printf("%d ", array[i]);
    }

    return 0;
}

Sample Output

Descending Order:
42 23 15 8 4

Explanation

The comparison condition is reversed so that larger elements move toward the beginning of the array.

Concepts Covered

  • Sorting
  • Descending Order
  • Arrays
  • Nested Loops

7. C Program to Find the Largest Element in an Array

Problem Statement

Write a C program to find the largest element in an array.

C Solution

#include <stdio.h>

int main()
{
    int array[] = {25, 18, 90, 43, 76};
    int size = 5;
    int largest, i;

    largest = array[0];

    for(i = 1; i < size; i++)
    {
        if(array[i] > largest)
        {
            largest = array[i];
        }
    }

    printf("Largest Element = %d", largest);

    return 0;
}

Sample Output

Largest Element = 90

Explanation

The program scans the array and continuously updates the largest value whenever a bigger element is encountered.

Concepts Covered

  • Arrays
  • Searching
  • Loops
  • Maximum Element

8. C Program to Find the Smallest Element in an Array

Problem Statement

Write a C program to find the smallest element in an array.

C Solution

#include <stdio.h>

int main()
{
    int array[] = {25, 18, 90, 43, 76};
    int size = 5;
    int smallest, i;

    smallest = array[0];

    for(i = 1; i < size; i++)
    {
        if(array[i] < smallest)
        {
            smallest = array[i];
        }
    }

    printf("Smallest Element = %d", smallest);

    return 0;
}

Sample Output

Smallest Element = 18

Explanation

The program compares each array element with the current smallest value and updates it whenever a smaller value is found.

Concepts Covered

  • Arrays
  • Searching
  • Minimum Element
  • Loops

9. C Program to Count the Number of Comparisons in Bubble Sort

Problem Statement

Write a C program to count the total number of comparisons performed during Bubble Sort.

C Solution

#include <stdio.h>

int main()
{
    int array[] = {5, 1, 4, 2, 8};
    int size = 5;
    int i, j, temp;
    int comparisons = 0;

    for(i = 0; i < size - 1; i++)
    {
        for(j = 0; j < size - i - 1; j++)
        {
            comparisons++;

            if(array[j] > array[j + 1])
            {
                temp = array[j];
                array[j] = array[j + 1];
                array[j + 1] = temp;
            }
        }
    }

    printf("Total Comparisons = %d", comparisons);

    return 0;
}

Sample Output

Total Comparisons = 10

Explanation

A counter variable is incremented during every comparison inside the Bubble Sort algorithm.

Concepts Covered

  • Bubble Sort
  • Performance Analysis
  • Arrays
  • Loops

10. C Program to Merge Two Sorted Arrays

Problem Statement

Write a C program to merge two sorted arrays into a single sorted array.

C Solution

#include <stdio.h>

int main()
{
    int firstArray[] = {10, 20, 30};
    int secondArray[] = {15, 25, 35};
    int mergedArray[6];

    int i = 0, j = 0, k = 0;

    while(i < 3 && j < 3)
    {
        if(firstArray[i] < secondArray[j])
        {
            mergedArray[k++] = firstArray[i++];
        }
        else
        {
            mergedArray[k++] = secondArray[j++];
        }
    }

    while(i < 3)
    {
        mergedArray[k++] = firstArray[i++];
    }

    while(j < 3)
    {
        mergedArray[k++] = secondArray[j++];
    }

    printf("Merged Sorted Array:\n");

    for(i = 0; i < 6; i++)
    {
        printf("%d ", mergedArray[i]);
    }

    return 0;
}

Sample Output

Merged Sorted Array:
10 15 20 25 30 35

Explanation

The program compares elements from both sorted arrays and stores the smaller element in the merged array until all elements are processed.

Concepts Covered

  • Arrays
  • Merging Arrays
  • Sorting
  • Searching Algorithms

11. C Program to Search an Element After Sorting

Problem Statement

Write a C program to sort an array and then search for an element using Linear Search.

C Solution

#include <stdio.h>

int main()
{
    int array[] = {45, 12, 78, 23, 9};
    int size = 5;
    int i, j, temp;
    int searchElement = 23;
    int found = 0;

    /* Bubble Sort */
    for(i = 0; i < size - 1; i++)
    {
        for(j = 0; j < size - i - 1; j++)
        {
            if(array[j] > array[j + 1])
            {
                temp = array[j];
                array[j] = array[j + 1];
                array[j + 1] = temp;
            }
        }
    }

    /* Linear Search */
    for(i = 0; i < size; i++)
    {
        if(array[i] == searchElement)
        {
            printf("Element Found at Position %d", i + 1);
            found = 1;
            break;
        }
    }

    if(found == 0)
    {
        printf("Element Not Found.");
    }

    return 0;
}

Sample Output

Element Found at Position 3

Explanation

The array is first sorted using Bubble Sort, and then Linear Search is performed to locate the required element.

Concepts Covered

  • Bubble Sort
  • Linear Search
  • Arrays
  • Searching and Sorting

12. C Program to Find Duplicate Elements in an Array

Problem Statement

Write a C program to identify duplicate elements in an array.

C Solution

#include <stdio.h>

int main()
{
    int array[] = {10, 20, 30, 20, 40, 10};
    int size = 6;
    int i, j;

    printf("Duplicate Elements:\n");

    for(i = 0; i < size; i++)
    {
        for(j = i + 1; j < size; j++)
        {
            if(array[i] == array[j])
            {
                printf("%d\n", array[i]);
                break;
            }
        }
    }

    return 0;
}

Sample Output

Duplicate Elements:
10
20

Explanation

Each element is compared with the remaining elements in the array to identify duplicates.

Concepts Covered

  • Arrays
  • Duplicate Elements
  • Nested Loops
  • Searching

13. C Program to Remove Duplicate Elements from a Sorted Array

Problem Statement

Write a C program to remove duplicate elements from a sorted array.

C Solution

#include <stdio.h>

int main()
{
    int array[] = {10, 10, 20, 20, 30, 40, 40};
    int size = 7;
    int i, j = 0;

    for(i = 0; i < size - 1; i++)
    {
        if(array[i] != array[i + 1])
        {
            array[j++] = array[i];
        }
    }

    array[j++] = array[size - 1];

    printf("Array After Removing Duplicates:\n");

    for(i = 0; i < j; i++)
    {
        printf("%d ", array[i]);
    }

    return 0;
}

Sample Output

Array After Removing Duplicates:
10 20 30 40

Explanation

Since the array is already sorted, duplicate elements appear consecutively, making them easy to remove.

Concepts Covered

  • Sorted Arrays
  • Duplicate Removal
  • Arrays
  • Loops

14. C Program to Implement Binary Search Using Functions

Problem Statement

Write a C program to implement Binary Search using a user-defined function.

C Solution

#include <stdio.h>

int binarySearch(int array[], int size, int searchElement)
{
    int low = 0;
    int high = size - 1;
    int middle;

    while(low <= high)
    {
        middle = (low + high) / 2;

        if(array[middle] == searchElement)
        {
            return middle;
        }

        if(array[middle] < searchElement)
        {
            low = middle + 1;
        }
        else
        {
            high = middle - 1;
        }
    }

    return -1;
}

int main()
{
    int array[] = {5, 10, 15, 20, 25, 30};
    int result;

    result = binarySearch(array, 6, 20);

    if(result != -1)
    {
        printf("Element Found at Position %d", result + 1);
    }
    else
    {
        printf("Element Not Found.");
    }

    return 0;
}

Sample Output

Element Found at Position 4

Explanation

The Binary Search logic is placed inside a separate function to improve code modularity and reusability.

Concepts Covered

  • Binary Search
  • Functions
  • Arrays
  • Searching Algorithms

15. C Program to Compare Linear Search and Binary Search

Problem Statement

Write a C program demonstrating the use of both Linear Search and Binary Search.

C Solution

#include <stdio.h>

int main()
{
    printf("Linear Search:\n");
    printf("- Works on Sorted and Unsorted Arrays\n");
    printf("- Time Complexity: O(n)\n\n");

    printf("Binary Search:\n");
    printf("- Requires Sorted Array\n");
    printf("- Time Complexity: O(log n)\n");

    return 0;
}

Sample Output

Linear Search:
- Works on Sorted and Unsorted Arrays
- Time Complexity: O(n)

Binary Search:
- Requires Sorted Array
- Time Complexity: O(log n)

Explanation

Linear Search checks each element sequentially, whereas Binary Search repeatedly divides a sorted array into halves, making it significantly faster for large datasets.

Concepts Covered

  • Linear Search
  • Binary Search
  • Time Complexity
  • Algorithm Comparison

Chapter Summary

In this chapter, you learned the fundamentals of Searching and Sorting algorithms in C Programming. You practiced Linear Search, Binary Search, Bubble Sort, Selection Sort, Insertion Sort, descending sorting, duplicate detection, duplicate removal, array merging, and algorithm comparisons. These algorithms form the foundation of efficient data processing and are widely used in software development, databases, operating systems, and competitive programming.


Key Takeaways

  • Searching helps locate specific elements in a dataset.
  • Linear Search works on both sorted and unsorted arrays.
  • Binary Search requires a sorted array.
  • Bubble Sort repeatedly swaps adjacent elements.
  • Selection Sort repeatedly selects the minimum element.
  • Insertion Sort inserts each element into its correct position.
  • Sorting improves search efficiency.
  • Binary Search is much faster than Linear Search for large sorted datasets.
  • Arrays are the most common data structure for implementing searching and sorting.
  • These algorithms are frequently asked in coding interviews.

Frequently Asked Questions (FAQs)

1. What is searching in C?

Searching is the process of locating a specific element within an array or collection of data.


2. What is the difference between Linear Search and Binary Search?

  • Linear Search checks every element sequentially and works on any array.
  • Binary Search repeatedly divides a sorted array into halves, making it much faster.

3. Why must Binary Search use a sorted array?

Binary Search relies on the ordering of elements to discard half of the search space after each comparison. Without sorting, this optimization is not possible.


4. What is sorting?

Sorting is the process of arranging data in ascending or descending order.


5. Which sorting algorithm is easiest to understand?

Bubble Sort is generally considered the easiest because it repeatedly compares and swaps adjacent elements until the array is sorted.


6. Which search algorithm is faster?

For large sorted datasets, Binary Search is much faster than Linear Search because it has a time complexity of O(log n) compared to O(n).


7. Where are searching and sorting algorithms used?

They are commonly used in databases, search engines, inventory systems, banking software, operating systems, file management systems, e-commerce platforms, and competitive programming.


8. Why are searching and sorting algorithms important in interviews?

Searching and sorting are core computer science topics that test your understanding of arrays, loops, algorithm design, optimization, and time complexity, making them one of the most frequently asked subjects in technical interviews.

Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.

Scroll to Top