public static void main (String [] args) {
int n;
Scanner sc = new Scanner (System.in);
System.out.print ("Enter how many elements");
n = sc.nextInt();
int [] a = new int [n];
//input array
for (int i=0; i<n; i++){
System.out.print("Enter element " +(i+1) + " ");
a[i] = sc.nextInt();
}
//sorting
for (int i=0; i<n-1; i++) {
for (int j=i+1; j<n; i++) {
if(a[i]>a[j]){
int t = a[i];
a[i] = a[j];
a[j] = t;
}
}
}
//output
for (int i=0; i<n; i++){
System.out.println(a[i]);
}
}
A java blog with a collection of examples and tutorials on Java and related technologies. (Under maintenance with continuous updates) Be in touch with java jazzle or k2java.blogspot.com.
Sunday, May 1, 2011
Sorting an array using selection sort (java)
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment