Basic Sorting Algorithm |
Sorting is used to organise data.
Example :- array[] = { 5,4,6,7,8}; array[] = {4,5,6,7,8};
2. Selection sort
3. Insertion sort
If the first element is greater, then is second element there are swap.
Types of Sorting
1. Bubble sort2. Selection sort
3. Insertion sort
Bubble sort
It is simplest sorting but slow, It Compare nearest elements repeatedly.Steps for bubble sorting
Start from first element and compare at first and second element.If the first element is greater, then is second element there are swap.
Now, compare second and third element and swap them if there are not in order.
This process repeated the above until the last elements.
Bubble sort |
Example:
#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]) {
// Swap
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[] = {5, 3, 8, 1, 2};
int n = sizeof(arr) / sizeof(arr[0]);
bubbleSort(arr, n);
printf("Sorted array: ");
printArray(arr, n);
return 0;
}
Result: Sorted array: 1 2 3 5 8
Selection sort
In this sort, set the 1st elements as sorted and Find the smallest element and swap it with the first element, then repeat.Steps for selection sort
Set the first element as the sorted and remaining elements as unsorted.
Compare the sorted elements with the remaining unsorted elements and swap with the sorted element.
Repeat this step for all remaining elements.
After each element as swap on the sorted elements of unsorted list.
After each element as swap on the sorted elements of unsorted list.
Example
#include <stdio.h>
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
int temp = arr[minIndex];
arr[minIndex] = 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[] = {5, 3, 8, 1, 2};
int n = sizeof(arr) / sizeof(arr[0]);
selectionSort(arr, n);
printf("Sorted array: ");
printArray(arr, n);
return 0;
}
Result: Sorted array: 1 2 3 5 8
Insertion sort
In this sort, Take one element and insert it at the correct position in the sorted part of the array.Steps for insertion sort:
Divide into two part first element as sorted and remaining elements as unsorted.Now compare the sorted element with the unsorted elements.
Find the correct position and swap the sorted elements and comparing element
Repeat this process until the remain all elements of unsorted part.
Insertion sort |
Example
#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[] = {5, 3, 8, 1, 2};
int n = sizeof(arr) / sizeof(arr[0]);
insertionSort(arr, n);
printf("Sorted array: ");
printArray(arr, n);
return 0;
}
Result: Sorted array: 1 2 3 5 8