Daa Lab File

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 8

Experiment – 1

Linear Search and Binary Search Using Recursion

1(a). Linear Search Using Recursion


 Algorithm for Linear Search using Recursive.

1) Start
2) Base Case: If the array is empty (i.e., n is 0), return -1 (not found).
3) Compare Element: Compare the key with the current element at index 0 in the array.
4) If Match: If the key matches the current element, return the index 0.
5) Recursively Search: If the key does not match the current element, recursively call the linearsearch
function with the following parameters:
6) The rest of the array (i.e., arr[1..n-1])
7) The updated size of the array (n-1)
8) Return the result of the recursive call.
9) End

 Pseudocode for Linear Search using Recursive


LinearSearch (array, index, key):
if index < 0:
return -1;
if item = key:
return index
return LinearSearch (array, index-1, key)

 Program for Linear Search using Recursive


#include <stdio.h>

int linearSearch(int arr[], int size, int key){


if (size == 0) {
return -1;
}
if (arr[size - 1] == key) {

return size - 1;
}
return linearSearch(arr, size - 1, key);
}
int main()
{
int arr[] = { 5, 15, 6, 9, 4 };
int key = 4;
int index
= linearSearch(arr, sizeof(arr) / sizeof(int), key);
if (index == -1) {
printf("Key not found in the array.\n");
}
else {
printf("The element %d is found at %d index of the "
"given array \n",
key, index);
}
return 0;
}

 OUTPUT.
Experiment – 2
BUBBLE SORT
 Algorithm for Bubble Sort.

1) Start
2) Read the array of given items from the user.
3) Take the first element(index = 0), compare the current element with the next element.
4) If the current element is greater than the next element, swap them.
5) Else,
6) If the current element is less than the next element, then move to the next element.
7) Repeat Step 3 to Step 5 until all elements are sorted.
8) End

Step 7: Stop
 Pseudocode for Bubble Sort.
begin BubbleSort(arr)
for all array elements
if arr[i] > arr[i+1]
swap(arr[i], arr[i+1])
end if
end for
return arr
end BubbleSort

 Program for 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++) {
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;
}

 OUTPUT
Experiment – 3
SELECTION SORT
 Algorithm for Selection Sort.
1. Set MIN to location 0.
2. Search the minimum element in the list.
3. Swap with value at location MIN.
4. Increment MIN to point to next element.
5. Repeat until the list is sorted.

 Pseudocode for Selection Sort. fori← 1 to n-1 do


min j ←i;
min x ← A[i]
for j ←i + 1 to n do
if A[j] < min x then
min j ← j
min x ← A[j]
A[min j] ← A [i]
A[i] ← min x

 Program for Selection 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;

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

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
Experiment – 4
INSERTION SORT
 Algorithm for Insertion Sort.
Step 1 - Start
Step 2 − If it is the first element, it is already sorted. return 1;
Step 3 − Pick next element
Step 4 − Compare with all elements in the sorted sub-list
Step 5 − Shift all the elements in the sorted sub-list that is greater than the value to be sorted
Step 6 − Insert the value
Step 7 − Repeat until list is sorted
Step 8 - End

 Pseudocode for Insertion Sort.


Algorithm: Insertion-Sort(A)
for j = 2 to A.length
key = A[j]
i=j–1
while i > 0 and A[i] > key
A[i + 1] = A[i]
i = i -1
A[i + 1] = key
 Program for Insertion Sort.

#include <math.h>
#include <stdio.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");
}

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

insertionSort(arr, n);
printArray(arr, n);

return 0;
}

 OUTPUT

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy