Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Comparator;
- 2 package bubblesort{
- 3 class Sort{
- 4 public final static int ASCENDING = 0;
- 5 public final static int DESCENDING = 1;
- 6
- 7 public static <T> T[] SortThis(Comparator comparator, T[] unSortedArray, int ordering ){
- 8 boolean sorted = false;
- 9 while(!sorted){
- 10 sorted = true;
- 11 for(int i = 0; i < unSortedArray.length -1; i++){
- 12 if(ordering == DESCENDING){
- 13 if(comparator.compare(unSortedArray[i], unSortedArray[i+1]) > 0){
- 14 T temp = unSortedArray[i];
- 15 unSortedArray[i] = unSortedArray[i+1];
- 16 unSortedArray[i+1] = temp;
- 17 sorted = false;
- 18 }
- 19 }
- 20 else{
- 21 if(comparator.compare(unSortedArray[i], unSortedArray[i+1]) < 0){
- 22 T temp = unSortedArray[i];
- 23 unSortedArray[i] = unSortedArray[i+1];
- 24 unSortedArray[i+1] = temp;
- 25 sorted = false;
- 26 }
- 27
- 28 }
- 29 }
- 30 }
- 31 return unSortedArray;
- 32 }
- 33
- 34 }
- 35 }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement