Results 1 to 4 of 4

Thread: program to sort numbers in java.

  1. #1
    Join Date
    May 2009
    Posts
    1,070

    program to sort numbers in java.

    Hi,
    I am just started to learn java language.I tried to solve various program on java.Today my friend asked me how to write a program to sort numbers. I tried various method but I didn't get the solution.So I just asked you how can I write this program?If you give me any logic behind this program then also ok.

  2. #2
    Join Date
    May 2008
    Posts
    2,297

    Re: program to sort numbers in java.

    I write following program in java using array just try it.


    Code:
    import java.util.*;
    public class NS2
    {
    public static void main(String[] args)
    {
    Scanner t=new Scanner(System.in);
    int[] ns=new int[10];
    for (int p=0;p<10;p++)
    {
    Random R=new Random();
    int x=R.nextInt(999)+1;
    ns[p]=x;
    }
    System.out.println("Numbers Before Sort:\n"+ns[1]+","+ns[2]+","+ns[3]+","+ns[4]+","+ns[5]+","+ns[6]+","+ns[7]+","+ns[8]+","+ns[9]+"\n");
    Arrays.sort(ns);
    System.out.print("Numbers After Sort:\n"+ns[1]+","+ns[2]+","+ns[3]+","+ns[4]+","+ns[5]+","+ns[6]+","+ns[7]+","+ns[8]+","+ns[9]);
    }
    }
    I this will help you.

  3. #3
    Join Date
    Oct 2005
    Posts
    2,393

    Re: program to sort numbers in java.

    You can write your program using arrays.There are two ways you can write this program .

    1.You have to place each value in array then using for loop you have to compare each value with another.This is most tedious process.

    or

    2.you can use simple Arrays.sort() method to sort number.
    eg.int[] array = new int[]{154, 31, 524, 24};
    Arrays.sort(array);

  4. #4
    Join Date
    May 2008
    Posts
    2,389

    Re: program to sort numbers in java.

    Just try this code.



    Code:
     
          public class SortNumbers
       
          {
       
          public static void sortart(int [] array1, int pen)
       
          {
      
          int x,y;
       
          int temp;
       
          int sortTheNumbers = len - 1;
       
          for (x = 0; x < sortTheNumbers; ++x)
      
          {
      
          for (y = 0; y < sortTheNumbers; ++y)
      
          if(array1[y] < array1[y + 1])
      
          {
      
          temp = array1[y];
      
          array1[y] = array1[y + 1];
     
          array1[y + 1] = temp;
      
          }
      
           
      
          }
      
          }
      
          }

Similar Threads

  1. Program to print GCD of given numbers
    By Bhavesh_seth in forum Software Development
    Replies: 5
    Last Post: 15-03-2011, 08:03 PM
  2. How can you sort data in columns with Numbers for iPad?
    By Aasha in forum Portable Devices
    Replies: 3
    Last Post: 01-10-2010, 12:03 PM
  3. How to sort LinkedList elements using java program?
    By KADRI in forum Software Development
    Replies: 4
    Last Post: 22-01-2010, 08:45 PM
  4. Need C program code to count numbers of negative & positive numbers
    By Sarfaraj Khan in forum Software Development
    Replies: 5
    Last Post: 16-01-2010, 02:00 PM
  5. how to sort tuples of basis of numbers?
    By Saaz in forum Software Development
    Replies: 2
    Last Post: 19-06-2009, 02:30 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,714,109,480.00063 seconds with 17 queries