I had heard about sorting for a while, but never actually gave it a try. So I decided to make an algorithm that would sort the members of a vector from least to greatest. Though this may seem pretty simple and/or silly, I actually put a lot of thought into it and wrote out tables/diagrams.
#include <iostream>
#include <vector>
namespace{
typedef unsigned int uint;
}
std::vector<int> sort(std::vector<int>a)
{
for (int i = 1; i < a.size(); ++i){
for (int j = 0; j < a.size() - 1; ++j){
if (a[j] > a[i]) std::swap(a[j], a[i]);
}
}
return a;
}
int main()
{
std::vector<int> a = { 17, 12, 1, 20, 4, 6, 94 };
for (uint i = 0; i < a.size(); ++i){
std::cout << sort(a)[i] << std::endl;
}
}
I know there's probably some sort function part of the standard C++ library, but for a sort function from scratch, how efficient is this? What could I do to improve it?