views:

514

answers:

5

After this was answered I continued to work my way through the code. It work's perfect this way:

static String[][] bubbleSort(String customerdata[][], int sortafter, int asc)
 {
 String temp [];
 boolean sort;

   do{
    sortiert = true;

    for (int i = 0  ; i < customerdata.length - 1; i++){
     if(customerdata[i][sortafter].compareTo(customerdata[i+1][sortafter]) < 0){
      temp = customerdata[i];
      customerdata[i] = customerdata[i+1];
      customerdata[i+1] = temp;

      sort = false;
     }
    }

   }while(!sort);

  return customerdata;
 }

But as you can see, I'm missing int asc inside this function. What I want is to additionaly return a sorted descending or ascending array (depending wether asc == 1 (asc), or asc == 0 (desc)).

I'm at loss how to implement it inside this. I mean currently I can sort it ascending or descending, but once AFTER this method was called with some nasty long for() and if() loops.

I'd like to have it compactly inside and depending wether I give bubblesort(x,0,0) or (x,0,1) the list should be returned descending or ascending.

+1  A: 

You can always sort ascending and simply reverse it if descending is required. It's a question of whether or not repeating the "if" test inside the loop is less efficient that another traversal of the array.

I'm assuming that the size of the array is relatively small. Bubble sort is notoriously inefficient and shouldn't be used except for small arrays.

duffymo
Yes, I'm aware just how inefficient bubblesort is, nevertheless I'm still learning Java and its not my habit to skipe a few chapters just because they are inefficient. Either way I want to understand how this works.
NoCanDo
Just for the record, I think that even with BubbleSort, your code is less horrible than some "professional" code I've seen on the operating table here :) I would ask you, though, to replace the leading tabs with (say) 2 spaces for easier reading.
Carl Smotricz
+1  A: 

Sorted ascending means the element at i is less than the element at i + 1. Sorted descending means the element at i is greater than the element at i + . The trick is to flip the logic where you decide whether the elements are out of place. Specifically, this line:

if(customerdata[i][sortafter].compareTo(customerdata[i+1][sortafter]) < 0){

should be changed to

if(customerdata[i][sortafter].compareTo(customerdata[i+1][sortafter]) > 0){

if you want to flip the order of the sorting.

Zach Hirsch
he's asking how he can add this into the code, conditionally of the parameter... He already knows how to change the sorting
Toad
Yea, well, this answered my question. Can be solved with if(asc == 0) and else inside the do loop. Efficient? Not really. Works? Heck yeah!
NoCanDo
+1  A: 

try this:

 for (int i = 0  ; i < customerdata.length - 1; i++){
      if(customerdata[i+asc][sortafter].compareTo(customerdata[i+1-asc][sortafter]) < 0){
           temp = customerdata[i];
           customerdata[i] = customerdata[i+1];
           customerdata[i+1] = temp;

           sort = false;
      }
 }

Asc can be 0 or 1 (ascending or descending...)

by adding it to your index, you basically swap the if statement, without adding another if ;^)

(note there are 2 positions which I changed: the "+ asc" and the "- asc")

EDIT: Don't forget to put a big assert at the first line making sure Asc can really not be anything else than 0 or 1 ;^)

Toad
Yea, asert would be a good idea, also exceptions try n catch too. If a user should type in something else besides 0 or 1.
NoCanDo
+1  A: 

And if you want the "software engineering" type answer instead of the quick hack answer I gave above, you can pass a functor (search for the Comparator class) to do the comparison, to allow for ultimately flexible searching.

James
+2  A: 

Simple solution: Can you make asc into one of 1 or -1 ?

Then you'd only need to change one line:

if(asc * customerdata[i][sortafter].compareTo(customerdata[i+1][sortafter]) < 0)
Carl Smotricz
Now that's the easiest way I. Thanks!
NoCanDo