[Small Algorithm] Sorting [C++]

Posts 1–10 of 10 · Page 1 of 1
[Small Algorithm] Sorting [C++]
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
vectors have sort edit: good job nonetheless, for gameh4xx0rs out there: this can be used to sort targets by hitpoints ;P
yeah, i know it... it's an algorithm called "bubble sort"... i've studyed it at school
Quote Originally Posted by Sixx93 View Post
yeah, i know it... it's an algorithm called "bubble sort"... i've studyed it at school
i actually made it now, but yea probably because my basics and most of my knowledge on programming came from school (finished already)
Quote Originally Posted by 'Bruno View Post
i actually made it now, but yea probably because my basics and most of my knowledge on programming came from school (finished already)
yeah... i'm studying C++ at school studyed the algorithm like in 5-6 months ago
Quote Originally Posted by Sixx93 View Post
yeah... i'm studying C++ at school studyed the algorithm like in 5-6 months ago
its quite a simple concept, find a bigger one, and move it on that position.. it's easy but i remember when i got told to do something like it a few years ago.. it was.. painful.. ^^ good luck with school btw.
Wow am I the only one here who doesn't have classes for programming? ;__;
Quote Originally Posted by Void View Post
Wow am I the only one here who doesn't have classes for programming? ;__;
i feel sorry for you

see the good point of all this... you dont have classes but you are better then us.. so.. ^^
Quote Originally Posted by Void View Post
Wow am I the only one here who doesn't have classes for programming? ;__;
I don't have classes either =)
Quote Originally Posted by Void View Post
Wow am I the only one here who doesn't have classes for programming? ;__;
Me and HD don't :O
Posts 1–10 of 10 · Page 1 of 1

Post a Reply

Similar Threads

Tags for this Thread

None

Talk with us