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

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

[An editor is available at the bottom of the page to write and execute the scripts.]

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

10. Write a C program to display sorted list using Gnome sort. Go to the editor
Gnome sort is a sorting algorithm originally proposed by Dr. Hamid Sarbazi-Azad (Professor of Computer Engineering at Sharif University of Technology) in 2000 and called "stupid sort" (not to be confused with bogosort), and then later on described by Dick Grune and named "gnome sort". The algorithm always finds the first place where two adjacent elements are in the wrong order, and swaps them. It takes advantage of the fact that performing a swap can introduce a new out-of-order adjacent pair only next to the two swapped elements.
Click me to see the solution

11. Write a C program that sort numbers using shell sorting method. Go to the editor
According to Wikipedia "Shell sort or Shell's method, is an in-place comparison sort. It can be seen as either a generalization of sorting by exchange (bubble sort) or sorting by insertion (insertion sort). The method starts by sorting pairs of elements far apart from each other, then progressively reducing the gap between elements to be compared. Starting with far apart elements can move some out-of-place elements into position faster than a simple nearest neighbor exchange."
Click me to see the solution

12. Write a C program that sort numbers using QuickSort method. Go to the editor
Note: According to Wikipedia "Quicksort 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. Inefficient implementations it is not a stable sort, meaning that the relative order of equal sort items is not preserved. Quicksort can operate in-place on an array, requiring small additional amounts of memory to perform the sorting."
Click me to see the solution

13. Write a C program that sort numbers using Bead Sort method. Go to the editor
According to Wikipedia "Bead sort, also called gravity sort, is a natural sorting algorithm, developed by Joshua J. Arulanandham, Cristian S. Calude and Michael J. Dinneen in 2002, and published in The Bulletin of the European Association for Theoretical Computer Science. Both digital and analog hardware implementations of bead sort can achieve a sorting time of O(n); however, the implementation of this algorithm tends to be significantly slower in software and can only be used to sort lists of positive integers. Also, it would seem that even in the best case, the algorithm requires O(n2) space".
Click me to see the solution

14. Write a C program that sort numbers using Bogo Sort method. Go to the editor
In computer science, Bogo Sort is a particularly ineffective sorting algorithm based on the generate and test paradigm. The algorithm successively generates permutations of its input until it finds one that is sorted. It is not useful for sorting but may be used for educational purposes, to contrast it with other more realistic algorithms.
Click me to see the solution

15. Write a C program that sort numbers using Cocktail Sort method. Go to the editor
Cocktail shaker sort (also known as bidirectional bubble sort, cocktail sort, shaker sort, ripple sort, shuffle sort, or shuttle sort ) is a variation of bubble sort that is both a stable sorting algorithm and a comparison sort. The algorithm differs from a bubble sort in that it sorts in both directions on each pass through the list. This sorting algorithm is only marginally more difficult to implement than a bubble sort and solves the problem of turtles in bubble sorts. It provides only marginal performance improvements, and does not improve asymptotic performance; like the bubble sort, it is not of practical interest, though it finds some use in education.
Click me to see the solution

16. Write a C program that sort numbers using Cycle sort method. Go to the editor
Cycle sort is an in-place, unstable sorting algorithm, a comparison sort that is theoretically optimal in terms of the total number of writes to the original array, unlike any other in-place sorting algorithm. It is based on the idea that the permutation to be sorted can be factored into cycles, which can individually be rotated to give a sorted result.
Click me to see the solution

17. Write a C program that sort numbers using Permutation Sort method. Go to the editor
Permutation sort, proceeds by generating the possible permutations of the input array/list until discovering the sorted one.
Click me to see the solution

18. Write a C program to sort a list of elements using the insertionsort algorithm. Go to the editor

```Input numbers you want to input: Input 5 values to sort

The ascending order of the values:
11
13
15
20
25
```
Click me to see the solution

C Programming Code Editor:

More to Come !

Do not submit any solution of the above exercises at here, if you want to contribute go to the appropriate exercise page.

﻿

Inviting useful, relevant, well-written and unique guest posts