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

Thread: help in sorting the array

  1. #1
    Junior Member
    Join Date
    Oct 2012
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default help in sorting the array

    i'm trying to sort this array in special method by :
    - Counting the number of smaller elements to find the correct position i.
    – If the element is in its correct position, move to the succeeding element.
    – Otherwise, swap the current element with the one found in position i.
    – Repeat the previous steps till you reach the last element.
    So i did this code but something wrong so please help :


    public class Ass {

    public static void swap(int [] x) {
    int i = 0 ;
    while (i<x.length){

    for (int j = 0 ; j<x.length ; j++){
    int b = 0 ;
    if ( x[i] > x[j] ) {
    b++ ;
    }
    if (b!=0) {
    int tmp = x[i] ;
    x[i]=x[b] ;
    x[b]= tmp ;

    }





    }
    i++ ;
    }
    }
    public static void main (String [] args) {
    int [] array = {5,7,3,6,9} ;
    swap(array) ;
    for (int k = 0 ; k<array.length ; k++)
    System.out.print(array[k] + " ");
    }
    }


  2. #2
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: help in sorting the array

    Please use code tags as described in the announcements page as well as in your previous thread.

  3. #3
    Junior Member
    Join Date
    Oct 2012
    Posts
    5
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: help in sorting the array

    Hi,
    I checked your code, even executed the same and what I found was,
    a change is needed in the swap code.
    what you have written is :

     int tmp = x[i] ;
     x[i]=x[b] ;
     x[b]= tmp ;

    which should be as follows :

     int tmp = x[i] ;
     x[i]=x[j] ;
     x[j]= tmp ;


    Because, since you are comparing the values at i and j positions, and if you find value at i is greater than value at j, then you should swap the values at i and j only but right now you are performing the swap between the values at i and b, which is wrong.

    b is just like a check variable ,to find out whether the value at ' i greater than j ' condition is true or false.

    actually there is no need of the variable b. you can compare the values and directly swap like below :

         if (x[i] > x[j]) {
    			  int tmp = x[i];
    			  x[i] = x[j];
    			  x[j] = tmp;
     
    			}

  4. #4
    Junior Member
    Join Date
    Oct 2012
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: help in sorting the array

    thnkxxx so muchh but this prints the biggest first i want the smallest , any help ??

  5. #5
    Junior Member
    Join Date
    Oct 2012
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: help in sorting the array

    nvmmm i solved it thnkxxx

  6. #6
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: help in sorting the array

    @ind
    Please see The problem with spoonfeeding.

    Since the OP has already taken the code provided and run with it, I am not going to delete it, but please see the link and try to be more suggestive and a little less free answers in your posts.

Similar Threads

  1. Array sorting
    By renars in forum Java Theory & Questions
    Replies: 4
    Last Post: May 17th, 2011, 10:45 AM
  2. Sorting an array
    By thebestpearl in forum Collections and Generics
    Replies: 5
    Last Post: April 17th, 2011, 08:58 PM
  3. [SOLVED] Sorting an Array
    By petemyster in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 12th, 2010, 11:07 AM
  4. array sorting
    By herbisey in forum What's Wrong With My Code?
    Replies: 2
    Last Post: June 27th, 2010, 12:07 PM
  5. Sorting an Array
    By Prince_85 in forum Algorithms & Recursion
    Replies: 2
    Last Post: February 21st, 2010, 03:00 PM