Wa0008.
Wa0008.
ES-CS201
Introduction
Bubble Sort
Insertion Sort
Selection Sort
Quick Sort
Merge Sort
INTRODUCTION
Definition:
Sorting is the process of arranging elements in a specific order
(ascending/descending).
Why Sorting?
• Efficient searching (e.g., Binary Search)
• Data organization
• Faster processing
Algorithm Code
1.Iterate through the array. void bubbleSort(int arr[], int n) {
2.Compare adjacent elements. for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++)
3.Swap if necessary. {
if (arr[j] > arr[j + 1]) {
4.Repeat until the array is sorted. int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
INSERTION SORT
Definition
•Builds the sorted array one item at a time.
•Picks an element and places it in the correct position.
Algorithm Code
1.Start with the second element. void insertionSort(int arr[], int n) {
for (int i = 1; i < n; i++) {
2.Compare it with previous elements. int key = arr[i];
3.Insert it in the correct position. int j = i - 1;
while (j >= 0 && arr[j] > key) {
4.Repeat for all elements. arr[j + 1] = arr[j];
j--;
}
arr[j + 1] = key;
}
}
SELECTION SORT
Definition
•Finds the smallest element and moves it to the beginning.
•Repeats for the remaining unsorted part.
Algorithm Code
1.Find the minimum element. void selectionSort(int arr[], int n) {
for (int i = 0; i < n - 1; i++) {
2.Swap it with the first unsorted element. int minIndex = i;
3.Move to the next unsorted element. for (int j = i + 1; j < n; j++)
{
4.Repeat until sorted. if (arr[j] < arr[minIndex])
{
minIndex = j;
}
}
int temp = arr[minIndex];
arr[minIndex] = arr[i];
arr[i] = temp;
QUICK SORT
Definition Code
•Uses divide and conquer. int partition(int arr[], int low, int high) {
int pivot = arr[high];
•Picks a pivot, partitions the array int i = low - 1;
for (int j = low; j < high; j++) {
•Sorts recursively. if (arr[j] < pivot) {
i++;
int temp = arr[i];
arr[i] = arr[j];
Algorithm }
arr[j] = temp;
1.Choose a pivot. }
int temp = arr[i + 1];
2.Partition the array into two subarrays. arr[i + 1] = arr[high];
arr[high] = temp;
3.Recursively apply quicksort. }
return i + 1;
4.Combine sorted subarrays. void quickSort(int arr[], int low, int high) {
if (low < high) {
int pi = partition(arr, low, high);
quickSort(arr, low, pi - 1);
quickSort(arr, pi + 1, high);
}
}
MERGE SHORT
Definition Code
void merge(int arr[], int l, int m, int r) {
•Uses divide and conquer. int n1 = m - l + 1;
int n2 = r - m;
•Splits the array, sorts each half, and int L[n1], R[n2];
for (int i = 0; i < n1; i++)
merges them. L[i] = arr[l + i];
for (int i = 0; i < n2; i++)
R[i] = arr[m + 1 + i];
int i = 0, j = 0, k = l;
while (i < n1 && j < n2) {
Algorithm if (L[i] <= R[j]) {
arr[k] = L[i];
1.Divide the array into two halves. i++;
} else {
2.Recursively sort each half. arr[k] = R[j];
j++;
}
3.Merge sorted halves. k++;
}
while (i < n1) arr[k++] = L[i++];
while (j < n2) arr[k++] = R[j++];
}
void mergeSort(int arr[], int l, int r) {
if (l < r) {
int m = l + (r - l) / 2;
mergeSort(arr, l, m);
mergeSort(arr, m + 1, r);
merge(arr, l, m, r);
}
}
CONCLUSION
Different sorting algorithms suit different use cases.
Quick Sort and Merge Sort are efficient for large data sets.
Bubble Sort, Insertion Sort, and Selection Sort are useful for small or
nearly sorted data.
Thank You