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 3 of 3

Thread: array sorting

  1. #1
    Junior Member
    Join Date
    Jun 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default array sorting

    i know there are lots of codes about this but i tried to write my own.

    what is wrong with that?

    public static void sortingArrays( int[] a){
     
                         int k = 0;
    	int index = a[0];
    	int[] a2 = new int[a.length];
     
    	for(int i=0; i<a.length; i++){
    		for(int j=0; j<a.length; j++){
    			if ( a[j] > index ){
    				index = a[j];
    				k=j;}
    		}
    		a[k] = 0;
    		a2[i] = index;		
    		System.out.print(a2[i]);   }	
    }
    Last edited by copeg; June 26th, 2010 at 10:28 AM. Reason: Please use the CODE tags


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: array sorting

    You never reset your index value for your outer loop so it will re-evaluate each inner loop based upon the next found max number. Some pointers
    1) Arrays.sort() is much easier and much faster for simple array sorting.
    2) you can use Integer.MIN_VALUE rather than 0 if you wish to also sort an array with negative numbers
    3) see Sorting algorithm - Wikipedia, the free encyclopedia if you wish to try your hand at more efficient sorting algorithms.

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

    Default Re: array sorting

    thank you for your answer. I corrected the code, now I'm happy

Similar Threads

  1. Sorting/Lexicographic =)
    By jcs990 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: March 12th, 2010, 11:19 PM
  2. Sorting an Array
    By Prince_85 in forum Algorithms & Recursion
    Replies: 2
    Last Post: February 21st, 2010, 03:00 PM
  3. [SOLVED] sorting
    By kite98765 in forum Algorithms & Recursion
    Replies: 8
    Last Post: February 4th, 2010, 08: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

Tags for this Thread