Module 5
Module 5
Sorting:
1)Bubble sort:
Bubble Sort is a simple sorting algorithm that repeatedly steps through the list, compares adjacent
elements, and swaps them if they are in the wrong order.
The biggest (or smallest) elements "bubble" to the end (or start) after each pass.
Example:
Step-by-Step:
Pass 1:
• Compare 5 and 3.
→ 5 > 3, so swap → [3, 5, 8, 4, 2]
• Compare 5 and 8.
→ 5 < 8, so no swap.
• Compare 8 and 4.
→ 8 > 4, so swap → [3, 5, 4, 8, 2]
• Compare 8 and 2.
→ 8 > 2, so swap → [3, 5, 4, 2, 8]
Pass 2:
• Compare 3 and 5.
→ 3 < 5, so no swap.
• Compare 5 and 4.
→ 5 > 4, so swap → [3, 4, 5, 2, 8]
• Compare 5 and 2.
→ 5 > 2, so swap → [3, 4, 2, 5, 8]
Pass 3:
• Compare 3 and 4.
→ 3 < 4, so no swap.
• Compare 4 and 2.
→ 4 > 2, so swap → [3, 2, 4, 5, 8]
Pass 4:
• Compare 3 and 2.
→ 3 > 2, so swap → [2, 3, 4, 5, 8]
[2, 3, 4, 5, 8]
Program:
#include <stdio.h>
int main() {
int arr[MAXSIZE];
int n, i;
scanf("%d", &n);
scanf("%d", &arr[i]);
}
printf("\nArray before sorting:\n");
printf("\n");
return 0;
int i, j, temp;
temp = arr[j];
arr[j + 1] = temp;
Selection Sort
Step-by-Step:
Pass 1:
Pass 2:
Pass 3:
• Swap 4 with 8.
Pass 4:
• Swap 5 with 8.
[2, 3, 4, 5, 8]
Program:
#include <stdio.h>
int main() {
scanf("%d", &maxsize);
scanf("%d", &elements[i]);
printf("\n");
selection(elements, maxsize);
return 0;
min_idx = i;
min_idx = j;
temp = elements[i];
elements[i] = elements[min_idx];
elements[min_idx] = temp;
Insertion Sort:
Insertion Sort builds the final sorted array one element at a time.
It takes one element, finds the correct place for it in the sorted part, and inserts it there.
Imagine sorting playing cards in your hand — you pick one card at a time and insert it at the correct
position.
Example:
Step-by-Step:
Pass 1: Insert 3
• Take 3.
• Compare with 5.
→ 3 < 5, so move 5 to the
sorted.
Pass 2: Insert 8
• Take 8.
• Compare with 5.
→ 8 > 5, so no shifting
[3, 5, 8] is sorted.
Pass 3: Insert 4
• Take 4.
• Compare with 8.
→ 4 < 8 → move 8 right.
• Compare with 5.
→ 4 < 5 → move 5 right.
• Compare with 3.
→ 4 > 3 → stop.
• Insert 4 after 3.
[3, 4, 5, 8] is sorted.
Pass 4: Insert 2
• Take 2.
[2, 3, 4, 5, 8]
Program:
#include <stdio.h>
int main() {
int i;
printf("\nHow many elements you want to sort: ");
scanf("%d", &maxsize);
scanf("%d", &elements[i]);
printf("\n");
insertionsort(elements, maxsize);
printf("\n");
return 0;
{ int i, j, index;
index = elements[i];
j = i;
while (j > 0 && elements[j - 1] > index)
elements[j] = index;
}
Searching:
Linear Search
Example:
Step-by-Step:
Done!
1. 5 → no
2. 3 → no
3. 8 → no
4. 4 → no
5. 2 → no
#include <stdio.h>
if (arr[i] == target)
int main() {
int target;
scanf("%d", &target);
if (result != -1)
else
return 0;
}
Binary Search
• If equal → Found!
Important:
Array must be sorted first!
Example:
Sorted array:
[2, 3, 4, 5, 8]
Step-by-Step:
1. Start:
o Left = 0 (value 2)
o Right = 4 (value 8)
2. Compare:
o Target 5 > 4
o Update: Left = 3
3. Next Step:
o Left = 3 (value 5)
o Right = 4 (value 8)
o Middle = (3+4)//2 = 3 → value is 5
4. Compare:
o Target 5 == 5
o Found at index 3!
Search for 7:
Step Action
empty
Program:
#include <stdio.h>
if (arr[mid] == target)
return mid;
low = mid + 1;
else
high = mid - 1;
}
return -1; // Not found
int main() {
int arr[] = {10, 20, 30, 40, 50, 60, 70}; // Must be sorted
int target;
scanf("%d", &target);
if (result != -1)
else
return 0;