DS lab
DS lab
ii) C Programs to implement the Searching Techniques – Linear & Binary Search
iii) C Programs to implement Sorting Techniques – Bubble, Selection and Insertion
Sort
#include <stdio.h>
// Swap elements from start and end until they meet in the middle
while (start < end) {
// Swap arr[start] and arr[end]
int temp = arr[start];
arr[start] = arr[end];
arr[end] = temp;
int main() {
int arr[] = {1, 2, 3, 4, 5};
int n = sizeof(arr) / sizeof(arr[0]);
ii) C Programs to implement the Searching Techniques – Linear & Binary Search
Linear search :
#include <stdio.h>
if (arr[i] == key) {
int main() {
if (result != -1) {
} else {
if (arr[mid] == key) {
return mid;
left = mid + 1;
else {
right = mid - 1;
}int main() {
if (result != -1) {
printf("Element found at index: %d\n", result);
} else {
return 0;
iii) C Programs to implement Sorting Techniques – Bubble, Selection and Insertion Sort
BUBBLE Sort :
#include <stdio.h>
arr[j + 1] = temp;
int main() {
printf("\n");
bubbleSort(arr, n);
printf("\n");
return 0;
Selection Sort :
#include <stdio.h>
int main() {
int arr[] = {64, 25, 12, 22, 11};
int n = sizeof(arr) / sizeof(arr[0]);
selectionSort(arr, n);
return 0;
}
int main() {
int arr[] = {12, 11, 13, 5, 6};
int n = sizeof(arr) / sizeof(arr[0]);
insertionSort(arr, n);
return 0;
}
OUT PUT : Original Array: 12 11 13 5 6
Sorted Array (Insertion Sort): 5 6 11 12 13