Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Sorting an Array

  1. #1
    Junior Member
    Join Date
    Nov 2010
    Posts
    10
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Sorting a number array (ascending)

    Hi, I'm in the middle of trying to write a program that will sort an array from lowest to highest, and I can't get the basics to work right. Any ideas? It starts off ok but then starts listing in descending order.
    public class SortingArray
    {
    	static int TestArray[] = {5,10,8,16,20,17,18,11,1,3,13,19};
     
    	public static void main(String Args[])
    	{
    		for (int count=0; count<TestArray.length;count++)
    		{
    			for (int count2=1;count2<TestArray.length;count2++)
    			{
    				if (TestArray[count] > TestArray[count2])
    				{
    					int temp = TestArray[count];
    					TestArray[count]=TestArray[count2];
    					TestArray[count2]=temp;
    				}
    			}
    		}
    		System.out.print(TestArray[0]+ " " +TestArray[1]+ " " +TestArray[2]+ " " +TestArray[3]+ " " +TestArray[4]+ " " +TestArray[5]);
    	}
    }
    Last edited by petemyster; December 12th, 2010 at 09:43 AM.


  2. #2
    Junior Member
    Join Date
    Nov 2010
    Posts
    10
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Sorting a number array (ascending)

    Quote Originally Posted by petemyster View Post
    Hi, I'm in the middle of trying to write a program that will sort an array from lowest to highest, and I can't get the basics to work right. Any ideas? It starts off ok but then starts listing in descending order.
    public class SortingArray
    {
    	static int TestArray[] = {5,10,8,16,20,17,18,11,1,3,13,19};
     
    	public static void main(String Args[])
    	{
    		for (int count=0; count<TestArray.length;count++)
    		{
    			for (int count2=1;count2<TestArray.length;count2++)
    			{
    				if (TestArray[count] > TestArray[count2])
    				{
    					int temp = TestArray[count];
    					TestArray[count]=TestArray[count2];
    					TestArray[count2]=temp;
    				}
    			}
    		}
    		System.out.print(TestArray[0]+ " " +TestArray[1]+ " " +TestArray[2]+ " " +TestArray[3]+ " " +TestArray[4]+ " " +TestArray[5]);
    	}
    }
    Silly mistake really, got it sorted.

    	public static void main(String Args[])
    	{
    		for (int count=0; count<TestArray.length;count++)
    		{
    			for (int count2=count+1;count2<TestArray.length;count2++)
    			{
    				if (TestArray[count] > TestArray[count2])
    				{
    					int temp = TestArray[count];
    					TestArray[count]=TestArray[count2];
    					TestArray[count2]=temp;
    				}
    			}
    		}
    		System

Similar Threads

  1. array sorting
    By herbisey in forum What's Wrong With My Code?
    Replies: 2
    Last Post: June 27th, 2010, 12:07 PM
  2. Sorting an Array
    By Prince_85 in forum Algorithms & Recursion
    Replies: 2
    Last Post: February 21st, 2010, 02:00 PM
  3. [SOLVED] sorting
    By kite98765 in forum Algorithms & Recursion
    Replies: 8
    Last Post: February 4th, 2010, 07:34 AM
  4. Having trouble insert/sorting array values w/ binary searching.
    By bh-chobo in forum Collections and Generics
    Replies: 4
    Last Post: October 8th, 2009, 02:38 AM
  5. [SOLVED] Java program to sort arrays containing dates
    By scottyadam in forum Collections and Generics
    Replies: 1
    Last Post: March 9th, 2009, 06:08 PM