Daa Lab File
Daa Lab File
Daa Lab File
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
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
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.
#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]);
}
}
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
#include <math.h>
#include <stdio.h>
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