• Listen to a special audio message from Bill Roper to the Hive Workshop community (Bill is a former Vice President of Blizzard Entertainment, Producer, Designer, Musician, Voice Actor) 🔗Click here to hear his message!
  • Read Evilhog's interview with Gregory Alper, the original composer of the music for WarCraft: Orcs & Humans 🔗Click here to read the full interview.

Bubble Sort

Bubble Sort is an algorithm used for ordering data from least to greatest.
Lot's of modders avoid making things that need sorted because it seems complicated at first. It's not that bad.

this Bubble Sort algorithm uses NO global variables, so all you need to do is copy/paste the code into your map.

The return is a string. the string will separate each piece of data by word.
(Ex. 10 20 30 40 . . .) So to use the sorted data in a trigger, all you need to do is understand very basic String conversion triggering.

You will have to use a Custom Script to use this algorithm.
If you're not very fluent, or not fluent at all with Galaxy, no worries.

Simply go to Actions and find Custom Script. This action will allow you to run a simple line of code.

Create a local variable of type string in your trigger.

then inside the script type lv_VariableName = bubbleSort(string)

this will run the code and return the data in order from least to greatest.


Here is the code:

Code:
string bubbleSort(string lp_list) {   // Greatest to Least
    int init_data;
//
//
//              VARIABLES
    int BS_itemCount = 1;                   //Number of items in the lp_list
    int TMP_i;                              //Loop Structure
    int TMP_n;                              //Temporary Integer                 
    int TMP_j = 1;                          //Temporary Integer
    fixed[31] BS_data;                      //Holds up to 31 pieces of data
    fixed TMP_dHold;                        //Temporarily Holds old data
    string BS_return;                       //Returned String
    bool BS_swapped = false;                //Indicates whether two items were swapped or not


//
//
//              ARRAYS
    init_data = 0;
    while (init_data <= 30) {
        BS_data[init_data] = 0.0;
        init_data = init_data + 1;
    }
//                                                   
//
//              ACTIONS
    while(TMP_i != StringLength(lp_list)) {
        Wait(0.0, c_timeGame);
        TMP_i = TMP_i + 1;
        if(StringSub(lp_list,TMP_i,TMP_i) == " ") {
            BS_itemCount = BS_itemCount + 1;
        }
    }
    TMP_i = 1;
    while(TMP_i <= BS_itemCount) {
        //Wait(0.0, c_timeGame);
        BS_data[TMP_i] = StringToFixed(StringWord(lp_list,TMP_i));
        TMP_i = TMP_i + 1;
    }
    TMP_n = BS_itemCount;
    while(BS_swapped == false || BS_swapped == true) {
        //Wait(0.0, c_timeGame);
        BS_swapped = false;
        TMP_j = 2;
        while(TMP_j <= TMP_n - 0) {
            //Wait(0.0, c_timeGame);
            if(BS_data[TMP_j-1] > BS_data[TMP_j]) {
                TMP_dHold = BS_data[TMP_j-1];
                BS_data[TMP_j-1] = BS_data[TMP_j];
                BS_data[TMP_j] = TMP_dHold;
                BS_swapped = true;
            }
            TMP_j = TMP_j + 1;
        }
        if(BS_swapped == false) { break; }
        
    }
    TMP_i = 1;
    while(TMP_i <= BS_itemCount) {
        //Wait(0.0, c_timeGame);
        BS_return = BS_return + FixedToString(BS_data[TMP_i], 0) + " ";
        TMP_i = TMP_i + 1;
    }
    BS_return = StringSub(BS_return, 1, StringLength(BS_return) - 1);
    return(BS_return);
}


Keywords:
bubble sort algorithm least to greatest order numbers data sorted leaderboard global board
Contents

bubbleSortTest.SC2Map (Map)

Reviews
17:55, 31st May 2016 System: Bubble Sort Review by Shar Dundred This is a very simple system, but it appears to be working properly. Not rated Approved

Moderator

M

Moderator

17:55, 31st May 2016

System: Bubble Sort

Review
by Shar Dundred



This is a very simple system, but it appears to be working properly.

Not rated
Approved
 
Level 8
Joined
Jul 17, 2007
Messages
143
I'm going to assume... You don't know what Bubble Sort is >.>.

Bubble Sort is one of the most simplistic forms of sorting.
I used bubble sort because I only need to sort 8 reals in my map. No reason to do heap sort or merge sort just for that.

If you need to sort over 50 pieces of data (frequently that is) I wouldn't suggest using this.

But... anyone who can use Google should know this, right?
 
Top