Welcome to the most active Linux Forum on the web.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
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.


  Search this Thread
Old 09-22-2006, 11:17 PM   #1
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;
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-22-2006, 11:54 PM   #2
LQ 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 ;-)):
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

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


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 08:40 PM
FC4 Problems with desktop selection hobz Fedora 0 07-27-2006 01:10 AM
Fullscreen console apps in GNU Screen in an X term (of some sort) problems. bugmenot60 Linux - Software 6 07-21-2006 04:48 AM
Frub problems loading kernal, returns to selection screen hypoglycemic Linux - Software 2 07-16-2005 09:50 PM
Problems with Java Web Start (java 1.4.2 - 1.5.0) on Slackware Egy Slackware 11 05-10-2005 06:30 AM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:31 AM.

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