Unit_5:
1. Program to Implement Bubble Sort
C Program:
CopyEdit
#include <stdio.h>
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]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
void printArray(int arr[], int n) {
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
printf("\n");
int main() {
int arr[] = {35, 25, 55, 5, 45, 15};
int n = sizeof(arr) / sizeof(arr[0]);
printf("Original array: ");
printArray(arr, n);
bubbleSort(arr, n);
printf("Sorted array: ");
printArray(arr, n);
return 0;
2. Program to Implement Selection Sort
C Program:
CopyEdit
#include <stdio.h>
void selectionSort(int arr[], int n) {
for (int i = 0; i < n - 1; i++) {
int minIdx = i;
for (int j = i + 1; j < n; j++) {
if (arr[j] < arr[minIdx])
minIdx = j;
int temp = arr[minIdx];
arr[minIdx] = arr[i];
arr[i] = temp;
void printArray(int arr[], int n) {
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
printf("\n");
int main() {
int arr[] = {30, 20, 50, 10, 5, 25};
int n = sizeof(arr) / sizeof(arr[0]);
printf("Original array: ");
printArray(arr, n);
selectionSort(arr, n);
printf("Sorted array: ");
printArray(arr, n);
return 0;
3. Program to Implement Insertion Sort
C Program:
c
CopyEdit
#include <stdio.h>
void insertionSort(int arr[], int n) {
for (int i = 1; i < n; i++) {
int key = arr[i];
int j = i - 1;
while (j >= 0 && arr[j] > key) {
arr[j + 1] = arr[j];
j--;
arr[j + 1] = key;
void printArray(int arr[], int n) {
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
printf("\n");
int main() {
int arr[] = {35, 25, 55, 5, 45, 15};
int n = sizeof(arr) / sizeof(arr[0]);
printf("Original array: ");
printArray(arr, n);
insertionSort(arr, n);
printf("Sorted array: ");
printArray(arr, n);
return 0;
4. Program to Implement Linear Search
C Program:
CopyEdit
#include <stdio.h>
int linearSearch(int arr[], int n, int key) {
for (int i = 0; i < n; i++) {
if (arr[i] == key)
return i;
return -1;
int main() {
int arr[] = {10, 20, 30, 40, 50};
int n = sizeof(arr) / sizeof(arr[0]);
int key;
printf("Enter the element to search: ");
scanf("%d", &key);
int result = linearSearch(arr, n, key);
if (result != -1)
printf("Element found at index %d\n", result);
else
printf("Element not found\n");
return 0;
5. Program to Implement Binary Search
C Program:
CopyEdit
#include <stdio.h>
int binarySearch(int arr[], int n, int key) {
int low = 0, high = n - 1;
while (low <= high) {
int mid = (low + high) / 2;
if (arr[mid] == key)
return mid;
else if (arr[mid] < key)
low = mid + 1;
else
high = mid - 1;
}
return -1;
int main() {
int arr[] = {5, 10, 15, 20, 25, 30};
int n = sizeof(arr) / sizeof(arr[0]);
int key;
printf("Enter the element to search: ");
scanf("%d", &key);
int result = binarySearch(arr, n, key);
if (result != -1)
printf("Element found at index %d\n", result);
else
printf("Element not found\n");
return 0;
6. Algorithm for Bubble Sort
1. Start
2. Take an array arr[] of size n.
3. Repeat for i = 0 to n - 1:
o Repeat for j = 0 to n - i - 1:
▪ If arr[j] > arr[j + 1], swap them.
4. End
7. Algorithm for Selection Sort
1. Start
2. Take an array arr[] of size n.
3. Repeat for i = 0 to n - 1:
o Set minIdx = i.
o Repeat for j = i + 1 to n - 1:
▪ If arr[j] < arr[minIdx], set minIdx = j.
o Swap arr[i] and arr[minIdx].
4. End
8. Algorithm for Insertion Sort
1. Start
2. Take an array arr[] of size n.
3. Repeat for i = 1 to n - 1:
o Set key = arr[i] and j = i - 1.
o While j >= 0 and arr[j] > key:
▪ Shift arr[j] to arr[j + 1].
▪ Decrement j.
o Set arr[j + 1] = key.
4. End
9. Algorithm for Linear Search
1. Start
2. Take an array arr[] of size n.
3. Take a key k to search.
4. Repeat for i = 0 to n - 1:
o If arr[i] == k, return index i.
5. Return -1 if not found.
6. End
10. Algorithm for Binary Search
1. Start
2. Sort the array arr[].
3. Set low = 0, high = n - 1.
4. While low <= high:
o Set mid = (low + high) / 2.
o If arr[mid] == k, return mid.
o If arr[mid] < k, set low = mid + 1.
o Else, set high = mid - 1.
5. Return -1 if not found.
6. End
11. Bubble Sort Order After Each Iteration
Elements: 35, 25, 55, 5, 45, 15
• Iteration 1: 25, 35, 5, 45, 15, 55
• Iteration 2: 25, 5, 35, 15, 45, 55
• Iteration 3: 5, 25, 15, 35, 45, 55
• Iteration 4: 5, 15, 25, 35, 45, 55
• Iteration 5: 5, 15, 25, 35, 45, 55
12. Selection Sort Order After Each Iteration
Elements: 30, 20, 50, 10, 5, 25
• Iteration 1: 5, 20, 50, 10, 30, 25
• Iteration 2: 5, 10, 50, 20, 30, 25
• Iteration 3: 5, 10, 20, 50, 30, 25
• Iteration 4: 5, 10, 20, 25, 30, 50
• Iteration 5: 5, 10, 20, 25, 30, 50