Here you go;
public class sortArray {
public static void main(String[] args) {
int abc[] = {1,2,7,95,43,21,22,12,11};
int i;
bubbleSort(abc, abc.length); //Pass the array to be sorted and its
length.
System.out.println("After sorting, the list elements are: ");
for(i=0; i<abc.length; i++) {
System.out.print(abc[i] + " ");
}
System.out.println("\nPrint First Five sorted Elements are: "); // show first
five sorted elements
for(i=4; i>=0; i--) {
System.out.print(abc[i] + " ");// print in descending order
}
}
private static void bubbleSort(int[] abc, int length) {
int temp, counter, index;
for(counter=0; counter<length-1; counter++) { //Loop once for each
element in the array.
for(index=0; index<length-1-counter; index++) { //Once for each
element, minus the counter.
if(abc[index] > abc[index+1]) { //Test if need a swap or not.
temp = abc[index]; //swap the two elements.
abc[index] = abc[index+1];
abc[index+1] = temp;
}
}
}
}
}
--- On Mon, 7/27/09, Vu Viet Dung <[email protected]> wrote:
From: Vu Viet Dung <[email protected]>
Subject: [java programming] Re: output top 3 from 1 array.
To: [email protected], "Free Java Programming Online Training Course By Sang
Shin" <[email protected]>
Date: Monday, July 27, 2009, 9:46 AM
Sort that array in descending order, and output first 5 number.
-----Original Message-----
From: [email protected]
[mailto:[email protected]] On Behalf Of nguyen
thanh lam
Sent: Sunday, July 26, 2009 3:37 PM
To: Free Java Programming Online Training Course By Sang Shin
Subject: [java programming] output top 3 from 1 array.
int abc[]={1,2,7,95,43,21,22,12,11};
how to output top 5 number which are greater than others?
--~--~---------~--~----~------------~-------~--~----~
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/javaprogrammingwithpassion?hl=en
-~----------~----~----~----~------~----~------~--~---