Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Reply
 
Thread Tools Search this Thread
  #1  
Old 24-11-2009
Member
 
Join Date: May 2009
Posts: 1,060
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.
Reply With Quote
  #2  
Old 24-11-2009
Member
 
Join Date: May 2008
Posts: 2,293
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.
Reply With Quote
  #3  
Old 24-11-2009
Member
 
Join Date: Oct 2005
Posts: 2,389
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);
Reply With Quote
  #4  
Old 24-11-2009
Member
 
Join Date: May 2008
Posts: 2,383
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;
  
      }
  
       
  
      }
  
      }
  
      }
__________________
The FIFA Manager 2009 PC Game
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


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


All times are GMT +5.5. The time now is 01:33 PM.