We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
Page Index Toggle Pages: 1
Sorting Arrays (Read 305 times)
Sorting Arrays
Dec 7th, 2008, 12:16am
 
hey there, i will really apreaciate help on this! ill be grateful for life!  

i need to bubble sort a file of numbers in ascending and descending order however for some reason the code is always geving me error or doesn't print on the screen. here's the code...

for (i = 0; i < Size-1; i++);
 for (j = Size-1; j>i; j--);
   if (nums[j-1] > nums[j]);
temp = nums[j-1];
nums[j-1] = nums[j];
nums[j] = temp;

for (i = 0; i<Size; i++){println(" "+ i)  ;  

 nums[i] = int(Strs[i]);
 OutPutLiness += " " + nums[i];
   OutPutLiness += " ";
 }
   text(OutPutLiness,90, 200);

Can point me in the right direction? PLEASE!!!
Re: Sorting Arrays
Reply #1 - Dec 7th, 2008, 4:12am
 
A bunch of problems:

for loops and if statement shouldn't have semicolons before them.  none of your variables are declared anywhere.  your if statement needs {brackets} to include multiple lines.

i was going to post working code but i feel like that would be doing your homework for you. this is homework right? someone else posted an identical question.
Re: Sorting Arrays
Reply #2 - Dec 28th, 2008, 11:47am
 
If it was a homework, i guess you missed the due date. :)

PFont f;
f = createFont("verdana", 10);
textFont(f);

background(255);
fill(0);

int nums[] = {34, 52, 6541, 415, 154, 514, 534, 514, 151, 34};
String OutPutLiness = "";

size (600,400);

for (int i = 0; i < nums.length-1; i++){
 for (int j = nums.length-1; j > i; j--){
   if (nums[j-1] > nums[j]){
     int temp = nums[j-1];  
     nums[j-1] = nums[j];  
     nums[j] = temp;
   }
 }
}

for (int i = 0; i < nums.length; i++){
 println(" "+ i);
 // nums[i] = int(Strs[i]);  // does not make sense for me.
 OutPutLiness += " " + nums[i];  
 OutPutLiness += " ";  
}  
text(OutPutLiness,5,30);
Page Index Toggle Pages: 1