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.
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]);
}
}
}
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");
}
}
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 ");
}
}
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]);
}
}
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.
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.
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.
I would suggest you try something simple, like a bubble sort. It isn't very efficient for large arrays, but it works.
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.
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.
// 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);
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
Yes. An array of arrays is nothing more than a multi-dimensional 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(<array>) See related link.
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/
// the build in sorting functions in Java will sort pretty much any array // of Comparable objects or primitives Arrays.sort(someArray);
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.
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.
array example 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.
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.
// 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);
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
Yes. An array of arrays is nothing more than a multi-dimensional array.
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.