943,483 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 11510
  • Java RSS
Jul 12th, 2003
0

Some help needed with Selection Sort in java, Please!!

Expand Post »
Working on a problem which uses a main only to implement Selection Sort. You enter arbitrary amount of integers and when the program runs it spits out the integers in increasing order. So the output would be something like this:

Enter Integers to sort: 3
> 1
> 8
> -5

answer:
> -5
> 1
> 8

Here is what i have done so far (note: Terminal is a class that lets me do input/output. It was custom made by someone) :

Java Syntax (Toggle Plain Text)
  1. public class SelectionSort
  2. {
  3. public static void main(String[] args)
  4. {
  5. // Prompt for the number of integers and put them in a array of that
  6. // size.
  7.  
  8. Terminal t = new Terminal();
  9. int n = t.readInt("Integers to sort? ");
  10. int[] integerArray;
  11. integerArray = new int[n];
  12.  
  13. for(int i = 0; i < n; i++) {
  14. integerArray[i] = t.readInt("> ");
  15. }
  16.  
  17. // Find the index with the smallest integer.
  18.  
  19. for(int i = 0; i < integerArray.length; i++) {
  20. int start = i;
  21. for (int x = i + 1; x < integerArray.length; x++) {
  22. if (integerArray[x] < integerArray[start])
  23. start = x;
  24. }
  25.  
  26. // Use Selection Sort alogorithm to sort the array in increasing
  27. // order.
  28.  
  29. int small = start;
  30. for (int z = 0; z < integerArray.length; z++) {
  31. int tmp = integerArray[small];
  32. integerArray[small] = integerArray[i];
  33. integerArray[i] = tmp;
  34.  
  35. }
  36.  
  37. }
  38. t.println();
  39.  
  40. // Print out the integers in increasing order.
  41.  
  42. for(int i = 0; i < n; i++) {
  43. t.println(integerArray[i]);
  44. }
  45.  
  46. }
  47.  
  48.  
  49.  
  50. }


It compiles fine, but when i run it, its somewhat buggy. The problem is that when i enter a odd number at the prompt "Enter number of integers: ", it works fine. But if i enter a even number, it doesn't work so well. It just prints the integers the way they were typed instead of in increasing order like so:

Integers to sort? 2
> 2
> 1

answer:

2
1

Thanks.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
acee is offline Offline
1 posts
since Jul 2003
Apr 29th, 2005
0

Re: Some help needed with Selection Sort in java, Please!!

I didn't very much understand your algorithm. So I will be sending the alogorithm that I have learnt. Hope its useful.

This is only the algorithm not the full program.

//Sorting in Descending Order
// arr is just an array

for(int i=0; i<arr.length-1;i++)
{
for(int j=i+1;j<arr.length;j++)
{
if(arr[i] > arr[j])
{
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
Reputation Points: 10
Solved Threads: 0
Newbie Poster
shyammurarka is offline Offline
1 posts
since Apr 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: while loops
Next Thread in Java Forum Timeline: converting python to java





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC