Professional Documents
Culture Documents
Analysis and Design
Analysis and Design
Algorithm
Submitted by:
Tarvish sonkhla
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>
if (r >= l) {
int mid = l + (r - l) / 2;
// itself
if (arr[mid] == x)
return mid;
if (arr[mid] > x)
// present in array
return -1;
}
int main()
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>
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;
}
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>
// Partition function
int partition(int arr[], int low, int high)
{
9
int j = high;
while (i < j) {
// QuickSort function
void quickSort(int arr[], int low, int high)
{
if (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]);
11
4. Merge Sort
// C program for Merge Sort
#include <stdio.h>
#include <stdlib.h>
12
while (i < n1 && j < n2) {
if (L[i] <= R[j]) {
arr[k] = L[i];
i++;
}
else {
arr[k] = R[j];
j++;
}
k++;
}
13
void mergeSort(int arr[], int l, int r)
{
if (l < r) {
int m = l + (r - l) / 2;
merge(arr, l, m, r);
}
}
// Driver code
int main()
{
int arr[] = { 12, 11, 13, 5, 6, 7 };
int arr_size = sizeof(arr) / sizeof(arr[0]);
14
mergeSort(arr, 0, arr_size - 1);
15