954,510 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

To arrange Array Members in ascending order !

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.

libran
Newbie Poster
5 posts since Apr 2007
Reputation Points: 10
Solved Threads: 0
 

Just do a google search for array sorting. Bubble sort and quick sort are simple ones you can pick up.

Phaelax
Practically a Posting Shark
858 posts since Mar 2004
Reputation Points: 92
Solved Threads: 51
 

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.

nordmann
Newbie Poster
9 posts since Mar 2005
Reputation Points: 10
Solved Threads: 0
 

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;iarr1[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

libran
Newbie Poster
5 posts since Apr 2007
Reputation Points: 10
Solved Threads: 0
 

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

InputStreamReader in = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(in);
String size = br.readLine();


Convert the String to int value and initialise the size of array like this

int i = Integer.parseInt(size); 
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
[INDENT]

int num=0;
for (int j = 0; j <size; j++) {[INDENT]String input = br.readLine();
num = Integer.parseInt(input);  
arr[j] = num;
[/INDENT]}

[/INDENT]And for sorting array you can use even a single variable and swap them instead of taking 2 variable. [INDENT]

[INDENT]if(arr1[j]>arr1[j+1])
{[INDENT]temp = a[j];
a[j] = a[j+1];
a[j+1]= temp;
[/INDENT]}
[/INDENT]

[/INDENT]

lucky1981_iway
Light Poster
46 posts since Apr 2007
Reputation Points: 10
Solved Threads: 3
 

java.util.sort may come in handy as well

stultuske
Posting Sensei
3,137 posts since Jan 2007
Reputation Points: 1,114
Solved Threads: 433
 

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;iarr1[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

cebubinary
Newbie Poster
18 posts since Jan 2008
Reputation Points: 10
Solved Threads: 0
 

class nandan
{
public void bubblesort(int Arr[])
{
int i = 0, j = 0, temp = 0, len=Arr.length[];
for(i=0;iArr[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]);
}
}
}
}

nupamanyu
Newbie Poster
1 post since Jan 2010
Reputation Points: 10
Solved Threads: 0
 

the last post in this thread is over two years old.
if he didn't find it by now, I doubt he still cares

stultuske
Posting Sensei
3,137 posts since Jan 2007
Reputation Points: 1,114
Solved Threads: 433
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You