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.

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

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.

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);
for(int i=0;i<(arr1.length-1);i++)
{
for (int j=i+1;j<arr1.length;j++)
{
if(arr1>arr1[j])
{
int x,y;
x=arr1;
y=arr1[j];
arr1 = 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);

}
}

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.

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

``````InputStreamReader in = new InputStreamReader(System.in);

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

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

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

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

java.util.sort may come in handy as well

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);
for(int i=0;i<(arr1.length-1);i++)
{
for (int j=i+1;j<arr1.length;j++)
{

if(arr1>arr1[j])
{

arr1=arr1+arr1[j];
arr1[j]=arr1-arr1[j];
arr1=arr1-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);

}
}

hey i have edited ur codes

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);
}
}
}
}

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.