Someone please help, here is class, variables and method that is fed in an array.
public class exchangeSortPart {
static int counterC = 0; // counts the comparisons
static int counterE = 0; // Counts exchanges
public static int[] exchangeSort(int[] list){ // method for insertion sort
int temp; // this is used to keep a copy of the element being switched
for (int i = 0; i < list.length-1;i++) { // for loop for running through array, the .length-1 because of the next for loop
for (int j = i + 1; j < list.length; j ++ ) // compares next element in array
{
counterC++; // this counts a comparison taking place
if( list[i] > list[j] ) // if the value on the left is greater than the value on the right, switch that bitch!
{
temp = list[i]; // make a copy of the left element to be switched
list[i] = list[j];
list[j] = temp;
counterE++; // this counts that an exchange took place
}
}
}
return list; // return list array
} // end of exchangeSort method
} // end of exchangeSortPart Class

New Topic/Question
Reply



MultiQuote




|