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

DS lab

The document contains C programs for reversing an array, implementing searching techniques (linear and binary search), and sorting techniques (bubble, selection, and insertion sort). Each section includes code examples along with expected output for clarity. The programs demonstrate fundamental algorithms in C programming.

Uploaded by

bharathisatya510
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

DS lab

The document contains C programs for reversing an array, implementing searching techniques (linear and binary search), and sorting techniques (bubble, selection, and insertion sort). Each section includes code examples along with expected output for clarity. The programs demonstrate fundamental algorithms in C programming.

Uploaded by

bharathisatya510
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

i)Write a program to reverse an array.

ii) C Programs to implement the Searching Techniques – Linear & Binary Search
iii) C Programs to implement Sorting Techniques – Bubble, Selection and Insertion
Sort

i)Write a program to reverse an array.

#include <stdio.h>

// Function to reverse an array


void reverseArray(int arr[], int n) {
int start = 0; // Starting index
int end = n - 1; // Ending index

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

// Move the indices towards the center


start++;
end--;
}
}

int main() {
int arr[] = {1, 2, 3, 4, 5};
int n = sizeof(arr) / sizeof(arr[0]);

printf("Original Array: ");


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

// Reverse the array


reverseArray(arr, n);

printf("Reversed Array: ");


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

OUT PUT: Original Array: 1 2 3 4 5


Reversed Array: 5 4 3 2 1

ii) C Programs to implement the Searching Techniques – Linear & Binary Search

Linear search :

#include <stdio.h>

// Function to perform Linear Search

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

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

if (arr[i] == key) {

return i; // Return the index of the key if found

return -1; // Return -1 if key is not found

int main() {

int arr[] = {10, 20, 30, 40, 50};

int n = sizeof(arr) / sizeof(arr[0]);

int key = 30;

int result = linearSearch(arr, n, key);

if (result != -1) {

printf("Element found at index: %d\n", result);

} else {

printf("Element not found in the array.\n"); }


return 0;

OUT PUT : Element found at index: 2

Binary search : #include <stdio.h>


// Function to perform Binary Search

int binarySearch(int arr[], int left, int right, int key) {

while (left <= right) {

int mid = left + (right - left) / 2;

// Check if the key is at the middle

if (arr[mid] == key) {

return mid;

// If key is greater, ignore the left half

if (arr[mid] < key) {

left = mid + 1;

// If key is smaller, ignore the right half

else {

right = mid - 1;

} return -1; // Return -1 if key is not found

}int main() {

int arr[] = {10, 20, 30, 40, 50};

int n = sizeof(arr) / sizeof(arr[0]);

int key = 40;

int result = binarySearch(arr, 0, n - 1, key);

if (result != -1) {
printf("Element found at index: %d\n", result);

} else {

printf("Element not found in the array.\n");

return 0;

OUT PUT : Element found at index: 3

iii) C Programs to implement Sorting Techniques – Bubble, Selection and Insertion Sort

BUBBLE Sort :

#include <stdio.h>

// Function to perform Bubble Sort

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

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

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

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

// Swap arr[j] and arr[j + 1]

int temp = arr[j];

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

arr[j + 1] = temp;

int main() {

int arr[] = {64, 34, 25, 12, 22, 11, 90};

int n = sizeof(arr) / sizeof(arr[0]);

printf("Original Array: ");

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


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

printf("\n");

bubbleSort(arr, n);

printf("Sorted Array (Bubble Sort): ");

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

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

printf("\n");

return 0;

OUT PUT : Original Array: 64 34 25 12 22 11 90

Sorted Array (Bubble Sort): 11 12 22 25 34 64 90

Selection Sort :
#include <stdio.h>

// Function to perform Selection Sort


void selectionSort(int arr[], int n) {
for (int i = 0; i < n - 1; i++) {
int minIndex = i;
for (int j = i + 1; j < n; j++) {
if (arr[j] < arr[minIndex]) {
minIndex = j;
}
}
// Swap arr[i] and arr[minIndex]
int temp = arr[i];
arr[i] = arr[minIndex];
arr[minIndex] = temp;
}
}

int main() {
int arr[] = {64, 25, 12, 22, 11};
int n = sizeof(arr) / sizeof(arr[0]);

printf("Original Array: ");


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

selectionSort(arr, n);

printf("Sorted Array (Selection Sort): ");


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

return 0;
}

OUT PUT : Original Array: 64 25 12 22 11


Sorted Array (Selection Sort): 11 12 22 25 64
Insertion Sort :
#include <stdio.h>

// Function to perform Insertion Sort


void insertionSort(int arr[], int n) {
for (int i = 1; i < n; i++) {
int key = arr[i];
int j = i - 1;

// Move elements greater than key to one position


ahead
while (j >= 0 && arr[j] > key) {
arr[j + 1] = arr[j];
j--;
}
arr[j + 1] = key;
}
}

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

printf("Original Array: ");


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

insertionSort(arr, n);

printf("Sorted Array (Insertion Sort): ");


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

return 0;
}
OUT PUT : Original Array: 12 11 13 5 6
Sorted Array (Insertion Sort): 5 6 11 12 13

You might also like