Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

Java 

bubble sorting help

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;

}

}

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



--
Programmer for Hire
{C#/.NET/Rust/Go/Java/PHP}


I don't understand why this is 50?
Code
for (int zz=1; zz<50; zz++)
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.
(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
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


Possibly Related Threads...
Thread / Author Replies Views Last Post
by lucienhfMar 22, 2024 03:06 AM 4 241 Mar 22, 2024 03:06 AM
Last Post: lucienhf
[Python] by ThisBearScatJan 27, 2020 06:27 PM 16 341 Jan 27, 2020 06:27 PM
Last Post: Lindros
[.NET] by JavaJaneMay 29, 2019 08:26 AM 1 184 May 29, 2019 08:26 AM
Last Post: tweety