Results 1 to 3 of 3
  1. #1
    jca
    jca is offline Member
    Join Date
    Jan 2011
    Posts
    1
    Rep Power
    0

    Unhappy ascending order using array rush

    nid some help we use basic code like int arr []=new arr[5].... like that....




    import java.io.*;
    public class aw{
    public static void main(String args[])throws IOException{
    BufferedReader bsit=new BufferedReader (new InputStreamReader (System.in));

    int[]array = new int[10];
    int x,y=0;

    for (x = 0; x < 10; x++){
    System.out.print("Enter a number: ");
    array[x] = Integer.parseInt(bsit.readLine());



    please continue this code......

  2. #2
    user0 is offline Senior Member
    Join Date
    Dec 2010
    Posts
    100
    Rep Power
    0

    Default

    Hey - you have to be more clear when you post a question for anyone to be able to help you. From your post subject, I am assuming you are looking to sort an array in ascending order.

    The Arrays class has a method sort() you might want to use in your case.

    The code will look like:

    Arrays.sort(arrays);

    Hope that helps.

    Best,
    --user0--

  3. #3
    JavaHater is offline Senior Member
    Join Date
    Dec 2010
    Posts
    165
    Rep Power
    10

    Default

    Compile, run and debug the code as you wish. Understand what the program is doing and insert your own explanation as comments.
    Java Code:
    import java.io.*;                                
    public class IntegerSort {
            public static void main(String[] args) throws IOException{
                    BufferedReader bsit=new BufferedReader (new InputStreamReader (System.in));
                    int[] array = new int[10];
                    int x,y=0;
                    for (x = 0; x < 10; x++){
                            System.out.print("Enter a number: ");
                            array[x] = Integer.parseInt(bsit.readLine());
                    }
                    int[] result = sort( array );
                    for(int i=0; i< result.length; i++){
                            System.out.println( result[i] );
                    }
            }
    
            public static int[] sort(int[] a) {
                    int tmp;
                    int smallest;
                    for (int i = 0; i < a.length; i++) {
                            smallest = i;
                    for (int j = i + 1; j < a.length; j++) {
                                    // ascending order
                            if(  a[j] < a[smallest] ) {
                                            smallest = j;
                            }
                    }
                            swap( a , i, smallest);
            }
                    return a;
            }
            public static void swap( int[] array, int a, int b ){
                    int tmp;
            tmp = array[a];
            array[a] = array[b];
            array[b] = tmp;
            }
    }
    Last edited by JavaHater; 01-03-2011 at 05:26 AM.

Similar Threads

  1. Checking ascending order of array
    By counterfox in forum New To Java
    Replies: 3
    Last Post: 10-22-2010, 11:44 PM
  2. Spiral Array Order Number
    By rendra in forum Advanced Java
    Replies: 14
    Last Post: 11-20-2009, 04:20 AM
  3. sorting variable values in ascending order?
    By jeffrey in forum New To Java
    Replies: 0
    Last Post: 07-14-2009, 08:59 AM
  4. How to sort array objects into alphabetical order...
    By lisalala in forum New To Java
    Replies: 5
    Last Post: 03-03-2009, 12:01 PM
  5. How to add coins in ascending order in arraylist
    By tribujohn in forum New To Java
    Replies: 2
    Last Post: 01-23-2009, 05:31 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •