7 Searching and Sorting
7 Searching and Sorting
• Part 1
1. Implement various sorting algorithms e.g., bubble sort, optimized bubble sort, selection sort,
insertion sort, merge sort, quick sort and heap sort.
2. Sample inputs of different types and sizes are added in a zip file available in the link. Sample file
reading code is also given in the zip.
3. Compare their performance on different input types (sorted, random, almost sorted)
4. Compare their performance on different input sizes (10, 100, 1000, 10000).
5. Analyze the above performances ( me taken, number of comparisons, no of swaps, space
required, etc.) and present the results visually (e.g., tables, plot as graphs) in the lab report. See
this link for reference.
• Part 2
1. Implement linear search using both iterative and recursive approach.
2. Implement binary search using both iterative and recursive approach.
3. (Optional) Implement interpolation search.
• Part 1
if (!swapped) break;
metrics->swaps++;
arr[j + 1] = key;
// Merge Sort void merge(int arr[], int l, int m, int r, Metrics *metrics) {
int nl = m - l + 1; int n2
=rm
int L[nl], R[n2];
=l int
< n2) { metrics-
while
>comparisons++;
arr[k] j++;
if (l < r) {
// Quick Sort int partition(int arr[], int low, int high, Metrics *metrics) { int
pivot = arr[high]; int i = (low - 1); for (int j = low; j high - 1; j++) {
metrics->comparisons++; if
(arr[j] < pivot) {
void quickSort(int arr[], int low, int high, Metrics *metrics) { if (low < high)
{ int pi = partition(arr, low, high, metrics); quickSort(arr, low, pi - 1,
metrics); quickSort(arr, pi + 1, high, metrics);
return O;
Output
Original array:
64 25 12 22 11 Sorted
array:
11 12 22 25 64
Comparisons: 10, Swaps: 9
right = mid - 1;
return -1; // Element not found
right = pos - 1;
printResult(binarySearchRecursive(arr, O, n
return O;
Output
/tmp/fewLNveDQx.o