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

DAA Exp1

Uploaded by

singhranjeet9478
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)
7 views

DAA Exp1

Uploaded by

singhranjeet9478
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/ 8

Experiment 1

Aim: write a program for Linear Search

Source Code:

#include <stdio.h>

int linearSearch(int a[], int n, int val) {

for (int i = 0; i < n; i++)

if (a[i] == val)

return i+1; }

return -1; }

int main() {

int a[30],i,n,val;

printf("Enter size of array: ");

scanf("%d",&n);

printf("Enter %d elements in the array : ", n);

for(i=0;i<n;i++)

scanf("%d", &a[i]); }

printf("Enter element to be searched ");

scanf("%d",&val);

int res = linearSearch(a, n, val);

if (res == -1)

printf("\nElement is not present in the array");

else

printf("\nElement is present at %d position of array", res);

return 0; }

Output:
Experiment 2
Aim: write a program for Binary Search

Source Code:

#include <stdio.h>

int binarySearch(int array[], int x, int low, int high) {

if (high >= low)

int mid = low + (high - low) / 2;

if (array[mid] == x)

return mid;

if (array[mid] > x)

return binarySearch(array, x, low, mid - 1);

return binarySearch(array, x, mid + 1, high);

return -1;

int main() {

int a[30],i,n,val;

printf("Enter size of array: ");

scanf("%d",&n);

printf("Enter %d elements in the array : ", n);

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

scanf("%d", &a[i]);

printf("Enter element to be searched ");

scanf("%d",&val);

int result = binarySearch(a, val, 0, n - 1);

if (result == -1)

printf("Not found");

else

printf("Element is found at index %d", result);


return 0;

Output:
Experiment 3
Aim: write a program for Bubble Sort.

Source Code:

#include <stdio.h>

void bubble_sort(int arr[], int n) {

int i, j;

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

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

if (arr[j] > arr[j + 1]) {

int temp = arr[j];

arr[j] = arr[j + 1];

arr[j + 1] = temp; } }

} }

int main() {

int arr[40],i,n;

printf("Enter size of array: ");

scanf("%d",&n);

printf("Enter %d elements in the array : ", n);

for(i=0;i<n;i++)

scanf("%d", &arr[i]); }

bubble_sort(arr, n);

printf("Sorted array: \n");

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

printf("%d ", arr[i]); }

return 0;

Output:
Experiment 4
Aim: write a program for Quick Sort

Source Code:

#include <stdio.h>

int partition (int a[], int start, int end)

int pivot = a[end];

int i = (start - 1);

for (int j = start; j <= end - 1; j++)

if (a[j] < pivot)

i++;

int t = a[i];

a[i] = a[j];

a[j] = t;

int t = a[i+1];

a[i+1] = a[end];

a[end] = t;

return (i + 1);

void quick(int a[], int start, int end)

if (start < end)

int p = partition(a, start, end);

quick(a, start, p - 1);


quick(a, p + 1, end);

int main() {

int arr[40],i,n;

printf("Enter size of array: ");

scanf("%d",&n);

printf("Enter %d elements in the array : ", n);

for(i=0;i<n;i++)

scanf("%d", &arr[i]);

quick(arr, 0, n - 1);

printf("Sorted array: \n");

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

printf("%d ", arr[i]);

return 0;

Output:
Experiment 5
Aim: write a program for Merge Sort

Source Code:

#include <stdio.h>

void merge(int a[], int beg, int mid, int end)

int i, j, k;

int n1 = mid - beg + 1;

int n2 = end - mid;

int LeftArray[n1], RightArray[n2];

for (int i = 0; i < n1; i++)

LeftArray[i] = a[beg + i];

for (int j = 0; j < n2; j++)

RightArray[j] = a[mid + 1 + j];

i = 0;

j = 0;

k = beg;

while (i < n1 && j < n2)

{ if(LeftArray[i] <= RightArray[j])

{ a[k] = LeftArray[i];

i++; }

else

{ a[k] = RightArray[j];

j++; }

k++; }

while (i<n1)

{ a[k] = LeftArray[i];

i++;

k++; }

while (j<n2)

{ a[k] = RightArray[j];
j++;

k++; } }

void mergeSort(int a[], int beg, int end)

if (beg < end)

int mid = (beg + end) / 2;

mergeSort(a, beg, mid);

mergeSort(a, mid + 1, end);

merge(a, beg, mid, end); } }

int main() {

int arr[40],i,n;

printf("Enter size of array: ");

scanf("%d",&n);

printf("Enter %d elements in the array : ", n);

for(i=0;i<n;i++)

scanf("%d", &arr[i]);

mergeSort(arr, 0, n - 1);

printf("Sorted array: \n");

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

printf("%d ", arr[i]);

return 0;

Output:

You might also like