Hack Forums
[Java] bubble sorting help - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118)
+---- Thread: [Java] bubble sorting help (/showthread.php?tid=5464067)



bubble sorting help - indeedm8 - Nov 14, 2016

https://puu.sh/sir6J/025a374638.png
Written code is below. for some reason my bubble sorting code isnt working idk why. ive been looking over it but it just wont print out correctly.
This is how it looks with my code when i try to sort it.
https://puu.sh/sirdw/5f66cdab5f.png
My goal is to sort each row from lowest to highest. like the first row is correct but not the others




double temp;
for (int zz=1; zz<50; zz++)
{
for (int index = 0; index < 4 ; index++)
{

if (Data[zz][index]>Data[zz][index+1])
{

temp=Data[zz][index];


Data[zz][index]=Data[zz][index+1];
Data[zz][index+1]=temp;

}

}

}


* - FeatureBug - Nov 15, 2016

Lowest to highest what? I'm assuming you should be ordering by total amount spent?


* - ka$h - Nov 15, 2016

I don't understand why this is 50?
Code
for (int zz=1; zz<50; zz++)



* - Probably - Nov 15, 2016

Not sure why your formatting is so weird.

Here's a bubble sort I used for my intro class:
Code
public static void bubble(double []things){
    double temp;
    for(int i = 0; i < things.length-1; i++){
    if(things[i] > things[i+1]){
        temp = things[i];
        things[i] = things[i+1];
        things[i+1] = temp;
        }
    }
}

Why does you first loop run from 1-49 rather than 0-49? Might be an issue there. Also, try using array.length to get the size of it (returns # of elements, not last index) rather than using a constant.


* - indeedm8 - Nov 15, 2016

(Nov 15, 2016 12:44 PM)Ka$h Wrote:  I don't understand why this is 50?
Code
for (int zz=1; zz<50; zz++)

oh i think i was just testing it at first to get it to work. i figured out my problem on why it didn't run multiple times to run comparisons. it ran only once because i think i messed up bracket somewhere, but now its good


(Nov 15, 2016 04:51 PM)Probably Wrote:  Not sure why your formatting is so weird.

Here's a bubble sort I used for my intro class:
Code
public static void bubble(double []things){
    double temp;
    for(int i = 0; i < things.length-1; i++){
    if(things[i] > things[i+1]){
        temp = things[i];
        things[i] = things[i+1];
        things[i+1] = temp;
        }
    }
}

Why does you first loop run from 1-49 rather than 0-49? Might be an issue there. Also, try using array.length to get the size of it (returns # of elements, not last index) rather than using a constant.

for our program there was nothing in the 0 slot so i started it at 1, the 50 was hard coded at first since i was just trying to get it to work, its fixed now.
and for your code dont you need a second for loop around all that so it can keep running over and over until everything is sorted? since that only sorts it out once. i got it now though :D


* - AlpBox - Dec 1, 2016

Hi. If youve finished the lab can you send it to me. Ill pay back


Please Ill pay $25. Ill be sure to change variable names