Code samples shown are made with C++ for integer vectors
void BubbleSort(std::vector& a) {
for (int i = 0; i < a.size() - 1; ++i) {
int j = a.size() - 1;
while (j > i) {
if (a[j] < a[j - 1]) std::swap(a[j], a[j - 1]);
j--;
}
}
}
Improved Bubble Sort
void ImprovedBubbleSort(std::vector& a) {
for (int i = 0; i < a.size() - 1; ++i) {
bool isChanged = false;
int j = a.size() - 1;
while (j > i) {
if (a[j] < a[j - 1]) {
std::swap(a[j], a[j - 1]);
isChanged = true;
}
j--;
}
if (!isChanged) return;
}
}
void InsertionSort(std::vector& arr) {
for (int i = 0, j = 0, key = 0; i < arr.size(); i++) {
j = i - 1;
key = arr[i];
while (j >= 0 && arr[j] > key) {
arr[j + 1] = arr[j];
j--;
}
arr[j + 1] = key;
}
}
void SelectionSort(std::vector& a) {
for (int i = 0; i < a.size(); ++i) {
int min = i;
for (int j = i; j < a.size(); ++j) if (a[j] < a[min]) min = j;
std::swap(a[min], a[i]);
}
}