PDA

View Full Version : Java: Changing the Sorts class


phantomxprime
01-23-2006, 01:56 AM
Hello,

I need to re-write the Sorts class for arrays so that it sorts in descending order, ie: high to low. The best I can do is: if i put in 5 numbers, 1-5, and it sorts, it shows up as 1,4,5,2,3.
This Sorts class came from the book we use, and is the version that our teacher wants us to use. Thank you for any and all help. And in reference to the stickied topic, i have spent the better part of a week working on this, and the best i get is 1,4,5,2,3. I don't think anybody else in my class has finished this one either.
//********************************************************************
// Sorts.java Author: Lewis/Loftus/Cocking
//
// Demonstrates the selection sort and insertion sort algorithms,
// as well as a generic object sort.
//********************************************************************

public class Sorts
{
//-----------------------------------------------------------------
// Sorts the specified array of integers using the selection
// sort algorithm.
//-----------------------------------------------------------------
public static void selectionSort (int[] numbers)
{
int min, temp;

for (int index = 0; index < numbers.length-1; index++)
{
min = index;
for (int scan = index+1; scan < numbers.length; scan++)
if (numbers[scan] < numbers[min])
min = scan;

// Swap the values
temp = numbers[min];
numbers[min] = numbers[index];
numbers[index] = temp;
}
}

//-----------------------------------------------------------------
// Sorts the specified array of integers using the insertion
// sort algorithm.
//-----------------------------------------------------------------
public static void insertionSort (int[] numbers)
{
for (int index = 1; index < numbers.length; index++)
{
int key = numbers[index];
int position = index;

// shift larger values to the right
while (position > 0 && numbers[position-1] > key)
{
numbers[position] = numbers[position-1];
position--;
}

numbers[position] = key;
}
}

Spookster
01-23-2006, 05:47 AM
Here you go



//-----------------------------------------------------------------
// Sorts the specified array of integers using the selection
// sort algorithm.
//-----------------------------------------------------------------
public static void selectionSort (int[] numbers)
{
int max, temp;

for (int index = 0; index < numbers.length-1; index++)
{
max = index;
for (int scan = index+1; scan < numbers.length; scan++)
if (numbers[scan] > numbers[max])
max = scan;

// Swap the values
temp = numbers[max];
numbers[max] = numbers[index];
numbers[index] = temp;
}
}

//-----------------------------------------------------------------
// Sorts the specified array of integers using the insertion
// sort algorithm.
//-----------------------------------------------------------------
public static void insertionSort (int[] numbers)
{
for (int index = 1; index < numbers.length; index++)
{
int key = numbers[index];
int position = index;

// shift larger values to the left
while (position > 0 && numbers[position-1] < key)
{
numbers[position] = numbers[position-1];
position--;
}

numbers[position] = key;
}

}

phantomxprime
01-23-2006, 04:01 PM
wow. Thank you. I spent so much time thinking that I needed to mess with the for loops, i didn't bother trying anything else by itself. Thanks.