0% found this document useful (0 votes)
9 views16 pages

DAA Shami

The document contains multiple programs demonstrating various sorting and searching algorithms including Recursive Binary Search, Recursive Linear Search, Heap Sort, Merge Sort, Selection Sort, Insertion Sort, and Quick Sort. Each program includes an algorithm description, code implementation in C, and sample output. The author of the document is Shami Srivastava, with a student ID provided.

Uploaded by

anshikashami
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)
9 views16 pages

DAA Shami

The document contains multiple programs demonstrating various sorting and searching algorithms including Recursive Binary Search, Recursive Linear Search, Heap Sort, Merge Sort, Selection Sort, Insertion Sort, and Quick Sort. Each program includes an algorithm description, code implementation in C, and sample output. The author of the document is Shami Srivastava, with a student ID provided.

Uploaded by

anshikashami
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/ 16

PROGRAM NO: 01

Aim: Program for Recursive Binary & Linear Search

PROGRAM No 1(a)
Recursive Binary Search
Algorithm:
START
BinarySearch(array, target, left, right):
IF right < left:
RETURN -1 // Base case: target not found
mid = floor((left + right) / 2)
IF array[mid] == target:
RETURN mid // Found the target at index mid
ELSE IF array[mid] < target:
RETURN BinarySearch(array, target, mid + 1, right) // Search right half
ELSE:
RETURN BinarySearch(array, target, left, mid - 1) // Search left half
END

Code:
#include <stdio.h>
int BinarySearch(int array[], int lb, int ub, int element)
{
if (ub >= lb)
{
int middle = lb + (ub - lb )/2;
if (array[middle] == element)
return middle;
if (array[middle] > element)
return BinarySearch(array, lb, middle-1, element);
else
return BinarySearch(array, middle+1, ub, element);
}
return -1;
}
int main(void)
{
//take sorted array
int element;
int array[] = {1, 4, 7, 9, 16, 56, 70};
int n = 7; printf("element:");
scanf("%d",&element);
SHAMI SRIVASTAVA
2200321540156
int found_index = BinarySearch(array, 0, n-1, element);
if(found_index == -1 )
printf("Element not found in the array ");
else
printf("Element found at index : %d",found_index);
return 0;
}

Output:

PROGRAM No 1(b)
Recursive Linear Search
Algorithm:
START
LinearSearch(array, target, index):
IF index >= length(array):
RETURN -1 // base case: target not found
IF array[index] == target:
RETURN index // found the target at index
ELSE:
RETURN LinearSearch(array, target, index + 1) // continue searching recursively
END

Code:
#include <stdio.h>
int LinearSearch(int a[],int ele,int i)
{ if(i>=0){ if(a[i]==ele){
return i+1;
}
else{
return (LinearSearch(a,ele,i-1));
}}
else
return -1;
}
int main() { int
n=10,ele,pos;
n=10;
SHAMI SRIVASTAVA
2200321540156
int
a[]={3,45,23,7,19,34,65,12,9,10};
printf("element:");
scanf("%d",&ele);
pos=LinearSearch(a,ele,n-1);
if(pos == -1 ) {
printf("Element not found in the array ");
}
else {
printf("Element found at index : %d",pos);
}
return 0;
}

Output:

SHAMI SRIVASTAVA
2200321540156
PROGRAM NO: 02

Aim: Program for Heap Sort.

Heap sort
Algorithm:
START
HeapSort(arr)
BuildMaxHeap(arr)
for i = length(arr) to 2
swap arr[1] with arr[i]
heap_size[arr] = heap_size[arr] ? 1
MaxHeapify(arr,1)
END

BuildMaxHeap(arr)

START
BuildMaxHeap(arr)
heap_size(arr) = length(arr)
for i = length(arr)/2 to 1
MaxHeapify(arr,i)
END

MaxHeapify(arr,i)

START
MaxHeapify(arr,i)
L = left(i)
R = right(i)
if L ? heap_size[arr] and arr[L] > arr[i]
largest = L
else
largest = i
if R ? heap_size[arr] and arr[R] > arr[largest]
largest = R
if largest != i
swap arr[i] with arr[largest]
MaxHeapify(arr,largest)
END

SHAMI SRIVASTAVA
2200321540156
Code:
#include <stdio.h>

void heapify(int a[], int n, int i)


{
int largest = i; // Initialize largest as root
int left = 2 * i + 1; // left child
int right = 2 * i + 2; // right child

if (left < n && a[left] > a[largest])


largest = left;

if (right < n && a[right] > a[largest])


largest = right;

if (largest != i) {
// swap a[i] with a[largest]
int temp = a[i];
a[i] = a[largest];
a[largest] = temp;

heapify(a, n, largest);
}
}

/*Function to implement the heap sort*/

void heapSort(int a[], int n)


{
for (int i = n / 2 - 1; i >= 0; i--)
heapify(a, n, i);
// One by one extract an element from heap
for (int i = n - 1; i >= 0; i--) {
/* Move current root element to end*/
// swap a[0] with a[i]
int temp = a[0];
a[0] = a[i];
a[i] = temp;

heapify(a, i, 0);
}
}
/* function to print the array elements */
void printArr(int arr[], int n)
{
for (int i = 0; i < n; ++i)
{
printf("%d", arr[i]);
printf(" ");
}

SHAMI SRIVASTAVA
2200321540156
}
int main()
{
int a[] = {48, 10, 23, 43, 28, 26, 1};
int n = sizeof(a) / sizeof(a[0]);
printf("Before sorting array elements are - \n");
printArr(a, n);
heapSort(a, n);
printf("\nAfter sorting array elements are - \n");
printArr(a, n);
return 0;
}

Output:

SHAMI SRIVASTAVA
2200321540156
PROGRAM NO: 03

Aim: Program for Merge Sort.

Merge sort
Algorithm:
START
FUNCTION MergeSort(arr, left, right):
IF left > right
RETURN
mid = (left+right)/2
MergeSort(arr, left, mid)
MergeSort(arr, mid+1, right)
Merge(arr, left, mid, right)
END

Merge algorithm:
START
FUNCTION Merge(arr, left, mid, right):
n1 = mid - left + 1;
n2 = right - mid;
FOR i=0 to n1:
L[i] = arr[left + i];
FOR j=0 to n2:
R[j] = arr[mid + 1 + j];
i = 0;
j = 0;
k = l;
WHILE (i < n1 && j < n2):
IF (L[i] <= R[j]):
arr[k] = L[i];
i++;
ELSE:
arr[k] = R[j];
j++;
k++;

WHILE (i < n1):


arr[k] = L[i];
i++;
k++;
WHILE (j < n2):
SHAMI SRIVASTAVA
2200321540156
arr[k] = R[j];
j++;
k++;
END

Code:
#include <stdio.h>
#include <stdlib.h>

void merge(int arr[], int l, int m, int r)


{
int i, j, k;
int n1 = m - l + 1;
int n2 = r - m;

int L[n1], R[n2]; // Create temp arrays


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

i = 0;
j = 0;
k = l;
while (i < n1 && j < n2) {
if (L[i] <= R[j]) {
arr[k] = L[i];
i++;
}
else {
arr[k] = R[j];
j++;
}
k++;
}
while (i < n1) {
arr[k] = L[i];
i++;
k++;
}
while (j < n2) {
arr[k] = R[j];
j++;
k++;
}
}

SHAMI SRIVASTAVA
2200321540156
void mergeSort(int arr[], int l, int r)
{
if (l < r) {
int m = l + (r - l) / 2;
mergeSort(arr, l, m);
mergeSort(arr, m + 1, r);
merge(arr, l, m, r);
}
}

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

printf("Given array is \n");


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

mergeSort(arr, 0, size - 1);

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


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

Output:

SHAMI SRIVASTAVA
2200321540156
PROGRAM NO: 04

Aim: Program for Selection Sort.

