Results 1 to 7 of 7
  1. #1
    seandingobat is offline Member
    Join Date
    Sep 2008
    Posts
    16
    Rep Power
    0

    Default write a selection sort without having numerous variable?

    i'm trying to do a selection sort to sort out an array of unsorted numbers.


    basically just trying to swap their position in the index.

    for example here:

    for( int x = ; x < list.length; x ++){
    for( int i = x; i < list.length; i ++){
    if( list[x] > list[i] ){
    list[x] = list[i];
    list[i] = list[x];
    }
    }
    }
    return list;


    i know that this is wrong, but i hope you get the idea. i'm complete lost here.

    how do i make it so that it makes sure that it checks all the way through the list first and not just the first one that it sees that fits the condition?

    then how can you swap their value if the value of list[x] has already changed to list[i]?

  2. #2
    emceenugget is offline Senior Member
    Join Date
    Sep 2008
    Posts
    564
    Rep Power
    7

    Default

    There is a Wikipedia article for this with pseudocode. It doesn't get much easier than that.

  3. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by emceenugget View Post
    There is a Wikipedia article for this with pseudocode. It doesn't get much easier than that.
    Can you post the wiki link here.

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,891
    Rep Power
    25

    Default

    list[x] = list[i];
    list[i] = list[x];
    To do a swap of contents of two variables you need a third variable to hold the value.
    The above will make the two array elements have the same content.
    The first line copies [i] to [x] making them both the same, losing the original contents of [x]. You need to save [x] in a temp so you can put store it into [i] next. Take a piece of paper and do the steps one at a time to see.

  5. #5
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Norm, is that clear what our OP is going to do? Trying to swap two consecutive numbers or else.

  6. #6
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,891
    Rep Power
    25

    Default

    Eranga,
    I'm just guessing. The code looked like a common mistake when trying to swap two variables.
    Without comments (Why don't teachers insist on coments???)
    there is no way to know what the OP is trying to do.

  7. #7
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by Norm View Post
    Why don't teachers insist on coments???
    I'm worrying that too Norm.

    I thought you have find some kind of a pattern from the first post.

Similar Threads

  1. Return onyl 1 MX record if numerous exist
    By rlzyoner in forum New To Java
    Replies: 1
    Last Post: 08-06-2008, 12:41 AM
  2. List Selection
    By Sarinam in forum AWT / Swing
    Replies: 2
    Last Post: 07-14-2008, 07:16 AM
  3. How to sort a list using Bubble sort algorithm
    By Java Tip in forum Algorithms
    Replies: 3
    Last Post: 04-29-2008, 09:04 PM
  4. Selection sort in Java
    By Java Tip in forum Algorithms
    Replies: 0
    Last Post: 04-15-2008, 08:41 PM
  5. Code for selection
    By kneekow in forum Eclipse
    Replies: 0
    Last Post: 02-01-2008, 04:10 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •