Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 07-22-2007, 11:30 PM
zoe zoe is offline
Member
 
Join Date: Jul 2007
Posts: 40
zoe is on a distinguished road
Help with sort algorithm
Hello, I need to re-write the Sorts class for arrays so that it sorts in descending order, example: 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.
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.

Code:
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; } }
Thanks
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 08-07-2007, 08:09 AM
Member
 
Join Date: Jul 2007
Posts: 40
fernando is on a distinguished road
Here you go
Code:
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; } }
Greetings.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to sort a list using Bubble sort algorithm Java Tip Algorithms 3 04-29-2008 10:04 PM
Help with algorithm susan New To Java 1 07-14-2007 12:26 AM
Help me with this algorithm Marcus Advanced Java 3 07-02-2007 03:30 PM
Help with Algorithm Daniel Advanced Java 2 07-02-2007 07:51 AM
Insertion sort algorithm Albert Advanced Java 2 06-28-2007 10:26 PM


All times are GMT +3. The time now is 10:12 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org