Staredit Network > Forums > Technology & Computers > Topic: C++ Bucket Sort
C++ Bucket Sort
Dec 27 2008, 5:12 am
By: Falkoner  

Dec 27 2008, 5:12 am Falkoner Post #1



In my programming class we need to make a Bucket Sort program to sort numbers, now, I have the program working perfectly, but I now need to make it so the bucket is dynamic, and the user is allowed to enter how many numbers they want.

Now, I could easily do this if I could just keep the 2D array of the bucket in a rectangular shape, but instead it's supposed to be able to become a sort of stairsteppish thing, and each column will add as many rows as it needs during the sort, and nothing more.

So here's where my problem is, I know some basics of dynamic memory, but what I need to be able to do is make each column in the bucket have a different number of rows, and I just don't know the code to do that.

Here's how the program looks so far, it's completely set up to go dynamic, I just need to know how to have each column have a dynamic number of rows.

Code
#include <iostream.h>
#include <stdlib.h>
#include <windows.h>
#include <iomanip.h>

class sort
{
    private:
        int totalnumbers;
        int bucket[11][10];
        int numberlist[10];

    public:
        int currentplace ;
        void input(int amount);
        void organize();
        void cleanbucket();
        void clearbucket();
        void output();
        void showbucket();
};

void sort::input(int amount);
void sort::organize();
void sort::cleanbucket();
void sort::clearbucket();
void sort::output();
void sort::showbucket();

void gotoxy(short x, short y);

main()
{
    sort bucket;
    bucket.clearbucket();
    bucket.currentplace = 1;
    system("title Bucket Sort");
    system("color 0A");

    cout << "Welcome to the Bucket Sort Program, this program will sort ten numbers that\nyou enter"
        << " into it from least to greatest. The numbers must be from 0 to 999." << endl;

    bucket.input(10);
    for(int amount = 3; amount > 0; amount--)
    {
        bucket.organize();
        bucket.showbucket();
        bucket.cleanbucket();
    }
    bucket.output();

    return 0;
}


void gotoxy(short x, short y) //Moves cursor to an XY position on the console
{
  HANDLE hConsoleOutput;
  COORD Cursor_an_Pos = {x,y};
  hConsoleOutput = GetStdHandle (STD_OUTPUT_HANDLE);
  SetConsoleCursorPosition(hConsoleOutput, Cursor_an_Pos);
}

void sort::input(int amount)
{
    totalnumbers = amount-1;
    for(int currentinput = 0; currentinput < amount; currentinput++)
    {
        cout << "\nPlease enter number " << currentinput+1 <<": ";
        cin >> numberlist[currentinput];
        system("cls");
        cout << "Welcome to the Bucket Sort Program, this program will sort ten numbers that\nyou enter"
            << " into it from least to greatest. The numbers must be from 0 to 999." << endl;
        gotoxy(0, 2);
    }
}

void sort::organize()
{
    for(int currentnumber = 0; currentnumber < totalnumbers+1; currentnumber++)
    {
        bucket[bucket[0][(numberlist[currentnumber]%(10*currentplace))/currentplace]+1][(numberlist[currentnumber]%(10*currentplace))/currentplace] = numberlist[currentnumber];
        bucket[0][(numberlist[currentnumber]%(10*currentplace))/currentplace]+=1;
        numberlist[currentnumber] = 0;
    }
    currentplace *= 10;
}

void sort::cleanbucket()
{
    int currentplacement = 0;
    for(int currentrow = 0; totalnumbers >= currentrow; currentrow++)
    {
        for(int numbers = 1; numbers < bucket[0][currentrow]+1; numbers++)
        {
            numberlist[currentplacement] = bucket[numbers][currentrow];
            currentplacement++;
        }
    }
    clearbucket();
}

void sort::clearbucket()
{
    for(int x = 0; x < 11; x++)
    {
        for(int y = 0; y < 10; y++)
        {
            bucket[x][y] = 0;
        }
    }
}

void sort::output()
{
    system("cls");
    cout << "Your numbers in order from least to greatest go:" << endl;
    for(int amount = 0; totalnumbers+1 > amount; amount++)
    {
        cout << numberlist[amount] << endl;
    }
}

void sort::showbucket()
{
    system("cls");
    for(int x = 0; x < 10; x++)
    {
        for(int y = 0; y < 11; y++)
        {
            cout << setw(4) << bucket[y][x] << ' ';
        }
        cout << endl << endl;
    }
    system("pause");
}


