Click here to Skip to main content
15,889,527 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
The following code is supposed to accomplish a few things. It is supposed to sort an existing array INTO a new array (sorted), from smallest to largest. In addition, it achieves this without the use of java's built in sorting (.sort, insertion, bubble etc). I am having issues though when it comes to "crossing out" values from the old array so that they do not present themselves again in the loop. Ideally, I should be able to run this and get a print out of the original array (unsorted) and the new array (sorted from smallest to largest).

Any suggestions?


Java
public class Lab1
{
  public static void main(String argv[])
  {
     int ar[] = { 7, 5, 2, 8, 4, 9, 6 };
     int sorted[] = new int[7];

     for(int i = 0; i < ar.length; i++)
     {
     int smallest = ar[0];
     int index = 0;

         for(int j = 0; j < ar.length; j++)
         {
             System.out.println("ar[" + j + "] = " + ar[j]);
             if (ar[j] < smallest)
             {
                 smallest = ar[j];
                 index = j;
             }
         }

         sorted[i] = smallest;
         ar[index] = Integer.MAX_VALUE;
      }

      for (int i = 0; i < sorted.length; i++)
      {
          System.out.println("sorted[" + i + "] = " + sorted[i]);
      }
   }
}
Posted
Updated 26-Jan-16 12:17pm
v4
Comments
PIEBALDconsult 26-Jan-16 19:16pm    
For what you describe, I prefer not to sort, but to insert in a sorted manner.
To insert a value, start at the "end" and shift the existing values down until you reach the correct spot, then just insert. Done.

1 solution

I suspect an error in your code, it should not compile at all.
Java
int ar[] = 7, 5, 2, 8, 4, 9, 6 };

and there is another error in
Java
int sorted[] = new int[0];

you need to give the size of sorted

otherwise, you should try to replace
Java
smallest = Integer.MAX_VALUE;

with
Java
ar[index] = Integer.MAX_VALUE;
 
Share this answer
 
Comments
Member 12289027 26-Jan-16 17:53pm    
I made the necessary changes, unfortunately giving the size of sorted has for whatever reason made the program print out 7 iterations of ar (original array before sorting) in addition to the sorted array.
Patrice T 26-Jan-16 17:57pm    
Show your corrections
use Improve question
Member 12289027 26-Jan-16 18:03pm    
Updated thread code, thank you very much btw. I've been scratching my head for hours.
Patrice T 26-Jan-16 18:11pm    
Do you still have the same error at line 31 ?
if no, Improve the question to have the problem description to match actual code
Member 12289027 26-Jan-16 18:14pm    
Nope!, the error on line 31 is gone.

I commented out the first print line, in that first outer loop, and now everything looks hunky-dory. Strange that instating that print line makes everything wonky though. =/

System.out.println("ar[" + j + "] = " + ar[j]);

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900