# C Programming Exercises, Practice, Solution : Searching and Sorting

## C Searching and Sorting Algorithm [9 exercises with solution]

1. Write a C program for binary search. Go to the editor

Binary Search : In computer science, a binary search or half-interval search algorithm finds the position of a target value within a sorted array. The binary search algorithm can be classified as a dichotomies divide-and-conquer search algorithm and executes in logarithmic time.

Click me to see the solution

2. Write a C program to sort a list of elements using the selection sort algorithm. Go to the editor

*According to Wikipedia “In computer science, selection sort is a sorting algorithm, specifically an in-place comparison sort. It has O(n2) time complexity, making it inefficient on large lists, and generally performs worse than the similar insertion sort”.
*Note :

a) To find maximum of elements

b) To swap two elements

Click me to see the solution

3. Write a C program to sort a list of elements using the bubble sort algorithm. Go to the editor

Bubble Sort works by repeatedly swapping the adjacent elements if they are in wrong order.

Click me to see the solution

4. Write a C program to sort a list of elements using the insertion sort algorithm. Go to the editor

Insertion sort is a simple sorting algorithm that builds the final sorted array (or list) one item at a time. It is much less efficient on large lists than other algorithms such as quicksort, heapsort, or merge sort.

Click me to see the solution

5. Write a C program to sort a list of elements using the merge sort algorithm. Go to the editor

Merge sort is an O(n log n) comparison-based sorting algorithm. Most implementations produce a stable sort, which means that the implementation preserves the input order of equal elements in the sorted output.

Click me to see the solution

6. Write a C program to sort numbers using heap algorithm(MAX heap). Go to the editor

A sorting algorithm that works by first organizing the data to be sorted into a special type of binary tree called a heap.

Click me to see the solution

7. Write a C program to sort a list of elements using the quick sort algorithm. Go to the editor

Quick sort is a comparison sort, meaning that it can sort items of any type for which a "less-than" relation (formally, a total order) is defined.

Note:
Read n values into array and Sort using Quick Sort

Click me to see the solution

8. Write a C program to sort a list of elements using the radix sort algorithm. Go to the editor

Radix sort is a non-comparative integer sorting algorithm that sorts data with integer keys by grouping keys by the individual digits which share the same significant position and value.

Click me to see the solution

9. Write a C Program for counting sort. Go to the editor

According to Wikipedia “In computer science, counting sort is an algorithm for sorting a collection of objects according to keys that are small integers; that is, it is an integer sorting algorithm. It operates by counting the number of objects that have each distinct key value, and using arithmetic on those counts to determine the positions of each key value in the output sequence. Its running time is linear in the number of items and the difference between the maximum and minimum key values, so it is only suitable for direct use in situations where the variation in keys is not significantly greater than the number of items. However, it is often used as a subroutine in another sorting algorithm, radix sort, that can handle larger keys more efficiently”.

Click me to see the solution

**C Programming Code Editor:**

#include <stdio.h> int main() { }