Help?

Edit: I guess the big thing I need is the code to change the size of memory that a pointer points to, if you give me that, I've got it, I just don't know how to resize it without deleting the entire thing.

Post has been edited 2 time(s), last time on Dec 27 2008, 5:37 am by Falkoner. Reason: Change to codebox



None.

Dec 27 2008, 5:39 am A_of-s_t Post #2

aka idmontie

You can have multivariable arrays:
int a[rows][columns];

And this will help you with your other problem:
http://www.fredosaurus.com/notes-cpp/newdelete/55dynexample.html

Post has been edited 1 time(s), last time on Dec 27 2008, 5:45 am by A_of-s_t.



Personal GitHub
Starcraft GitHub Organization - Feel free to request member status!
TwitchTV

Dec 27 2008, 5:44 am Falkoner Post #3



Quote
And this will help you with your other problem:
http://www.fredosaurus.com/notes-cpp/newdelete/55dynexample.html

So you can't resize, you have to create a new array?

Quote
You can have multivariable arrays:
int a[rows, columns];
I don't think I understand what you mean there, is using a comma correct syntax? If so, what does that change?



None.

Dec 27 2008, 5:48 am A_of-s_t Post #4

aka idmontie

I'm sorry, it not seperated with a comma. I've been using another coding language lately :P .

I know that with a multivariable array, its like having a plane or choices, so it should cover what your doing since it only involves columns and rows, and I know you can just apply the resize techniques in that webpage I posted to a 2D array. So, its up to you to figure out how to put it together. I haven't messed with dynamic arrays since I made a special plugin for SC >.>



Personal GitHub
Starcraft GitHub Organization - Feel free to request member status!
TwitchTV

Dec 27 2008, 5:49 am Falkoner Post #5



Quote
I know that with a multivariable array, its like having a plane or choices, so it should cover what your doing since it only involves columns and rows, and I know you can just apply the resize techniques in that webpage I posted to a 2D array. So, its up to you to figure out how to put it together. I haven't messed with dynamic arrays since I made a special plugin for SC >.>

Hmm, well, I did a bit of searching, and I came across vectors, which sound like exactly what I need, am I wrong?



None.

Dec 27 2008, 5:52 am A_of-s_t Post #6

aka idmontie

I've never heard or used vectors in C++. However, they do use some functions that I think might help you.



Personal GitHub
Starcraft GitHub Organization - Feel free to request member status!
TwitchTV

Dec 27 2008, 5:53 am Falkoner Post #7



Here is the page I found, I might end up converting over to vectors for this.



None.

Options
  Back to forum
Please log in to reply to this topic or to report it.
Members in this topic: None.
[06:38 pm]
Ultraviolet -- :wob:
[2026-6-29. : 2:13 pm]
Vrael -- pee poo sibling
[2026-6-28. : 7:00 pm]
Symmetry -- poo poo papa
[2026-6-28. : 2:46 pm]
lil-Inferno -- pee pee child
[2026-6-27. : 6:10 pm]
Ultraviolet -- sweet summer child
[2026-6-26. : 10:31 am]
NudeRaider -- blessed innocent soul knows nothing of the strife we had before EUDs were discovered :teehee:
[2026-6-23. : 3:29 am]
DarkenedFantasies -- Probably just didn't care. For example, at some point before release, they've updated the graphics of some of the Protoss buildings (Forge, CyberCore, Citadel, Observatory, Arbiter Tribunal), but instead of properly re-rendering them with edited 3D models, they did crappy copy-paste jobs on the rendered graphics.
[2026-6-22. : 8:35 pm]
Ultraviolet -- :wob:
[2026-6-21. : 11:38 pm]
Symmetry -- :wob:
[2026-6-21. : 4:56 am]
Ultraviolet -- I suppose we'll likely never know, but my guess would be that they already saw it operating successfully and there was no monetary incentive to finish the original work. And the dev cycle in old school Blizzard was so hectic, it's possible it just got forgotten about after the original game got released. Plus there's an element of existing MPQ files that were packaged with the original discs becoming outdated if they updated it. And it's not like they remade the original MPQs, they just made new ones for BW specifically
Please log in to shout.


Members Online: O)FaRTy1billion[MM]