User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the Java section within the Software Development category of DaniWeb, a massive community of 391,554 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,562 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Java advertiser: Lunarpages Java Web Hosting
Views: 7958 | Replies: 1
Reply
Join Date: Jul 2003
Posts: 1
Reputation: acee is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
acee acee is offline Offline
Newbie Poster

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

  #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) :

public class SelectionSort
{
  public static void main(String[] args)
  {
    // Prompt for the number of integers and put them in a array of that
    // size.

    Terminal t = new Terminal();
    int n = t.readInt("Integers to sort? ");
    int[] integerArray;
    integerArray = new int[n];

    for(int i = 0; i < n; i++) {
      integerArray[i] = t.readInt("> ");
    }

     // Find the index with the smallest integer.

     for(int i = 0; i < integerArray.length; i++) {
       int start = i;
       for (int x = i + 1; x < integerArray.length; x++) {
         if (integerArray[x] < integerArray[start])
          start = x;
        }

     // Use Selection Sort alogorithm to sort the array in increasing
     // order.

        int small = start;
        for (int z = 0; z < integerArray.length; z++) {
          int tmp = integerArray[small];
          integerArray[small] = integerArray[i];
          integerArray[i] = tmp;

        }

   }
        t.println();

      // Print out the integers in increasing order.

          for(int i = 0; i < n; i++) {
          t.println(integerArray[i]);
         }

    }



}

 


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.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Apr 2005
Location: Nepal
Posts: 1
Reputation: shyammurarka is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
shyammurarka shyammurarka is offline Offline
Newbie Poster

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

  #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  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb Java Marketplace
Thread Tools Display Modes

Similar Threads
Other Threads in the Java Forum

All times are GMT -4. The time now is 9:26 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC