Download as pdf or txt
Download as pdf or txt
You are on page 1of 15

Analysis and design

Algorithm
Submitted by:

Tarvish sonkhla

Roll no: SGL22357

Semester: 3rd (CSE)

Computer Science & Engineering

University Institute of Engineering and Technology

Panjab University SSG Regional Centre, Hoshiarpur

1
Searching Algorithms
1. Linear Search
// C code to linearly search x in arr[]
#include <stdio.h>
int search(int arr[], int N, int x)
{
for (int i = 0; i < N; i++)
if (arr[i] == x)
return i;
return -1;
}

// Driver code
int main(void)
{
int arr[] = { 2, 3, 4, 10, 40 };
int x = 10;
int N = sizeof(arr) / sizeof(arr[0]);
// Function call
int result = search(arr, N, x);
(result == -1)
? printf("Element is not present in array")
: printf("Element is present at index %d", result);
return 0;
}

2
2. Iterative Binary Search
// C program to implement iterative Binary Search
#include <stdio.h>
// An iterative binary search function.
int binarySearch(int arr[], int l, int r, int x)
{
while (l <= r) {
int m = l + (r - l) / 2;
// Check if x is present at mid
if (arr[m] == x)
return m;
if (arr[m] < x)
l = m + 1;
else
r = m - 1;
}
// If we reach here, then element was not present
return -1;
}
int main(void)
{
int arr[] = { 2, 3, 4, 10, 40 };
int n = sizeof(arr) / sizeof(arr[0]);
int x = 10;
int result = binarySearch(arr, 0, n - 1, x);
(result == -1) ? printf("Element is not present"" in array"): printf("Element is
present at "
"index %d",result);
return 0;}

3
3.Recursive Binary Search
// C program to implement recursive Binary Search

#include <stdio.h>

int binarySearch(int arr[], int l, int r, int x)

if (r >= l) {

int mid = l + (r - l) / 2;

// If the element is present at the middle

// itself
if (arr[mid] == x)

return mid;

// If element is smaller than mid, then

// it can only be present in left subarray

if (arr[mid] > x)

return binarySearch(arr, l, mid - 1, x);

// Else the element can only be present


// in right subarray

return binarySearch(arr, mid + 1, r, x);

// We reach here when element is not

// present in array

return -1;

}
int main()

int arr[] = { 2, 3, 4, 10, 40 };


int n = sizeof(arr) / sizeof(arr[0]);

int x = 10;
4
int result = binarySearch(arr, 0, n - 1, x);
(result == -1)? printf("Element is not present in array"): printf("Element is
present at index %d", result); // ternary operator

return 0;

Sorting Algorithms
1. Bubble Sort
// Optimized implementation of Bubble sort
#include <stdbool.h>
#include <stdio.h>

void swap(int* xp, int* yp)


{
int temp = *xp;
*xp = *yp;
*yp = temp;
}
void bubbleSort(int arr[], int n)
{
int i, j;
bool swapped;
for (i = 0; i < n - 1; i++) {
swapped = false;
for (j = 0; j < n - i - 1; j++) {

5
if (arr[j] > arr[j + 1]) {
swap(&arr[j], &arr[j + 1]);
swapped = true;
}
}
if (swapped == false)
break;
}
}
void printArray(int arr[], int size)
{
int i;
for (i = 0; i < size; i++)
printf("%d ", arr[i]);
}
int main()
{
int arr[] = { 64, 34, 25, 12, 22, 11, 90 };
int n = sizeof(arr) / sizeof(arr[0]);
bubbleSort(arr, n);
printf("Sorted array: \n");
printArray(arr, n);
return 0;
}

2. Selection Sort
// C program for implementation of selection sort
#include <stdio.h>

6
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;
// Swap the found minimum element with the first element
if(min_idx != i)
swap(&arr[min_idx], &arr[i]);
}
}
void printArray(int arr[], int size)
{
int i;
for (i=0; i < size; i++)
printf("%d ", arr[i]);
printf("\n");
}

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

3. Insertion Sort
void insertion_sort(int arr[], int n)
{
int i, j, key;
for ( int j = 1; j < n; j++)
{
key = arr[j];
i = j - 1;
while ((i>=0)&&(arr[i] > key))
{
arr[i+1]=arr[i];
i=i-1;
}
arr[i+1]=key;
}

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

8
int main()
{
int arr[] = {3,4,5,2,1};
int n = sizeof(arr) / sizeof(arr[0]);
insertion_sort(arr, n);
printf("sorted array:");
printArray(arr, n);
return (0);
}

3. Quick Sort
// C program to implement Quick Sort Algorithm
#include <stdio.h>

// Function to swap two elements


void swap(int* a, int* b)
{
int temp = *a;
*a = *b;
*b = temp;
}

// Partition function
int partition(int arr[], int low, int high)
{

// initialize pivot to be the first element


int pivot = arr[low];
int i = low;

9
int j = high;

while (i < j) {

// condition 1: find the first element greater than


// the pivot (from starting)
while (arr[i] <= pivot && i <= high - 1) {
i++;
}

// condition 2: find the first element smaller than


// the pivot (from last)
while (arr[j] > pivot && j >= low + 1) {
j--;
}
if (i < j) {
swap(&arr[i], &arr[j]);
}
}
swap(&arr[low], &arr[j]);
return j;
}

// QuickSort function
void quickSort(int arr[], int low, int high)
{
if (low < high) {

// call Partition function to find Partition Index


int partitionIndex = partition(arr, low, high);

10
// Recursively call quickSort() for left and right
// half based on partition Index
quickSort(arr, low, partitionIndex - 1);
quickSort(arr, partitionIndex + 1, high);
}
}

// driver code
int main()
{
int arr[] = { 19, 17, 15, 12, 16, 18, 4, 11, 13 };
int n = sizeof(arr) / sizeof(arr[0]);

// printing the original array


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

// calling quickSort() to sort the given array


quickSort(arr, 0, n - 1);

// printing the sorted array


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

11
4. Merge Sort
// C program for Merge Sort
#include <stdio.h>
#include <stdlib.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;
j = 0;
k = l;

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

13
void mergeSort(int arr[], int l, int r)
{
if (l < r) {
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 code
int main()
{
int arr[] = { 12, 11, 13, 5, 6, 7 };
int arr_size = sizeof(arr) / sizeof(arr[0]);

printf("Given array is \n");


printArray(arr, arr_size);

14
mergeSort(arr, 0, arr_size - 1);

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


printArray(arr, arr_size);
return 0;
}

15

You might also like