Why am i sharing a Sorting algorithm, such simple thing? well the fact is that most of the programmers/coders who just started to read the basics and get an exercise to Sort something, Oh Jesus.. it's an headache.
(Honestly it happened to me when started, now it's something that i make in a minute)
In my opinion making our own algorithms is always useful, even if it is just for learning and exercising our own logic. (I know there is a Sort function somewhere)
I was doing a few bugfixes/updates on that console snake game and just created the algorithm, so I'm just going to share since it can be helpful for starters.
[PHP]//
// sort.cpp
// Sorting
//
// Created by Bruno Monteiro on 2010. (Aka Brinuz @ mpgh.net)
//
#include <iostream>
using namespace std;
int main()
{
int askedNums[10];
cout << "Input nums:" << endl;
//ask for nums
for(int i = 0; i < 10; i++)
{
cin >> askedNums[i];
}
//Sort algorithm
int aux;
for(int i = 0; i < 10; i++)
{
for(int j = 0; j < 10; j++)
{
if(askedNums[i] > askedNums[j])
{
aux = askedNums[i];
askedNums[i] = askedNums[j];
askedNums[j] = aux;
}
}
}
cout << endl << endl << "Organized:" << endl;
//print nums
for(int i = 0; i < 10; i++)
{
cout << askedNums[i] << endl;
}
return 0;
}[/PHP]
probably not the fastest, but it's something that i did without thinking that much.
Btw, that one sorts form the highest number to the lower.
Eg:
Code:
1
4
8
9
6
7
4
2
3
5
Will end up as:
Code:
9
8
7
6
5
4
4
3
2
1
Have fun