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.
[04:15 am]
DarkenedFantasies -- you eat lots of beans
[03:46 am]
IskatuMesk -- how do i get gas
[11:43 pm]
Moose -- you don't
[10:06 pm]
Zoan -- how do i get minerals
[2026-4-14. : 11:45 pm]
ClansAreForGays -- Anyone wanna played Skewed StarCraft?
[2026-4-14. : 12:07 am]
Vrael -- NudeRaider
NudeRaider shouted: Vrael ranting still is though
you're a gentleman and a scholar, thank you
[2026-4-13. : 10:07 pm]
NudeRaider -- ya why phone people when you can just write letters
[2026-4-13. : 9:37 pm]
IskatuMesk -- I have never and will never own a phone
[2026-4-13. : 9:15 pm]
NudeRaider -- Vrael ranting still is though
[2026-4-13. : 9:14 pm]
ClansAreForGays -- anticapitalism isnt edgy anymore
Please log in to shout.


Members Online: Rawflesh0615, anoeth47