| | |
To arrange Array Members in ascending order !
![]() |
•
•
Join Date: Apr 2007
Posts: 5
Reputation:
Solved Threads: 0
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.
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.
•
•
Join Date: Mar 2005
Posts: 9
Reputation:
Solved Threads: 0
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.
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.
•
•
Join Date: Apr 2007
Posts: 5
Reputation:
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;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.
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.
Declare the array
int [] arr ;
Get the array size from user like this
Convert the String to int value and initialise the size of array like this
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 [] arr ;
Get the array size from user like this
Java Syntax (Toggle Plain Text)
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
Java Syntax (Toggle Plain Text)
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
And for sorting array you can use even a single variable and swap them instead of taking 2 variable.Java Syntax (Toggle Plain Text)
int num=0; for (int j = 0; j <size; j++) {<blockquote>String input = br.readLine(); num = Integer.parseInt(input); arr[j] = num; </blockquote>}
Java Syntax (Toggle Plain Text)
<blockquote>if(arr1[j]>arr1[j+1]) {<blockquote>temp = a[j]; a[j] = a[j+1]; a[j+1]= temp; </blockquote>} </blockquote>
•
•
Join Date: Jan 2008
Posts: 18
Reputation:
Solved Threads: 0
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
{
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
![]() |
Similar Threads
- ascending order (C++)
- Random number with ascending order help (C++)
- names in ascending order problem (C++)
Other Threads in the Java Forum
- Previous Thread: help with pseudocode
- Next Thread: Sending XML Message to Web Service
| Thread Tools | Search this Thread |
android api applet application apps array arrays automation awt bidirectional binary birt bluetooth businessintelligence busy_handler(null) card class classes client code collision columns component constructor crashcourse database designadrawingapplicationusingjavajslider draw eclipse error errors eventlistener exception expand fractal game givemetehcodez graphics gui guidancer html ide image inetaddress integer intellij j2me java javadoc javafx javamicroeditionuseofmotionsensor javaprojects jme jni jpanel jtree julia linux list loop machine map method methods mobile mobiledevelopmentcreatejar myaggfun netbeans newbie oracle physics plazmic print problem program project radio recursion scanner server set sharepoint smart sms smsspam sort sortedmaps sql string subclass support swing textfield threads tree trolltech unlimited utility webservices windows





