LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices



Reply
 
Search this Thread
Old 09-23-2006, 12:17 AM   #1
gomez6211
LQ Newbie
 
Registered: Jul 2005
Location: San Antonio, TX
Distribution: Fedora, Suse, Mandriva, Slackware
Posts: 13

Rep: Reputation: 0
Java - problems with selection sort


I'm having problems getting a specific implementation of selection sort working. The pseudo code given is as follows:

FOR i = 0 TO n-2 DO
min = i
FOR j = i+1 TO n-1 DO
IF A[j] < A[min] THEN
min = j
swap(A[i], A[min])

my code is as follows:

for(int i = 0; i <= array.length-2; i++)
{
int min = i;
for (int j = i+1; j <= array.length-1; j++)
{
if(array[j] < array[min])
{
min = j;
comp_count++;
}
int temp = array[i];
array[i] = array[min];
array[min] = temp;
}
}

For some reason, it doesn't sort properly. I can't seem to find out where I made a mistake. I would appreciate if anyone could point out my mistake.
 
Old 09-23-2006, 12:54 AM   #2
paulsm4
Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Hi -

There's something wrong with your algorithm. Here's a slightly modified version that works (albeit with typical bubble sort lack of efficiency ;-)):
Code:
public class Bubble {

  public static void main (String[] args) {

    int[] array = { 3,7,1,5,3,8,2 };

    for (int i = 0; i < array.length-1; i++) {
      for (int j = 0; j < array.length-1-i; j++) {
        if (array[j+1] < array[j]) {
          int temp = array[j];
          array[j] = array[j+1];
          array[j+1] = temp;
        }
      }
    }

    for (int i = 0; i < array.length-1; i++) {
      System.out.println ("a["+ i + "]: " + array[i] + "...");
    }

  }      

}
'Hope that helps .. PSM

PS:
Please be sure you use "[code]" tags in future LQ posts!
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Is there a line limit with the sort utility? Trying to sort 130 million lines of text gruffy Linux - General 4 08-10-2006 09:40 PM
FC4 Problems with desktop selection hobz Fedora 0 07-27-2006 02:10 AM
Fullscreen console apps in GNU Screen in an X term (of some sort) problems. bugmenot60 Linux - Software 6 07-21-2006 05:48 AM
Frub problems loading kernal, returns to selection screen hypoglycemic Linux - Software 2 07-16-2005 10:50 PM
Problems with Java Web Start (java 1.4.2 - 1.5.0) on Slackware Egy Slackware 11 05-10-2005 07:30 AM


All times are GMT -5. The time now is 12:02 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration