0% found this document useful (0 votes)
13 views11 pages

Lesson 8 Lab

The document provides instructions for learners to complete a lab on sorting methods and binary search algorithms. It explains how to implement selection sort, quick sort, merge sort, insertion sort, and binary search through coding examples. Learners will be able to demonstrate and run code for each sorting method and binary search after completing the detailed steps provided.

Uploaded by

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

Lesson 8 Lab

The document provides instructions for learners to complete a lab on sorting methods and binary search algorithms. It explains how to implement selection sort, quick sort, merge sort, insertion sort, and binary search through coding examples. Learners will be able to demonstrate and run code for each sorting method and binary search after completing the detailed steps provided.

Uploaded by

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

Module 8: Algorithms and Error Handling

Lab 1: Sorting

Overview:
This lab enables you to explore different sorting methods.
After performing the lab you will be able to:
1. Demonstrate Selection sort, Quick sort, Merge sort, and Insertion sort
2. Implement a binary search algorithm

Business Scenario:
The user requires an understanding of the various sorting methods to implement sorting
programs. They will understand and implement all these methods with the help of the
detailed instructions provided below.

Detailed Instructions:
Selection Sort
1. In this program, we will implement the selection sort algorithm.
#include <stdio.h>

void swap(int *xp, int *yp)


{
int temp = *xp;
*xp = *yp;
*yp = temp;
}

void selectionSort(int arr[], int n)


{
int i, j, min_idx;

// One by one move boundary of unsorted subarray


for (i = 0; i < n-1; i++)
{
// Find the minimum element in unsorted array
min_idx = i;
for (j = i+1; j < n; j++)
if (arr[j] < arr[min_idx])
min_idx = j; // Found new minimum

// Swap the found minimum element with the first element


swap(&arr[min_idx], &arr[i]);
Module 8: Algorithms and Error Handling
}
}

/* Function to print an array */


void printArray(int arr[], int size)
{
int i;
for (i=0; i < size; i++)
printf("%d ", arr[i]);
printf("\n");
}

// Driver program to test above functions


int main()
{
int arr[] = {64, 25, 12, 22, 11};
int n = sizeof(arr)/sizeof(arr[0]);
selectionSort(arr, n);
printf("Sorted array: \n");
printArray(arr, n);
return 0;
}

2. To compile and run the program, copy the source code into a .c file and compile it.
3. Once the program is compiled successfully, execute the program.
Module 8: Algorithms and Error Handling
Quick Sort
1. In this program, you will implement the quick sort algorithm.
#include <stdio.h>
#include <stdlib.h>

void display(int*,int);

void swap(int *a, int *b)


{
*a = *a - *b;
*b = *b + *a;
*a = *b - *a;
}

int partition(int *arr, int start, int end) //Partition the array
{
int pivot = start;
int left = start , right = end;
int i , pivotVal = arr[pivot];

while(left < right)


{
while(arr[left] <= pivotVal && left <=end )
left++;
while(arr[right] > pivotVal && right >= 0)
right--;
if(left < right)
swap(&arr[left], &arr[right]);
}

arr[start] = arr[right];
arr[right] = pivotVal;

return right;
}

void quick(int *arr, int start, int end)


{
int m;
if(start < end)
{
m = partition(arr,start,end); //Pivot
quick(arr,start,m-1);
quick(arr,m+1,end);
}
}
Module 8: Algorithms and Error Handling

void display(int *arr,int nmem)


{
int i;
for(i = 0; i < nmem;i++)
printf("%d ",arr[i]);
printf("\n");
}

int main()
{
int arr[]={12,32,2,56,34,23,67,122};
int nmem = sizeof(arr)/sizeof(int);

quick(arr, 0, nmem - 1);


display(arr,nmem); //Sorted array
}

2. To compile and run the program, copy the source code into a .c file and compile it.
3. Once the program is compiled successfully, execute the program.
Module 8: Algorithms and Error Handling
Merge Sort
1. This lab will show you how to implement merge sort algorithm.
#include<stdlib.h>
#include<stdio.h>

// Merges two subarrays of arr[].


// First subarray is arr[l..m]
// Second subarray is arr[m+1..r]
void merge(int arr[], int l, int m, int r)
{
int i, j, k;
int n1 = m - l + 1;
int n2 = r - m;

/* create temp arrays */


int L[n1], R[n2];

/* Copy data to temp arrays L[] and R[] */


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

/* Merge the temp arrays back into arr[l..r]*/


i = 0; // Initial index of first subarray
j = 0; // Initial index of second subarray
k = l; // Initial index of merged subarray
while (i < n1 && j < n2)
{
if (L[i] <= R[j])
{
arr[k] = L[i];
i++;
}
else
{
arr[k] = R[j];
j++;
}
k++;
}

/* Copy the remaining elements of L[], if there


are any */
while (i < n1)
{
Module 8: Algorithms and Error Handling
arr[k] = L[i];
i++;
k++;
}

/* Copy the remaining elements of R[], if there


are any */
while (j < n2)
{
arr[k] = R[j];
j++;
k++;
}
}

/* l is for left index and r is right index of the


sub-array of arr to be sorted */
void mergeSort(int arr[], int l, int r)
{
if (l < r)
{
// Same as (l+r)/2, but avoids overflow for
// large l and h
int m = l+(r-l)/2;

// Sort first and second halves


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

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

/* Function to print an array */


void printArray(int A[], int size)
{
int i;
for (i=0; i < size; i++)
printf("%d ", A[i]);
printf("\n");
}

/* Driver program to test above functions */


int main()
{
int arr[] = {12, 11, 13, 5, 6, 7};
int arr_size = sizeof(arr)/sizeof(arr[0]);
Module 8: Algorithms and Error Handling
printf("Given array is \n");
printArray(arr, arr_size);

mergeSort(arr, 0, arr_size - 1);

printf("\nSorted array is \n");


printArray(arr, arr_size);
return 0;
}

2. To compile and run the program, copy the source code into a .c file and compile it.
3. Once the program is compiled successfully, execute the program.
Module 8: Algorithms and Error Handling
Insertion Sort
1. Here, we will implement the insertion sort algorithm.
#include <stdio.h>
#include <math.h>

/* Function to sort an array using insertion sort*/


void insertionSort(int arr[], int n)
{
int i, key, j;
for (i = 1; i < n; i++)
{
key = arr[i];
j = i-1;

/* Move elements of arr[0..i-1], that are


greater than key, to one position ahead
of their current position */
while (j >= 0 && arr[j] > key)
{
arr[j+1] = arr[j];
j = j-1;
}
arr[j+1] = key;
}
}

// A utility function to print an array of size n


void printArray(int arr[], int n)
{
int i;
for (i=0; i < n; i++)
printf("%d ", arr[i]);
printf("\n");
}

/* Driver program to test insertion sort */


int main()
{
int arr[] = {12, 11, 13, 5, 6};
int n = sizeof(arr)/sizeof(arr[0]);

insertionSort(arr, n);
printArray(arr, n);

return 0;
Module 8: Algorithms and Error Handling
}

2. To compile and run the program, copy the source code into a .c file and compile it.
3. Once the program is compiled successfully, execute the program.
Module 8: Algorithms and Error Handling
Binary Search Algorithm
1. The lab shows how to implement binary search algorithm.
#include <stdio.h>

// A recursive binary search function. It returns


// location of x in given array arr[l..r] is present,
// otherwise -1
int binarySearch(int arr[], int l, int r, int query)
{
if (r >= l)
{
int mid = l + (r - l) / 2;

// If the element is present at the middle


// itself
if (arr[mid] == query)
return mid;

// If element is smaller than mid, then


// it can only be present in left subarray
if (arr[mid] > query)
return binarySearch(arr, l, mid - 1, query);

// Else the element can only be present


// in right subarray
return binarySearch(arr, mid + 1, r, query);
}

// We reach here when element is not


// present in array
return -1;
}

int main(void)
{
int arr[] = {2, 3, 4, 10, 40};
int n = sizeof(arr) / sizeof(arr[0]);
int query = 10;
int result = binarySearch(arr, 0, n - 1, query);
if (result == -1)
printf("Element is not present in array\n");
else
printf("Element is present at index %d\n", result);
return 0;
}
Module 8: Algorithms and Error Handling
2. To compile and run the program, copy the source code into a .c file and compile it.
3. Once the program is compiled successfully, execute the program.

You might also like