Bubble Sort Arrays in java


/ Published in: Java
Save to your folder(s)

This is a Bubble Sorter in java
(was a pain in the ass to get right)
you can change the sorting style from ascending to descending order
by changing the line
if(vals[d+1] < vals[d])
to
if(vals[d+1] > vals[d])


Copy this code and paste it in your HTML
  1. public static double[] SortArray(double[] vals)
  2. {
  3.  
  4. double temp=0;
  5. double ret[]= new double[vals.length];
  6. for(int c=0;c < vals.length;c++)
  7. {
  8. for(int d=0;d < vals.length - 1;d++)
  9. {
  10. if(vals[d+1] < vals[d])
  11. {
  12.  
  13. temp = vals[d];
  14. vals[d] = vals[d + 1];
  15. vals[d + 1] = temp;
  16. }
  17. }
  18. }
  19. ret = vals;
  20. return ret;
  21. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.