answersLogoWhite

0


Best Answer

If you are really lazy, like me, I just use what Java 5.0 has advailable.

Just call Arrays.sort();

put your data structure as the parameter, it is a void method.

i.e.

int [] arr = { 2,31, 2, 1, 42, 34, 42, 1337, 101 };

Arrays.sort( arr );

//now arr is sorted, yay the magic of Java.

User Avatar

Wiki User

16y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

11y ago

class sorting

{

public static void main(String as[])

{

int[] num={7,4,5,9};

int l=num.length;

int i,j;

for(i=1;i<l;i++)

{

System.out.println(" "+num[i]);

}

System.out.println("\n");

System.out.println("Ascending order");

Arrays.sort(num);

for(i=0;i<l;i++)

{

System.out.println(""+num[i]);

}

}

}

This answer is:
User Avatar

User Avatar

Wiki User

11y ago

class Sort

{

public static void main(String args[])

{

int arr;

int arr = 4,8,3,2,5,6;

array.sort(arr);

System.out.println("sort the given numbers in ascending order");

}

}

This answer is:
User Avatar

User Avatar

Wiki User

11y ago

import java.util.*;

class table

{

public static void main (String argsp[])

{

Scanner in = new Scanner (System.in);

int a,b,c,d=0;

System.out.print ("\n\t Enter the number to print table = ");

a=in.nextInt();

System.out.print ("\n\t Enter 1 for ascending and 2 for descending order = ");

c=in.nextInt();

if (c==1)

{

System.out.println ("\n\t Table of "+a+" in ascending order ");

b=1;

while (b<=10)

{

d=b*a;

System.out.println ("\n\t "+a+" * "+b+" = "+d);

++b;

}

}

else if (c==2)

{

System.out.println ("\n\t Table of "+a+" in ascending order ");

b=10;

while (b>0)

{

d=b*a;

System.out.println ("\n\t "+a+" * "+b+" = "+d);

--b;

}

}

else

System.out.println ("\n\t Invalid number ");

}

}

This answer is:
User Avatar

User Avatar

Wiki User

11y ago

import java.util.*;

public class array

{

public static void main(String[]args)

{

int num[]={5,4,3,2,1,0};

int l=num.length;

int i,j;

System.out.println("given number");

for(i=0;i<l;i++)

System.out.println(""+num[i]);

}

System.out.println("\n");

System.out.println("ascending order");

Array.sort(num);

for(i=0;i<l;i++)

{

System.out.println(" "+num[i]);

}

}

This answer is:
User Avatar

User Avatar

Wiki User

11y ago

If it is for a programming exercise, I suggest the bubble sort, which is fairly simple to implement (but not very efficient for large arrays). The basic idea is to compare consecutive elements with one another; if they are out of order (the first is greater than the second, if you want an ascending order), exchange them. Repeat this until you reach the end of the array. Then, repeat the whole cycle over and over again, until all elements are sorted.

This answer is:
User Avatar

User Avatar

Wiki User

10y ago

Get the input; store it in an Array or perhaps an ArrayList; sort it; print the result out.

For the ordering, you might use a bubble sort - this is not very efficient, but fairly easy to program.

This answer is:
User Avatar

User Avatar

Wiki User

10y ago

I suggest you use a bubble sort, to keep it simple. That is, if the array has (for example) 10 elements, just compare element #1 and element #2, and exchange if they are out of order, then do the same for elements #2 and #3, etc., up to elements #9 and #10. Then repeat from the beginning, either a total of 9 times (assuming 10 elements), or until no elements were found that are out of order.

This answer is:
User Avatar

User Avatar

Wiki User

10y ago

I would suggest you try something simple, like a bubble sort. It isn't very efficient for large arrays, but it works.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: JAVA program for sorting the elements in the array?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

What does it mean to have a string split in java?

To have a string split in Java means that a string array, containing substrings (can be delimited by elements of a specified string or Unicode character array), is returned.


How do you find the average of rows and columns in a 2d array in java?

You add up all the array elements, then divide by the number of elements. You can use a nested for() loop in Java; inside the inner for() loop, you can both increase a counter (to count how many elements there are), and add to a "sum" variable.


Write a program sort a list of names in alphabetical order in java programming language?

// Let's assume we're sorting the characters in String toSort // convert the String to an array of characters char[] chars = toSort.toCharArray(); // let Java do the sorting for you Arrays.sort(chars); // recreate the original String with the newly sorted array of characters toSort = new String(chars);


How do you initialize an array variable in java program?

An array in java is a collection of items stored into a single unit. The array has some number of slots (elements), each slot in the array can hold an object or a primitive value. Arrays in java are objects that can be treated just like other objects in the languageArrays can contain any type of element value , but we can't store different types in a single array. We can have an array of integers or an array of strings or an array of arrays.To create an array in java ,use three steps1. Declare a variable to hold the array2. Create a new array object and assign it to the array variable3. Store things in that array


Can an array contain elements of an object type?

Yes. An array of arrays is nothing more than a multi-dimensional array.

Related questions

What is the Java program for sorting an array?

Java has a very efficient built in implementation of quick sort. You can use it on any array of primitives or Comparable Objects by invoking Arrays.sort(&lt;array&gt;) See related link.


Sorting an array of numbers in java?

here you will a good example on java sorting algorithm application http://javacodespot.blogspot.com/2010/08/java-sorting-animations.html http://javacodespot.blogspot.com/


Sorting of array through function in java programming?

// the build in sorting functions in Java will sort pretty much any array // of Comparable objects or primitives Arrays.sort(someArray);


What does it mean to have a string split in java?

To have a string split in Java means that a string array, containing substrings (can be delimited by elements of a specified string or Unicode character array), is returned.


What is an error in java?

An error or more commonly known as an Exception is a situation where the java program behaves in a way it is not supposed to do so. It is a problem in the code that is causing the JVM to terminate or throw error messages in the console. Ex: When you initialize an array list with 10 elements and try to access the 11th element in the array list you will get an array index out of bounds exception.


What are benefits of array in java?

array example in java


How do you find the average of rows and columns in a 2d array in java?

You add up all the array elements, then divide by the number of elements. You can use a nested for() loop in Java; inside the inner for() loop, you can both increase a counter (to count how many elements there are), and add to a "sum" variable.


Why might one need a sting array object in Java?

One might need a sting array object in Java to use protective measures on one's computer to prevent one's program from writing outside the bounds of the array.


Write a program sort a list of names in alphabetical order in java programming language?

// Let's assume we're sorting the characters in String toSort // convert the String to an array of characters char[] chars = toSort.toCharArray(); // let Java do the sorting for you Arrays.sort(chars); // recreate the original String with the newly sorted array of characters toSort = new String(chars);


How do you initialize an array variable in java program?

An array in java is a collection of items stored into a single unit. The array has some number of slots (elements), each slot in the array can hold an object or a primitive value. Arrays in java are objects that can be treated just like other objects in the languageArrays can contain any type of element value , but we can't store different types in a single array. We can have an array of integers or an array of strings or an array of arrays.To create an array in java ,use three steps1. Declare a variable to hold the array2. Create a new array object and assign it to the array variable3. Store things in that array


Can an array contain elements of an object type?

Yes. An array of arrays is nothing more than a multi-dimensional array.


What is the different sorting algorithms for arrays in java?

The built in array sorting algorithm (java.util.Arrays.sort) depends on the type of data being sorted. Primitive types are sorted with a modified implementation of quicksort. Objects are sorted with a modified implementation of mergesort.