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.
[10:13 pm]
Oh_Man -- No APM in chess. Computer just outthinks the human by seeing more moves ahead and more permutations
[10:12 pm]
Oh_Man -- U reckon? Kasparov waa defeated by computer decades ago
[05:47 pm]
NimoStar -- If its any consolation, the human brain would still mop the floor with this bot if it wasn't limited by the mechanical interface
[05:34 pm]
NimoStar -- Well. The bot didn't come up with any new strategy. It just has superhuman micro. Hard to beat that. I reckon even the simplest dedicated chess engines are unbeatable for humans now.
[02:13 pm]
Oh_Man -- all g
[10:54 am]
NudeRaider -- Oh_Man
Oh_Man shouted: NudeRaider did u watch the entire video? it's not one game. it's currently dominating the ladder at number 1 position. do ur research first!
Oops, caught me there, no I haven't. :( I was imagining how the game went. Sorry for that. Watching it now. brb
[10:52 am]
NudeRaider -- NimoStar
NimoStar shouted: They were not "Unprepared" that was their fifth loss in a row against the bot. The other replays are also available
that doesn't mean he was prepared. When humans come around with new strategies it often takes weeks until the pros figure out how to shut it down. Oh_Man that's certainly a way more convincing argument for micro being "too good to beat".
[09:43 am]
DiearAnother -- wirefram
[08:35 am]
NimoStar -- Well, pros showed that long ago... half of zerg pros just ling muta rush
[08:35 am]
NimoStar -- " its shown is that you can trade 1000's of apm to make up for lack of strategy"
Please log in to shout.


Members Online: O)FaRTy1billion[MM], Oh_Man