Selection sort
Algorithm:
START
FUNCTION SelectionSort(array, size){
FOR(i=1; i<n-1; i++)
min = i
FOR (j=i; j<n; j++)
IF array[j] < array[min]:
min = j
SWAP(array[i], array[min])
END

Code:
#include <stdio.h>
void SelectionSort(int array[], int k) {
for (int l = 0; l < k - 1; l++) {
int min = l;
for (int m = l + 1; m < k; m++) {
if (array[m] < array[min]) {
min = m;
}
}
int temp = array[l];
array[l] = array[min];
array[min] = temp;
}
}
int main(){
int n;
n = 8;
int arr[8] = {9, 67, 81, 34, 13, 42, -14, 55}; //initialize an array
printf("Array before Sorting: ");
for(int i = 0; i<n; i++)
printf("%d ",arr[i]);
printf("\n");
SelectionSort(arr, n);
printf("Array after Sorting: ");
for(int i = 0; i<n; i++)
printf("%d ", arr[i]);
SHAMI SRIVASTAVA
2200321540156
printf("\n");
}

Output:

SHAMI SRIVASTAVA
2200321540156
PROGRAM NO: 05

Aim: Write a program for Insertion Sort.

Insertion Sort
Algorithm:
START
FUNCTION InsertionSort(array, size)
FOR(j=2; j<size; j++)
key = array[j]
i=j–1
WHILE i > 0 and array[i] > key
array[i + 1] = array[i]
i = i -1
array[i + 1] = key
END

Code:
#include <stdio.h>
void InsertionSort(int array[], int size){
int key, j;
for(int i = 1; i<size; i++) {
key = array[i];
j = i;
while(j > 0 && array[j-1]>key) {
array[j] = array[j-1];
j--;
}
array[j] = key;
}
}
int main(){
int n;
n = 8;
int arr[8] = {67, 44, 82, 17, 20, 5, 56, -65};
printf("Array before Sorting: ");
for(int i = 0; i<n; i++)
printf("%d ",arr[i]);
printf("\n");
InsertionSort(arr, n);
printf("Array after Sorting: ");
for(int i = 0; i<n; i++)
printf("%d ", arr[i]);
printf("\n");
SHAMI SRIVASTAVA
2200321540156
}

Output:

SHAMI SRIVASTAVA
2200321540156
PROGRAM NO: 06

Aim: Program for Quick Sort.

Quick sort
Algorithm:
START
FUNCTION QuickSort(left, right)
IF right-left <= 0:
RETURN
ELSE
pivot = A[right]
partition = Partition(left, right, pivot)
QuickSort(left,partition-1)
QuickSort(partition+1,right)
END

Partition Algorithm:
START
FUNCTION Partition(left, right, pivot)
leftPointer = left
rightPointer = right - 1
WHILE True DO:
WHILE A[++leftPointer] < pivot DO:
//do-nothing
WHILE rightPointer > 0 && A[--rightPointer] > pivot DO:
//do-nothing
IF leftPointer >= rightPointer:
break
ELSE:
SWAP(leftPointer,rightPointer)
SWAP leftPointer,right
RETURN leftPointer
END

Code:
#include <stdio.h>
#include <stdbool.h>
#define MAX 7
int intArray[MAX] = {

SHAMI SRIVASTAVA
2200321540156
4,6,3,2,1,9,7
};
void swap(int num1, int num2) {
int temp = intArray[num1];
intArray[num1] = intArray[num2];
intArray[num2] = temp;
}
int partition(int left, int right, int pivot) {
int i;
int leftPointer = left - 1;
int rightPointer = right;
while (true) {
while (intArray[++leftPointer] < pivot) {
//do nothing
}
while (rightPointer > 0 && intArray[--rightPointer] > pivot) {
//do nothing
}

if (leftPointer >= rightPointer) {


break;
} else {
printf(" item swapped :%d,%d\n", intArray[leftPointer], intArray[rightPointer]);
swap(leftPointer, rightPointer);
}
}

printf(" pivot swapped :%d,%d\n", intArray[leftPointer], intArray[right]);


swap(leftPointer, right);
printf("Updated Array: ");
for (i = 0; i < MAX; i++) {
printf("%d ", intArray[i]);
}
printf("\n");
return leftPointer;
}

void quickSort(int left, int right) {


if (right - left <= 0) {
return;
} else {
int pivot = intArray[right];
int partitionPoint = partition(left, right, pivot);
quickSort(left, partitionPoint - 1);
quickSort(partitionPoint + 1, right);
}
}

SHAMI SRIVASTAVA
2200321540156
int main() {
int i;
printf("Input Array: ");
for (i = 0; i < MAX; i++) {
printf("%d ", intArray[i]);
}
printf("\n");
quickSort(0, MAX - 1);
printf("Output Array: ");
for (i = 0; i < MAX; i++) {
printf("%d ", intArray[i]);
}
printf("\n");
}

Output:

SHAMI SRIVASTAVA
2200321540156

You might also like