944,091 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 42129
  • Java RSS
Apr 29th, 2007
0

To arrange Array Members in ascending order !

Expand Post »
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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
libran is offline Offline
5 posts
since Apr 2007
Apr 29th, 2007
0

Re: To arrange Array Members in ascending order !

Just do a google search for array sorting. Bubble sort and quick sort are simple ones you can pick up.
Reputation Points: 92
Solved Threads: 51
Practically a Posting Shark
Phaelax is offline Offline
856 posts
since Mar 2004
May 1st, 2007
0

Re: To arrange Array Members in ascending order !

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
nordmann is offline Offline
9 posts
since Mar 2005
May 2nd, 2007
0

Re: To arrange Array Members in ascending order !

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
libran is offline Offline
5 posts
since Apr 2007
May 2nd, 2007
0

Re: To arrange Array Members in ascending order !

Declare the array
int [] arr ;
Get the array size from user like this

Java Syntax (Toggle Plain Text)
  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
Java Syntax (Toggle Plain Text)
  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
Java Syntax (Toggle Plain Text)
  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.
Java Syntax (Toggle Plain Text)
  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>
Reputation Points: 10
Solved Threads: 3
Light Poster
lucky1981_iway is offline Offline
46 posts
since Apr 2007
May 2nd, 2007
0

Re: To arrange Array Members in ascending order !

java.util.sort may come in handy as well
Reputation Points: 938
Solved Threads: 356
Posting Maven
stultuske is offline Offline
2,525 posts
since Jan 2007
Jan 8th, 2008
-1

Re: To arrange Array Members in ascending order !

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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
cebubinary is offline Offline
18 posts
since Jan 2008
Jan 15th, 2010
-1
Re: To arrange Array Members in ascending order !
class nandan
{
public void bubblesort(int Arr[])
{
int i = 0, j = 0, temp = 0, len=Arr.length[];
for(i=0;i<len;i++)
{
for(j=0;j<(len-1)-i;j++)
if(Arr[j]>Arr[j+1])
{
temp=Arr[j];
Arr[j]=Arr[j+1];
Arr[j+1]=temp;
}
for(i=0;i<10;i++)
{
System.out.println(Arr[i]);
}
}
}
}
Reputation Points: 10
Solved Threads: 0
Newbie Poster
nupamanyu is offline Offline
1 posts
since Jan 2010
Jan 17th, 2010
0
Re: To arrange Array Members in ascending order !
the last post in this thread is over two years old.
if he didn't find it by now, I doubt he still cares
Reputation Points: 938
Solved Threads: 356
Posting Maven
stultuske is offline Offline
2,525 posts
since Jan 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: How to rename existing (running) jar file?
Next Thread in Java Forum Timeline: converting the java code to euqivalent c++ code





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC