0% found this document useful (0 votes)
12 views

DAA

The document contains multiple C programs demonstrating various search and sorting algorithms, including Linear Search, Binary Search, Heap Sort, Merge Sort, Selection Sort, Insertion Sort, and Quick Sort. Each program includes code snippets, output examples, and explanations of the algorithms used. The programs showcase how to implement these algorithms and their results when executed.

Uploaded by

bantijk7331
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 views

DAA

The document contains multiple C programs demonstrating various search and sorting algorithms, including Linear Search, Binary Search, Heap Sort, Merge Sort, Selection Sort, Insertion Sort, and Quick Sort. Each program includes code snippets, output examples, and explanations of the algorithms used. The programs showcase how to implement these algorithms and their results when executed.

Uploaded by

bantijk7331
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/ 11

1. Program for Recursive Binary & Linear Search.

2. #include<stdio.h>
3. #include<conio.h>
4. int Linear_search(int a[],int i,int key){
5. if(i<0){
6. return -1;
7. }
8. if(a[i]==key){
9. return i;
10. }
11. return Linear_search(a,i-1,key);
12.
13. }
14.
15. int main(){
16. int a[]={1,2,4,5,6,7};
17.
18. int i=sizeof(a)/sizeof(a[0]);
19. printf("Array is\n");
20. for(int k=0;i<i;k++){
21. printf("%d\t",a[k]);
22. }
23.
24. int key;
25. printf("Enter the ele==");
26. scanf("%d",&key);
27. int result_of_Linear=Linear_search(a,i-1,key);
28.
29. (result_of_Linear == -1) ? printf("Element not found") : printf("Element
found at index: %d", result_of_Linear);
30. return 0;
31. }
OUTPUT

Array is

Enter the ele==2

Element found at index: 1


#include <stdio.h>

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


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

if (arr[mid] == x) return mid;


if (arr[mid] > x) return binarySearch(arr,l,mid,x);
return binarySearch(arr, mid+1, r, x);
}
return -1;
}
int main(void)
{
int arr[] = {2, 3, 4, 10, 40};
int n = sizeof(arr)/ sizeof(arr[0]);

printf("Array Is\n");
for(int k=0;k<n;k++){
printf("%d\t",arr[k]);
}
int x = 10;
int result = binarySearch(arr, 0, n-1, x);
(result == -1)? printf("\nElement is not present in array"): printf("\nElement is
present at index %d", result);
return 0;
}

OUTPUT

Array Is

2 3 4 10 40

Element is present at index 3


2. Program for Heap Sort.

#include <stdio.h>
void swap(int* a, int* b)
{

int temp = *a;


*a = *b;
*b = temp;
}

void heapify(int arr[], int N, int i)


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

if (left < N && arr[left] > arr[largest])

largest = left;
if (right < N && arr[right] > arr[largest])

largest = right;

if (largest != i) {

swap(&arr[i], &arr[largest]);
heapify(arr, N, largest);
}
}

void heapSort(int arr[], int N)


{

// Build max heap


for (int i = N / 2 - 1; i >= 0; i--)

heapify(arr, N, i);

// Heap sort
for (int i = N - 1; i >= 0; i--) {

swap(&arr[0], &arr[i]);
heapify(arr, i, 0);
}
}

void printArray(int arr[], int N)


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

// Driver's code
int main()
{
int arr[] = { 12, 11, 13, 5, 6, 7 };
int N = sizeof(arr) / sizeof(arr[0]);
printf("Unsorted Array =\n ");
for(int i=0;i<N;i++){
printf("%d\t",arr[i]);
}

// Function call
heapSort(arr, N);
printf("\nSorted array is\n");
printArray(arr, N);

OUTPUT

Unsorted Array =

12 11 13 5 6 7

Sorted array is

5 6 7 11 12 13
3. Program for Merge Sort.

#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;
// Create temp arrays
int L[n1], R[n2];
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++;
}
}
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);
}
}
void printArray(int A[], int size)
{
int i;
for (i = 0; i < size; i++)
printf("%d ", A[i]);
printf("\n");
}
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);

mergeSort(arr, 0, arr_size - 1);

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


printArray(arr, arr_size);
return 0;
}

OUTPUT

Given array is

12 11 13 5 6 7

Sorted array is

5 6 7 11 12 14
4. Program for Selec on Sort.

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

for (i = 0; i < n - 1; i++) {

min_idx = i;
for (j = i + 1; j < n; j++)
if (arr[j] < arr[min_idx])
min_idx = j;
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");
}

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

OUTPUT

Sorted array: 11 12 22 25 64
5.Program for Inser on Sort.

#include <stdio.h>
#include <math.h>
void insertionSort(int arr[], int n)
{
int i, key, j;
for (i = 1; i < n; i++)
{
key = arr[i];
j = i - 1;

while (j >= 0 && arr[j] > key)


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

// Driver code
int main()
{
int arr[] = {12, 11, 13, 5, 6};
int n = sizeof(arr) / sizeof(arr[0]);
printf("Unsorted Array is\n");
for(int k=0;k<n;k++){
printf("%d\t",arr[k]);
}
printf("\n");
insertionSort(arr, n);
printArray(arr, n);

return 0;
}
OUTPUT
OUTPUT

Unsorted Array is
12 11 13 5 6

Sorted Array is

5 6 11 12 13
6. Program for Quick Sort.

#include <stdio.h>
void swap(int* a, int* b)
{
int t = *a;
*a = *b;
*b = t;
}
int partition(int arr[], int low, int high)
{
int pivot = arr[high];
int i = (low - 1);

for (int j = low; j <= high - 1; j++) {


if (arr[j] < pivot) {
i++;
swap(&arr[i], &arr[j]);
}
}
swap(&arr[i + 1], &arr[high]);
return (i + 1);
}
void quickSort(int arr[], int low, int high)
{
if (low < high) {
int pi = partition(arr, low, high);
quickSort(arr, low, pi - 1);
quickSort(arr, pi + 1, high);
}
}

// Driver code
int main()
{
int arr[] = { 10, 7, 8, 9, 1, 5 };
int N = sizeof(arr) / sizeof(arr[0]);
printf("Unsorted Array is\n");
for(int k=0;k<N;k++){
printf("%d\t",arr[k]);
}

// Function call
quickSort(arr, 0, N - 1);
printf("\nSorted array: \n");
for (int i = 0; i < N; i++)
printf("%d ", arr[i]);
return 0;
}

OUTPUT

Unsorted Array is

10 7 8 9 1 5

Sorted array:

1 5 7 8 9 10

You might also like