Skip to content
This repository has been archived by the owner on Jul 10, 2024. It is now read-only.

Commit

Permalink
Selection sort in C++ (#5769)
Browse files Browse the repository at this point in the history
Co-authored-by: Riyazul555 <riyazulislam2003@gmail.com>
Co-authored-by: Ritesh Kokam <61982298+RiteshK-611@users.noreply.github.com>
  • Loading branch information
3 people authored Jun 29, 2024
1 parent 800baef commit 32ff9ab
Showing 1 changed file with 37 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#include <iostream>
#include <vector>

void selectionSort(std::vector<int>& list) {
int n = list.size();

for (int i = 0; i < n - 1; ++i) {
// Set current element as minimum
int minIndex = i;

// Check the element to be minimum
for (int j = i + 1; j < n; ++j) {
if (list[j] < list[minIndex]) {
minIndex = j;
}
}

// Swap the minimum element with the current element
if (minIndex != i) {
std::swap(list[i], list[minIndex]);
}
}
}

int main() {
std::vector<int> unsortedList = {64, 25, 12, 22, 11};

selectionSort(unsortedList);

std::cout << "Sorted list: ";
for (int num : unsortedList) {
std::cout << num << " ";
}
std::cout << std::endl;

return 0;
}

0 comments on commit 32ff9ab

Please sign in to comment.