Question

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.

Was it helpful?

Solution

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.

OTHER TIPS

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)

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.

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 ;^)

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top