Advertisement
Guest User

Untitled

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