Sorting Methods for an Array

I'm trying to sort an object array of bank accounts based on their balance. JGRASP doesn't like my calling statement, which I don't think is correct.

My question is am I calling the method correctly (trying to set the bankArray[counter].getBalance()) and am I using the sort method correctly?

my call method

bankArray = bankArray[counter].selectionSort(bankArray[counter].getBalance(), counter);

My sort method:

publicstaticvoid selectionSort (double [] sort,int length)

{

for (int i = length; i < 1; i--)

{

double currentMax;

int currentMaxIndex;

currentMax = sort[0];

currentMaxIndex = 0;

for (int j = 1; j < i; j++)

{

if (currentMax < sort[j])

{

currentMax = sort[j];

currentMaxIndex = j;

}//End if

}//End for

[1468 byte] By [Imbriuma] at [2007-10-3 11:32:41]
# 1
To sort arrays you can use the java.util.Arrays.sort method.-Puce
Pucea at 2007-7-15 13:59:32 > top of Java-index,Java Essentials,New To Java...
# 2
not allowed to for this assignment :/ Instructor wants to make sure we understand how sorting works, but obviously I'm not clear on calling the method, and what I should be sending :(.
Imbriuma at 2007-7-15 13:59:32 > top of Java-index,Java Essentials,New To Java...
# 3

There are some points to notice:

Point 1: Your method receive an array of double and an int, as you passing a double and an int.

Point 2: Your algorithm doesn't work properly. First of all, the selection sort works by selecting the smallest unsorted item remaining in the list, and then swapping it with the item in the next position to be filled. Also, note that you don't swap the array elements in any moment.

Point 3: In Java, your selectionSort method don't need to receive a parameter to inform the array length. The array itself already have that information ("myArray.length").

Point 4: About the call of your method, you made it as the selectionSort method returns an array of BankAccounts - Supposing that you represent a bank account through an BankAccount class and bankArray is an array of BankAccount objects. It's not true. As you can see, your selectionSort method returns void, or either, returns nothing.

Observing those 4 first points, we can re-write your selectionSort method like this

public static void selectionSort(double[] numbers) {

for (int i = 0; i < numbers.length - 1; i++)

{

int minIndex = i;

for (int j = i + 1; j < numbers.length; j++)

{

if (numbers[j] < numbers[minIndex])

minIndex = j;

}

double temp = numbers[i];

numbers[i] = numbers[minIndex];

numbers[minIndex] = temp;

}

}

Now, when I try to re-write the call, I noticed other important odd thing about your approach: The selectionSort method was written to sort bankAccounts, but it receives an array of double values. Being thus, you must to previously create a double[] object filled with the accounts' balances and then sort it. But, once sorted, how can you return the balances to their proper account? There is no way to do it. You have to sort a bunch of BankAccount objects, instead of an array of balances...

An intuitive implementation of this sorting cound be something like that:

public static void selectionSort(BankAccount[] accounts) {

for (int i = 0; i < accounts.length - 1; i++) {

int minIndex = i;

for (int j = i + 1; j < accounts.length; j++) {

double

currBalance = accounts[j].getBalance(),

minBalance = accounts[minIndex].getBalance()

;

if (currBalance < minBalance)

minIndex = j;

}

BankAccount temp = accounts[i];

accounts[i] = accounts[minIndex];

accounts[minIndex] = temp;

}

}

I noticed form your call that the selectionSort method boleng to the BankAccount class. Once this method is static, It's strongly recomended that you invoke this method in a static way (from the class, instead of from an object).

Here it goes a fictional BankAccount class and a TestBankAccount class. Read it, Run it, Study it, Understand it.

package help.sun.imbrium;

public class BankAccount {

private double balance;

private int number;

public static void selectionSort(BankAccount[] accounts) {

for (int i = 0; i < accounts.length - 1; i++) {

int minIndex = i;

for (int j = i + 1; j < accounts.length; j++) {

double

currBalance = accounts[j].getBalance(),

minBalance = accounts[minIndex].getBalance()

;

if (currBalance < minBalance)

minIndex = j;

}

BankAccount temp = accounts[i];

accounts[i] = accounts[minIndex];

accounts[minIndex] = temp;

}

}

public BankAccount(int number, double balance) {

this.balance = balance;

this.number = number;

}

public double getBalance() {

return balance;

}

public void setBalance(double balance) {

this.balance = balance;

}

public int getNumber() {

return number;

}

public void setNumber(int number) {

this.number = number;

}

public String toString(){

return "[Account #" + number + " balance: $" + balance + "]";

}

}

class TestBankAccount{

public static void main(String[] args) {

BankAccount[] accounts = {

new BankAccount(1, 154.23),

new BankAccount(2, 1554.23),

new BankAccount(3, 15.3),

new BankAccount(4, 854),

new BankAccount(5, -4.79),

};

System.out.println("Unsorted:\n\t" + arrayToString(accounts, " "));

BankAccount.selectionSort(accounts);

System.out.println("Sorted:\n\t" + arrayToString(accounts, " "));

}

private static String arrayToString(Object[] objs, String separator){

StringBuilder sb = new StringBuilder();

for (int i = 0; i < objs.length; i++) {

Object obj = objs[i];

sb.append(obj.toString());

if(i < objs.length - 1)

sb.append(separator);

}

return sb.toString();

}

}

Lumantua at 2007-7-15 13:59:32 > top of Java-index,Java Essentials,New To Java...