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

Reply

Join Date: Jul 2003
Posts: 1
Reputation: acee is an unknown quantity at this point 
Solved Threads: 0
acee acee is offline Offline
Newbie Poster

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

 
0
  #1
Jul 12th, 2003
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) :

  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.
Reply With Quote Quick reply to this message  
Join Date: Apr 2005
Posts: 1
Reputation: shyammurarka is an unknown quantity at this point 
Solved Threads: 0
shyammurarka shyammurarka is offline Offline
Newbie Poster

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

 
0
  #2
Apr 29th, 2005
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;
}
}
}
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Java Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC