Advertisement
EDGEUS

ban?

Dec 18th, 2019
221
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.91 KB | None | 0 0
  1. // Java implementation of Introsort algorithm
  2.  
  3. import java.io.IOException;
  4.  
  5. public class IntroSort {
  6.  
  7.     // the actual data that has to be sorted
  8.     private int a[];
  9.  
  10.     // the number of elements in the data
  11.     private int n;
  12.  
  13.     // Constructor to initialize the size
  14.     // of the data
  15.     IntroSort(int n)
  16.     {
  17.         a = new int[n];
  18.         this.n = 0;
  19.     }
  20.  
  21.     // The utility function to insert the data
  22.     private void dataAppend(int temp)
  23.     {
  24.         a[n] = temp;
  25.         n++;
  26.     }
  27.  
  28.     // The utility function to swap two elements
  29.     private void swap(int i, int j)
  30.     {
  31.         int temp = a[i];
  32.         a[i] = a[j];
  33.         a[j] = temp;
  34.     }
  35.  
  36.     // To maxHeap a subtree rooted with node i which is
  37.     // an index in a[]. heapN is size of heap
  38.     private void maxHeap(int i, int heapN, int begin)
  39.     {
  40.         int temp = a[begin + i - 1];
  41.         int child;
  42.  
  43.         while (i <= heapN / 2) {
  44.             child = 2 * i;
  45.  
  46.             if (child < heapN
  47.                     && a[begin + child - 1] < a[begin + child])
  48.                 child++;
  49.  
  50.             if (temp >= a[begin + child - 1])
  51.                 break;
  52.  
  53.             a[begin + i - 1] = a[begin + child - 1];
  54.             i = child;
  55.         }
  56.         a[begin + i - 1] = temp;
  57.     }
  58.  
  59.     // Function to build the heap (rearranging the array)
  60.     private void heapify(int begin, int end, int heapN)
  61.     {
  62.         for (int i = (heapN) / 2; i >= 1; i--)
  63.             maxHeap(i, heapN, begin);
  64.     }
  65.  
  66.     // main function to do heapsort
  67.     private void heapSort(int begin, int end)
  68.     {
  69.         int heapN = end - begin;
  70.  
  71.         // Build heap (rearrange array)
  72.         this.heapify(begin, end, heapN);
  73.  
  74.         // One by one extract an element from heap
  75.         for (int i = heapN; i >= 1; i--) {
  76.  
  77.             // Move current root to end
  78.             swap(begin, begin + i);
  79.  
  80.             // call maxHeap() on the reduced heap
  81.             maxHeap(1, i, begin);
  82.         }
  83.     }
  84.  
  85.     // function that implements insertion sort
  86.     private void insertionSort(int left, int right)
  87.     {
  88.  
  89.         for (int i = left; i <= right; i++) {
  90.             int key = a[i];
  91.             int j = i;
  92.  
  93.             // Move elements of arr[0..i-1], that are
  94.             // greater than the key, to one position ahead
  95.             // of their current position
  96.             while (j > left && a[j - 1] > key) {
  97.                 a[j] = a[j - 1];
  98.                 j--;
  99.             }
  100.             a[j] = key;
  101.         }
  102.     }
  103.  
  104.     // Function for finding the median of the three elements
  105.     private int findPivot(int a1, int b1, int c1)
  106.     {
  107.         int max = Math.max(Math.max(a[a1], a[b1]), a[c1]);
  108.         int min = Math.min(Math.min(a[a1], a[b1]), a[c1]);
  109.         int median = max ^ min ^ a[a1] ^ a[b1] ^ a[c1];
  110.         if (median == a[a1])
  111.             return a1;
  112.         if (median == a[b1])
  113.             return b1;
  114.         return c1;
  115.     }
  116.  
  117.     // This function takes the last element as pivot, places
  118.     // the pivot element at its correct position in sorted
  119.     // array, and places all smaller (smaller than pivot)
  120.     // to the left of the pivot
  121.     // and greater elements to the right of the pivot
  122.     private int partition(int low, int high)
  123.     {
  124.  
  125.         // pivot
  126.         int pivot = a[high];
  127.  
  128.         // Index of smaller element
  129.         int i = (low - 1);
  130.         for (int j = low; j <= high - 1; j++) {
  131.  
  132.             // If the current element is smaller
  133.             // than or equal to the pivot
  134.             if (a[j] <= pivot) {
  135.  
  136.                 // increment index of smaller element
  137.                 i++;
  138.                 swap(i, j);
  139.             }
  140.         }
  141.         swap(i + 1, high);
  142.         return (i + 1);
  143.     }
  144.  
  145.     // The main function that implements Introsort
  146.     // low --> Starting index,
  147.     // high --> Ending index,
  148.     // depthLimit --> recursion level
  149.     private void sortDataUtil(int begin, int end, int depthLimit)
  150.     {
  151.         if (end - begin > 16) {
  152.             if (depthLimit == 0) {
  153.  
  154.                 // if the recursion limit is
  155.                 // occurred call heap sort
  156.                 this.heapSort(begin, end);
  157.                 return;
  158.             }
  159.  
  160.             depthLimit = depthLimit - 1;
  161.             int pivot = findPivot(begin,
  162.                     begin + ((end - begin) / 2) + 1,
  163.                     end);
  164.             swap(pivot, end);
  165.  
  166.             // p is partitioning index,
  167.             // arr[p] is now at right place
  168.             int p = partition(begin, end);
  169.  
  170.             // Separately sort elements before
  171.             // partition and after partition
  172.             sortDataUtil(begin, p - 1, depthLimit);
  173.             sortDataUtil(p + 1, end, depthLimit);
  174.         }
  175.  
  176.         else {
  177.             // if the data set is small,
  178.             // call insertion sort
  179.             insertionSort(begin, end);
  180.         }
  181.     }
  182.  
  183.     // A utility function to begin the
  184.     // Introsort module
  185.     private void sortData()
  186.     {
  187.  
  188.         // Initialise the depthLimit
  189.         // as 2*log(length(data))
  190.         int depthLimit
  191.                 = (int)(2 * Math.floor(Math.log(n) /
  192.                 Math.log(2)));
  193.  
  194.         this.sortDataUtil(0, n - 1, depthLimit);
  195.     }
  196.  
  197.     // A utility function to print the array data
  198.     private void printData()
  199.     {
  200.         for (int i = 0; i < n; i++)
  201.             System.out.print(a[i] + " ");
  202.     }
  203.  
  204.     // Driver code
  205.     public static void main(String args[]) throws IOException
  206.     {
  207.         int[] inp = { 2, 10, 24, 2, 10, 11, 27,
  208.                 4, 2, 4, 28, 16, 9, 8,
  209.                 28, 10, 13, 24, 22, 28,
  210.                 0, 13, 27, 13, 3, 23,
  211.                 18, 22, 8, 8 };
  212.  
  213.         int n = inp.length;
  214.         IntroSort introsort = new IntroSort(n);
  215.  
  216.         for (int i = 0; i < n; i++) {
  217.             introsort.dataAppend(inp[i]);
  218.         }
  219.  
  220.         introsort.sortData();
  221.         introsort.printData();
  222.     }
  223. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement