help With Java SelectionSort numbers to display ascending and descending Order.
help With Java SelectionSort numbers to display ascending and descending Order.
This is the program _ i want to put this in descending order,
Pls help me as soon as possible....
// program to demonstrate the selection sort
Code Java:
class SelectionSort
{
// method to display the contents of an array
static void displayData(int[] data)
{
for (int index=0; index != data.length; index++)
System.out.println(data[index]+"\t");
}
static int positionOfLargest(int[] data, int limit)
{
// method to return the position of the largest item
// in the data with bounds 0..limit
int largest = data[0];
int indexOfLargest = 0;
for (int index=1; index <= limit; index++)
{
if (data[index]> largest)
{
largest = data[index];
indexOfLargest = index;
}
}
return indexOfLargest;
}
public static void selectionSort(int[] data)
{
// method to sort the contents of an data into
// scending order
int temporary;
int position;
int size=data.length;
for (int index=size-1; index > 0; index--)
{
position=positionOfLargest(data, index);
// swap numbers
if (index != position)
{
temporary = data[index];
data[index] = data[position];
data[position] = temporary;
}
}
}
public static void main(String[] args)
{
int[] data = {18,7,15,8,13};
System.out.println("numbers before being sorted\n");
displayData(data);
selectionSort(data);
System.out.println("numbers after being sorted\n");
displayData(data);
}
}
Change the sort in descending Order?
Re: help With Java SelectionSort numbers to display ascending and descending Order.
use [highlight = java] your code here [/highlight]
for posting you code! please
Re: help With Java SelectionSort numbers to display ascending and descending Order.
Quote:
Change the sort in descending Order?
it is very easy!
here is the solution!!
just change this
Code :
if (data[index]> largest)
with
Code :
if (data[index]< largest)
is your problem solved?
regards
Re: help With Java SelectionSort numbers to display ascending and descending Order.
i'm very very new to this.
IT'S SOLVED !!!!!!!!!!! THANK YOU VERY MUCH
HOPE U'LL HELP ME IN OTHER PROGS' TOOO
:o