To arrange Array Members in ascending order !

Reply

Join Date: Apr 2007
Posts: 5
Reputation: libran is an unknown quantity at this point 
Solved Threads: 0
libran libran is offline Offline
Newbie Poster

To arrange Array Members in ascending order !

 
0
  #1
Apr 29th, 2007
I m trying this Java program which arranges members of array in an ascending order. The size of array is given by User (an int).

I hav spent lot of time thinking abt its logic but didnt get thru.
I am not using any readymade libraries and trying pure logic for this.

Anyone having any idea abt the logic, let me guide.

Thnx.
Reply With Quote Quick reply to this message  
Join Date: Mar 2004
Posts: 763
Reputation: Phaelax is on a distinguished road 
Solved Threads: 38
Phaelax Phaelax is offline Offline
Master Poster

Re: To arrange Array Members in ascending order !

 
0
  #2
Apr 29th, 2007
Just do a google search for array sorting. Bubble sort and quick sort are simple ones you can pick up.
Reply With Quote Quick reply to this message  
Join Date: Mar 2005
Posts: 9
Reputation: nordmann is an unknown quantity at this point 
Solved Threads: 0
nordmann nordmann is offline Offline
Newbie Poster

Re: To arrange Array Members in ascending order !

 
0
  #3
May 1st, 2007
Why would you not use the facilities offered in the standard JCL? Are you doing this as an exercise for a programming class, or are you coding something for a real-world project?

If it's for a project and you want to get the job done with a minimum of effort and a maximum of quality, look into the java.util.Arrays class, which has various overloaded sort methods. If sorting Objects, you'll want to have them implement the java.util.Comparator interface in order to define the sorting order.
Reply With Quote Quick reply to this message  
Join Date: Apr 2007
Posts: 5
Reputation: libran is an unknown quantity at this point 
Solved Threads: 0
libran libran is offline Offline
Newbie Poster

Re: To arrange Array Members in ascending order !

 
0
  #4
May 2nd, 2007
Yes, I am trying this code for a problem given to me in an assignment. I hav succeeded a bit in cracking the logic of sorting members in ascending order. Here it is :

class SortArray
{
public static void main(String args[])
{
int arr1[] = {11,15,8,9,3,6,10,12,5,1,2,13,4,7,14};
for (int i=0;i<arr1.length;i++) System.out.println(arr1[i]);
for(int i=0;i<(arr1.length-1);i++)
{
for (int j=i+1;j<arr1.length;j++)
{
if(arr1[i]>arr1[j])
{
int x,y;
x=arr1[i];
y=arr1[j];
arr1[i] = y;
arr1[j] = x;
}
}
}

System.out.println("Array after sorting in ascending order");
System.out.println();
for (int i=0;i<arr1.length;i++) System.out.println(arr1[i]);

}
}

This wrks fine iff array members are initialised inadvance. I want this prgm to be like : an object of class SortArray will invoke diff methods to initialize size of array, random assignment of its members and then arranging them in ascending order.

Guide me pls. thnx.
Reply With Quote Quick reply to this message  
Join Date: Apr 2007
Posts: 46
Reputation: lucky1981_iway is an unknown quantity at this point 
Solved Threads: 3
lucky1981_iway's Avatar
lucky1981_iway lucky1981_iway is offline Offline
Light Poster

Re: To arrange Array Members in ascending order !

 
0
  #5
May 2nd, 2007
Declare the array
int [] arr ;
Get the array size from user like this

  1.  
  2. InputStreamReader in = new InputStreamReader(System.in);
  3. BufferedReader br = new BufferedReader(in);
  4. String size = br.readLine();

Convert the String to int value and initialise the size of array like this
  1.  
  2. int i = Integer.parseInt(size);
  3. arr = new int[i]

Now repeat the above steps for receiving the input number from user until it's not met to array size by using for loop like this
  1.  
  2. int num=0;
  3. for (int j = 0; j <size; j++) {<blockquote>String input = br.readLine();
  4. num = Integer.parseInt(input);
  5. arr[j] = num;
  6. </blockquote>}
And for sorting array you can use even a single variable and swap them instead of taking 2 variable.
  1. <blockquote>if(arr1[j]>arr1[j+1])
  2. {<blockquote>temp = a[j];
  3. a[j] = a[j+1];
  4. a[j+1]= temp;
  5. </blockquote>}
  6. </blockquote>
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 706
Reputation: stultuske is a jewel in the rough stultuske is a jewel in the rough stultuske is a jewel in the rough 
Solved Threads: 84
stultuske's Avatar
stultuske stultuske is offline Offline
Master Poster

Re: To arrange Array Members in ascending order !

 
0
  #6
May 2nd, 2007
java.util.sort may come in handy as well
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 18
Reputation: cebubinary is an unknown quantity at this point 
Solved Threads: 0
cebubinary cebubinary is offline Offline
Newbie Poster

Re: To arrange Array Members in ascending order !

 
0
  #7
Jan 8th, 2008
class SortArray
{
public static void main(String args[])
{
int arr1[] = {11,15,8,9,3,6,10,12,5,1,2,13,4,7,14};
for (int i=0;i<arr1.length;i++) System.out.println(arr1[i]);
for(int i=0;i<(arr1.length-1);i++)
{
for (int j=i+1;j<arr1.length;j++)
{

if(arr1[i]>arr1[j])
{

arr1[i]=arr1[i]+arr1[j];
arr1[j]=arr1[i]-arr1[j];
arr1[i]=arr1[i]-arr1[j];



}
}
}

System.out.println("Array after sorting in ascending order");
System.out.println();
for (int i=0;i<arr1.length;i++)
System.out.println(arr1[i]);

}
}



hey i have edited ur codes
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC