Results 1 to 10 of 10
  1. #1
    'Bruno's Avatar
    Join Date
    Dec 2009
    Gender
    male
    Location
    Portugal
    Posts
    2,883
    Reputation
    290
    Thanks
    1,036
    My Mood
    Busy

    [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
    Light travels faster than sound. That's why most people seem bright until you hear them speak.

  2. The Following User Says Thank You to 'Bruno For This Useful Post:

    falzarex (06-23-2010)

  3. #2
    Hell_Demon's Avatar
    Join Date
    Mar 2008
    Gender
    male
    Location
    I love causing havoc
    Posts
    3,976
    Reputation
    343
    Thanks
    4,320
    My Mood
    Cheeky
    vectors have sort edit: good job nonetheless, for gameh4xx0rs out there: this can be used to sort targets by hitpoints ;P
    Ah we-a blaze the fyah, make it bun dem!

  4. #3
    Sixx93's Avatar
    Join Date
    Sep 2009
    Gender
    male
    Posts
    673
    Reputation
    21
    Thanks
    250
    My Mood
    Cool
    yeah, i know it... it's an algorithm called "bubble sort"... i've studyed it at school

  5. #4
    'Bruno's Avatar
    Join Date
    Dec 2009
    Gender
    male
    Location
    Portugal
    Posts
    2,883
    Reputation
    290
    Thanks
    1,036
    My Mood
    Busy
    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)
    Last edited by 'Bruno; 06-24-2010 at 12:22 AM.
    Light travels faster than sound. That's why most people seem bright until you hear them speak.

  6. #5
    Sixx93's Avatar
    Join Date
    Sep 2009
    Gender
    male
    Posts
    673
    Reputation
    21
    Thanks
    250
    My Mood
    Cool
    Quote Originally Posted by Brinuz 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

  7. #6
    'Bruno's Avatar
    Join Date
    Dec 2009
    Gender
    male
    Location
    Portugal
    Posts
    2,883
    Reputation
    290
    Thanks
    1,036
    My Mood
    Busy
    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.
    Light travels faster than sound. That's why most people seem bright until you hear them speak.

  8. #7
    Void's Avatar
    Join Date
    Sep 2009
    Gender
    male
    Location
    Inline.
    Posts
    3,198
    Reputation
    205
    Thanks
    1,445
    My Mood
    Mellow
    Wow am I the only one here who doesn't have classes for programming? ;__;

  9. #8
    'Bruno's Avatar
    Join Date
    Dec 2009
    Gender
    male
    Location
    Portugal
    Posts
    2,883
    Reputation
    290
    Thanks
    1,036
    My Mood
    Busy
    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.. ^^
    Last edited by 'Bruno; 06-24-2010 at 03:03 AM.
    Light travels faster than sound. That's why most people seem bright until you hear them speak.

  10. #9
    Hell_Demon's Avatar
    Join Date
    Mar 2008
    Gender
    male
    Location
    I love causing havoc
    Posts
    3,976
    Reputation
    343
    Thanks
    4,320
    My Mood
    Cheeky
    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 =)
    Ah we-a blaze the fyah, make it bun dem!

  11. #10
    mwb1234's Avatar
    Join Date
    May 2009
    Gender
    male
    Posts
    460
    Reputation
    7
    Thanks
    65
    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

Similar Threads

  1. finally (small) blackdrag0 v4.0!!!
    By blackdrag0 in forum WarRock - International Hacks
    Replies: 20
    Last Post: 05-29-2007, 01:39 PM
  2. Sort of Got No spread..
    By Jeckels in forum WarRock - International Hacks
    Replies: 5
    Last Post: 05-01-2007, 02:43 PM
  3. warrock retail algorithm
    By shadowsecret in forum WarRock - International Hacks
    Replies: 5
    Last Post: 04-19-2007, 09:25 PM
  4. Some sort of riddle
    By System79 in forum Entertainment
    Replies: 10
    Last Post: 10-09-2006, 06:28 PM
  5. a small little request ^^
    By metabee22 in forum Help & Requests
    Replies: 12
    Last Post: 04-10-2006, 08:04 PM