Compares in bubbles (pairs), from the beginning of the array to the end. If the first element of the bubble is bigger, the 2 elements will be swapped.
The above process is repeated through the whole array until there are no more swaps to do.
Its complexity:
void bubbleSort(std::vector<int>& arr) {
int n = arr.size();
bool swapped; // Flag to check if any swaps occurred in a pass
for (int i = 0; i < n - 1; ++i) {
swapped = false; // Reset swapped flag for each pass
for (int j = 0; j < n - 1 - i; ++j) {
// Compare adjacent elements
if (arr[j] > arr[j + 1]) {
// Swap if they are in the wrong order
std::swap(arr[j], arr[j + 1]);
swapped = true; // Mark that a swap occurred
}
}
// If no two elements were swapped in inner loop, then array is sorted
if (swapped == false) {
break;
}
}
}
Quick Excersice:
Given the arr
array, calculate the number of comparisons and the number of swaps
std::vector<int> myVector = {64, 347, 5, 12, 212, 11, 920, 64, 37, 25, 72, 22, 96, 90};
Source Code: bubble_sort.cpp
It will search for the minimum (or maximum) value in the array, then it will go through the rest of the array and if it finds a smaller value, they will swap their position.
The above process is repeated with all the array, previous sorted values will not be visited anymore.
Its complexity:
void selectionSort(std::vector<int>& arr) {
int n = arr.size();
for (int i = 0; i < n - 1; ++i) {
// Find the minimum element in the unsorted part of the array
int min_idx = i;
for (int j = i + 1; j < n; ++j) {
if (arr[j] < arr[min_idx]) {
min_idx = j;
}
}
// Swap the found minimum element with the first element of the unsorted part
if (min_idx != i) {
std::swap(arr[min_idx], arr[i]);
}
}
}
Quick Excersice
Is it really
Source Code: selection_sort.cpp
The process of sorting an array with insertion sort is similar to how a person might sort a hand of playing cards
Its complexity:
The algorithm starts with the first element, which is considered the sorted portion of the array.
It then takes the next element (from the unsorted portion) and compares it with the elements in the sorted portion, moving from right to left.
As it compares, it shifts any element larger than the current value one position to the right to create a space for insertion.
The current element is then inserted into its correct position.
This process repeats for each element until the entire array is sorted.
void insertionSort(std::vector<int>& arr) {
int n = arr.size();
for (int i = 1; i < n; ++i) {
int key = arr[i];
int j = i - 1;
// Move elements of the sorted subarray that are greater
// than the key to one position ahead of their current position.
while (j >= 0 && arr[j] > key) {
arr[j + 1] = arr[j];
j = j - 1;
}
arr[j + 1] = key;
}
}
Quick Excersice
What would the worst case scenario?
Write a 5 elements array. You may need to verify it
arr = { , , , , }
Source Code: insertion_sort.cpp
This is a recursive algorithm that divides the array by 2 and then sort each one of the in a recursive way.
This algorithm follows the divide-and-conquer principle.
Its complexity:
// Recursively divides the array and sorts subarrays
void mergeSort(std::vector<int>& arr, int low, int high) {
if (low < high) {
int mid = low + (high - low) / 2; // Calculate the middle index
// Recursively sort the left and right halves
mergeSort(arr, low, mid);
mergeSort(arr, mid + 1, high);
// Merge the sorted halves
merge(arr, low, mid, high);
}
}
Source code: merge_sort.cpp
// Merges two sorted subarrays into a single sorted array
void merge(std::vector<int>& arr, int low, int mid, int high) {
std::vector<int> temp; // Temporary array for merging
int left = low; // Starting index of the left subarray
int right = mid + 1; // Starting index of the right subarray
// Merge elements into the temporary array in sorted order
while (left <= mid && right <= high) {
if (arr[left] <= arr[right]) {
temp.push_back(arr[left]);
left++;
} else {
temp.push_back(arr[right]);
right++;
}
}
.
.
.
}
// Merges two sorted subarrays into a single sorted array
void merge(std::vector<int>& arr, int low, int mid, int high) {
.
.
.
// Copy any remaining elements from the left subarray
while (left <= mid) {
temp.push_back(arr[left]);
left++;
}
// Copy any remaining elements from the right subarray
while (right <= high) {
temp.push_back(arr[right]);
right++;
}
// Copy the sorted elements from the temporary array back to the original array
for (int i = low; i <= high; i++) {
arr[i] = temp[i - low];
}
}
Quick Excercise
How may sub-arrays would be created in the following array:
arr = [20,19,18,17,16,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1]
Source code: merge_sort.cpp
Yet another divide-and-conquer algorithm, but with a pivot
The election of the pivot is free, a simple pivot's choosing technique is just taking the first or the last element of the array.
Then, create 2 sub-arrays, one will contain the smaller values, and other with bigger values than the pivot
Its Complexity
// Main Quicksort function
void quickSort(std::vector<int>& arr, int low, int high) {
if (low < high) {
// pi is partitioning index, arr[pi] is now at right place
int pi = partition(arr, low, high);
// Separately sort elements before partition and after partition
quickSort(arr, low, pi - 1);
quickSort(arr, pi + 1, high);
}
}
Source code: quick_sort.cpp
// Function to partition the array and return the pivot's final index
int partition(std::vector<int>& arr, int low, int high) {
int pivot = arr[high]; // Choosing the last element as the pivot
int i = (low - 1); // Index of smaller element
for (int j = low; j <= high - 1; j++) {
// If current element is smaller than or equal to pivot
if (arr[j] <= pivot) {
i++; // Increment index of smaller element
std::swap(arr[i], arr[j]);
}
}
std::swap(arr[i + 1], arr[high]);
return (i + 1);
}
Quick Excercise
Let's figure out what's the best pivot choosing technique (
arr = [32,14,67,84,39,96,19,32,52,59,1,30,14,97,96,73,55,46,16,19]
Source code: quick_sort.cpp
For a 10,000,000 randomized integers array
HeapSort
, ShellSort
and BucketSort
algorithmsBubbleSort
, SelectionSort
, InsertionSort
, QuickSort
and MergeSort
to your programThis material is genereated thanks to some extracts from following resources:
AI Overview