Advertisement
Guest User

Untitled

a guest
Oct 17th, 2017
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.55 KB | None | 0 0
  1. package sort;
  2.  
  3. import java.util.Arrays;
  4. import java.util.Comparator;
  5. import java.util.LinkedList;
  6. import java.util.List;
  7. import java.util.Random;
  8.  
  9. public class BubbleSort_Solution {
  10.  
  11.     /**
  12.      * swaps two adjacent members of the array. Caller to ensure array bounds.
  13.      *
  14.      * $Id: BubbleSortGeneric.java 934 2017-10-04 08:01:07Z ag0015 $
  15.      *
  16.      * @author Andre Gruning for COM2031 2015-2017 -- Beer Licence.
  17.      *
  18.      * @param <T>
  19.      *            type of elements in the array
  20.      * @param L
  21.      *            array
  22.      * @param index
  23.      *            elements at index and index+1 are swapped.
  24.      */
  25.  
  26.     private static <T> void swapNeighbours(final T[] L, final int index) {
  27.         T temp = L[index];
  28.         L[index] = L[index + 1];
  29.         L[index + 1] = temp;
  30.     }
  31.  
  32.     /**
  33.      * implements bubbleSort -- brute force sorting by swapping adjacent
  34.      * elements if they are not in the right order. Maximal number of swaps
  35.      * performed is proportional to n^2 if n is the length of L, ie bubbleSort
  36.      * is O(n^2).
  37.      *
  38.      * @param <T>
  39.      *            type of elements to sort
  40.      *
  41.      * @param L
  42.      *            list to sort
  43.      * @return number of swaps performed
  44.      */
  45.  
  46.     public static <T> int bubbleSort(final T[] L, final Comparator<T> c) {
  47.  
  48.         int count = 0;
  49.  
  50.         for (int j = L.length - 1; j > 0; j--) {
  51.             for (int i = 0; i < j; i++) {
  52.                 if (c.compare(L[i], L[i + 1]) < 0) {
  53.                     swapNeighbours(L, i);
  54.                     count++;
  55.                 }
  56.             }
  57.         }
  58.         return count;
  59.     }
  60.  
  61.     /**
  62.      * a helper method that creates a random permutation of number from 0 to
  63.      * n-1.
  64.      *
  65.      * @param n
  66.      *            length of permutation
  67.      * @return a random permutation of numbers from 0 to n-1
  68.      */
  69.     private static Long[] randomPermutation(final int n) {
  70.  
  71.         // space to store the permutation
  72.         Long[] v = new Long[n];
  73.  
  74.         // a random generator
  75.         Random r = new Random();
  76.  
  77.         // helper to create the permutation
  78.         List<Long> u = new LinkedList<Long>();
  79.  
  80.         // file u with numbers 0...n-1
  81.         for (long i = 0; i < v.length; i++) {
  82.             u.add(i);
  83.         }
  84.  
  85.         // select an element from u randomly and move to v until u has no more
  86.         // elements.
  87.         int vIndex = 0;
  88.         while (u.size() > 0) {
  89.             int randomIndex = r.nextInt(u.size());
  90.             v[vIndex++] = u.get(randomIndex);
  91.             u.remove(randomIndex);
  92.         }
  93.         // v contains now a random permutation of numbers 0...n-1
  94.         return v;
  95.     }
  96.  
  97.     /**
  98.      * For testing purposes: generate some random data to sort and check against
  99.      * Java's own library function whether our implementation yields the same
  100.      * results. (Assuming that the Java library is well debugged.)
  101.      *
  102.      * @param args
  103.      *            command line arguments -- unused
  104.      */
  105.     public static void main(String[] args) {
  106.  
  107.         // sample data to sort to contain a permutation of numbers 0...999
  108.         Long v[] = randomPermutation(1000);
  109.  
  110.         // just a (deep) copy of v
  111.         Long[] w = Arrays.copyOf(v, v.length);
  112.  
  113.         // count the number of errors our algorithm makes:
  114.         int error_count = 0;
  115.  
  116.         // how does a Long compare to another Long?
  117.         Comparator<Long> comp = new Comparator<Long>() {
  118.  
  119.             @Override
  120.             public int compare(Long arg0, Long arg1) {
  121.  
  122.                 if (arg0 > arg1)
  123.                     return -1;
  124.                 else if (arg1 > arg0)
  125.                     return 1;
  126.                 return 0;
  127.             }
  128.  
  129.         };
  130.  
  131.         // sort v and find out how many swaps done
  132.         int swapsBubble = bubbleSort(v, comp);
  133.         System.out.println("Bubble swaps: " + swapsBubble);
  134.  
  135.         // test against Java library whether our implementation of bubblesort
  136.         // works as expected.
  137.         Arrays.sort(w);
  138.         if (!Arrays.deepEquals(v, w)) {
  139.             error_count++;
  140.         }
  141.         System.out.println("Errors: " + error_count);
  142.     }
  143. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement