0% found this document useful (0 votes)
12 views8 pages

7 Searching and Sorting

Uploaded by

karanjaiswalf39
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views8 pages

7 Searching and Sorting

Uploaded by

karanjaiswalf39
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Assignment no.

Title: Searching and Sor ng

• 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

#include <stdio.h> #include


<stdlib.h>
#include <time.h>

// Struct to hold the count of comparisons and swaps typedef struct {


int comparisons; int swaps;
} Metrics;

// Bubble Sort void bubbleSort(int arr[], int n, Metrics *metrics)


{ metrics->comparisons = metrics->swaps = O;
for (int i = O; i < n - 1; i++) { for (int j
= O; j < n - i - 1; j++) { metrics-
>comparisons++;
if (arr[j] > arr[j + 1]) { int
temp = arr[j]; arr[j] =
arr[j + 1]; arr[j + 1] =
temp; metrics-
>swaps++;
// Optimized Bubble Sort void optimizedBubbleSort(int arr[], int n, Metrics
*metrics) { metrics->comparisons = metrics->swaps = O; int swapped;
for (int i = O; i < n - 1; i++) { swapped = O;
for (int j = O; j < n - i - 1; j++) { metrics->comparisons++;
if (arr[j] > arr[j + 1]) { int
temp = arr[j]; arr[j] =
arr[j + 1]; arr[j + 1] =
temp; metrics-
>swaps++; swapped =
1;

if (!swapped) break;

// Selection Sort void selectionSort(int arr[], int n, Metrics


*metrics) { metrics->comparisons = metrics->swaps = O;
for (int i = O; i < n - 1; i++) { int min
idx = i; for (int J• = i + I; j < n; j4-
4-) {
metrics->comparisons++; if
(arr[j] < arr[min_idx]) {
min_idx = j;
if (min_idx != i) { int temp =
arr[min_idx]; arr[min_idx] =
arr[i]; arr[i] = temp; metrics-
>swaps++;

// Insertion Sort void insertionSort(int arr[], int n, Metrics *metrics)


{ metrics->comparisons = metrics->swaps = O; for (int i = 1; i < n;
i++) { int key = arr[i]; int J I - I while (j >= O && arr[j] > key) {
metrics->comparisons++; arr[j + 1] = arr[j];

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

for (int i = O; i < nl; i++)


L[i] = arr[l + i]; for (int j =
O; j < n2; j++) R[j] = arr[m +
1 + j];

=l int
< n2) { metrics-
while
>comparisons++;

if (L[i] < arr[k]


= L[i];

arr[k] j++;

while (i < nl) { arr[k] =


L[i];

while (j < n2) {


arr[k]
void mergeSort(int arr[], int l, int r, Metrics *metrics) {

if (l < r) {

mergeSort(arr, l, m, metrics); mergeSort(arr, m + 1, r,


metrics); merge(arr, l, m, r, metrics);

// 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) {

int temp = arr[i]; arr[i]


= arr[j]; arr[j] = temp;
metrics->swaps++; int
temp = arr[i + 1]; arr[i +
1] = arr[high]; arr[high]
= temp; metrics-
>swaps++; return (i +
1);

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

// Heap Sort void heapify(int arr[], int n, int i, Metrics *metrics) {


int largest = i; int left = 2 + 1; int right = 2 + 2;

metrics->comparisons++; if (left < n &&


arr[left] > arr[largest]) largest = left;

metrics->comparisons++; if (right < n &&


arr[right] > arr[largest]) largest = right;
if (largest != i { int temp = arr[i]; arr[i] =
arr[largest]; arr[largest] = temp;
metrics->swaps++; heapify(arr, n,
largest, metrics);

void heapSort(int arr[], int n, Metrics *metrics) { metrics-


>comparisons = metrics->swaps = O; for (int i = n / 2 - 1;
i O; i--) heapify(arr, n, i, metrics);

for (int i = n - 1; i > O; i--) { int


temp = arr[O]; arr[O] = arr[i];
arr[i] = temp; metrics-
>swaps++; heapify(arr, i, O,
metrics);

// Helper function to display array void


printArray(int arr[], int size) { for (int i = O;
i < size; i++) printf("%d ", arr[i]);
printf("\n");
// Driver code int main() { int arr[] = {64, 25,
12, 22, 11}; int n = sizeof(arr) /
sizeof(arr[O]);
Metrics metrics;

printf("Original array:\n"); printArray(arr, n);

// Choose any sorting function here bubbleSort(arr, n, &metrics);

printf("Sorted array:\n"); printArray(arr, n); printf("Comparisons: %d, Swaps: %d\n",


metrics.comparisons, metrics.swaps);

return O;
Output
Original array:
64 25 12 22 11 Sorted
array:
11 12 22 25 64
Comparisons: 10, Swaps: 9

=== Code Execution Successful


Part 2
#include <stdio.h>

// Linear Search - Iterative int linearSearchlterative(int arr[], int


n, int x) { for (int i = O; i < n; i++) { if (arr[i] x) { return i; //
Return index of element if found

return -1; // Element not found

// Linear Search - Recursive int linearSearchRecursive(int arr[], int n, int


x, int index) { if (index >= n) { return -1; // Element not found

if (arr[index] x) { return index; // Return index of element if found

return linearSearchRecursive(arr, n, x, index + 1);

// Binary Search - Iterative int binarySearchlterative(int


arr[], int n, int x) { int left = O, right = n - 1; while (left <=
right) { int mid = left + (right - left) / 2;
if (arr[mid] x) { return mid; // Element
found
} else if (arr[mid] < x) { left =
mid + 1;

right = mid - 1;
return -1; // Element not found

// Binary Search - Recursive int binarySearchRecursive(int arr[], int left, int


right, int x) { if (left <= right) { int mid = left + (right - left) / 2; if (arr[mid]
x) { return mid; // Element found
} else if (arr[mid] < x) { return binarySearchRecursive(arr, mid + 1,
right, x);

return binarySearchRecursive(arr, left, mid - 1, x);

return -1; // Element not found

// Interpolation Search int interpolationSearch(int arr[],


int n, int x) { int left = O, right = n - 1;
while (left <= right && x >= arr[left] && x <= arr[right]) { if (left right)
{ if (arr[left] x) return left; return -1;

int pos = left + ((double)(right - left) / (arr[right] - arr[left])) * (x - arr[left]);

if (arr[pos] x) { return pos; // Element


found

if (arr[pos] < x) { left =


pos + 1;

right = pos - 1;

return -1; // Element not found


// Helper function to display search results void printResult(int
result) { if (result != printf("Element found at index %d\n",
result);

printf("Element not found\n");


// Driver code int main() { int arr[] = {2, 3, 4,
10, 40, 50, 60}; int n = sizeof(arr) /
sizeof(arr[O]); int x = 10

printf("Linear Search (Iterative):\n"); printResult(linearSearchlterative(arr, n, x));

printf("Linear Search (Recursive):\n"); printResult(linearSearchRecursive(arr, n, x, O));

printf("Binary Search (Iterative):\n");


printResult(binarySearchlterative(arr, n, x));

printf("Binary Search (Recursive):\n");

printResult(binarySearchRecursive(arr, O, n

printf("lnterpolation Search:\n"); printResult(interpolationSearch(arr,


n, x));

return O;
Output
/tmp/fewLNveDQx.o

Linear Search (Iterative): Element found


at index 3
Linear Search (Recursive):
Element found at index 3 Binary
Search (Iterative): Element
found at index 3
Binary Search (Recursive):
Element found at index 3
Interpolation Search: Element
found at index 3

=== Code Execution Successful

You might also like