# ascending order using array rush

• 01-03-2011, 02:17 AM
jca
ascending order using array rush
nid some help we use basic code like int arr []=new arr[5].... like that....

import java.io.*;
public class aw{
public static void main(String args[])throws IOException{

int[]array = new int[10];
int x,y=0;

for (x = 0; x < 10; x++){
System.out.print("Enter a number: ");

• 01-03-2011, 02:32 AM
user0
Hey - you have to be more clear when you post a question for anyone to be able to help you. From your post subject, I am assuming you are looking to sort an array in ascending order.

The Arrays class has a method sort() you might want to use in your case.

The code will look like:

Arrays.sort(arrays);

Hope that helps.

Best,
• 01-03-2011, 05:24 AM
JavaHater
Compile, run and debug the code as you wish. Understand what the program is doing and insert your own explanation as comments.
Code:

import java.io.*;
public class IntegerSort {
public static void main(String[] args) throws IOException{
int[] array = new int[10];
int x,y=0;
for (x = 0; x < 10; x++){
System.out.print("Enter a number: ");
}
int[] result = sort( array );
for(int i=0; i< result.length; i++){
System.out.println( result[i] );
}
}

public static int[] sort(int[] a) {
int tmp;
int smallest;
for (int i = 0; i < a.length; i++) {
smallest = i;
for (int j = i + 1; j < a.length; j++) {
// ascending order
if(  a[j] < a[smallest] ) {
smallest = j;
}
}
swap( a , i, smallest);
}
return a;
}
public static void swap( int[] array, int a, int b ){
int tmp;
tmp = array[a];
array[a] = array[b];
array[b] = tmp;
}
}