Advertisement
Guest User

Untitled

a guest
Apr 8th, 2020
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.04 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6.  
  7. import java.util.Arrays;
  8. //export JVM_ARGS="-Xmx1024m -XX:MaxPermSize=256m";
  9.  
  10. public class ArrayMaker {
  11.  
  12. // Java implementation to print
  13. // the string in Lexicographical order
  14. // Used for index in heap
  15.     static int x = -1;
  16.  
  17. // Predefining the heap array
  18.     static String[] heap = new String[200000];
  19.     static String[] heapSorted = new String[0];
  20.  
  21. // Defining formation of the heap
  22.         static void heapForm(String k) {
  23.         x++;
  24.  
  25.         heap[x] = k;
  26.  
  27.         int child = x;
  28.  
  29.         String tmp;
  30.  
  31.         int index = x / 2;
  32.  
  33.         // Iterative heapiFy
  34.         while (index >= 0) {
  35.  
  36.             // Just swapping if the element
  37.             // is smaller than already
  38.             // stored element
  39.             if (heap[index].compareTo(heap[child]) > 0) {
  40.  
  41.                 // Swapping the current index
  42.                 // with its child
  43.                 tmp = heap[index];
  44.                 heap[index] = heap[child];
  45.                 heap[child] = tmp;
  46.                 child = index;
  47.  
  48.                 // Moving upward in the
  49.                 // heap
  50.                 index = index / 2;
  51.             } else {
  52.                 break;
  53.             }
  54.         }
  55.     }
  56.  
  57. // Defining heap sort
  58.     static void heapSort() {
  59.         int left1, right1;
  60.  
  61.         while (x >= 0) {
  62.             String k;
  63.             k = heap[0];
  64.  
  65.             // Taking output of
  66.             // the minimum element
  67.             heapSorted = Arrays.copyOf(heapSorted, heapSorted.length + 1);
  68.             heapSorted[heapSorted.length - 1] = k;
  69. //            System.out.print(k + " ");
  70.  
  71.             // Set first element
  72.             // as a last one
  73.             heap[0] = heap[x];
  74.  
  75.             // Decrement of the
  76.             // size of the string
  77.             x = x - 1;
  78.  
  79.             String tmp;
  80.  
  81.             int index = 0;
  82.  
  83.             int length = x;
  84.  
  85.             // Initilizing the left
  86.             // and right index
  87.             left1 = 1;
  88.  
  89.             right1 = left1 + 1;
  90.  
  91.             while (left1 <= length) {
  92.  
  93.                 // Process of heap sort
  94.                 // If root element is
  95.                 // minimum than its both
  96.                 // of the child then break
  97.                 if (heap[index].compareTo(heap[left1]) <= 0
  98.                         && heap[index].compareTo(heap[right1]) <= 0) {
  99.                     break;
  100.                 } // Otherwise checking that
  101.                 // the child which one is
  102.                 // smaller, swap them with
  103.                 // parent element
  104.                 else {
  105.  
  106.                     // Swapping
  107.                     if (heap[left1].compareTo(heap[right1]) < 0) {
  108.                         tmp = heap[index];
  109.                         heap[index] = heap[left1];
  110.                         heap[left1] = tmp;
  111.                         index = left1;
  112.                     } else {
  113.                         tmp = heap[index];
  114.                         heap[index] = heap[right1];
  115.                         heap[right1] = tmp;
  116.                         index = right1;
  117.                     }
  118.                 }
  119.  
  120.                 // Changing the left index
  121.                 // and right index
  122.                 left1 = 2 * left1;
  123.                 right1 = left1 + 1;
  124.             }
  125.         }
  126.     }
  127.  
  128. // Utility function
  129.     static void sort(String k[]) {
  130.  
  131.         // To heapiFy
  132.         for (int i = 0; i < 2000; i++) {
  133.             heapForm(k[i]);
  134.         }
  135.  
  136.         // Calling heap sort function
  137.         heapSort();
  138.     }
  139.  
  140.     static String getAlphaNumericString(int n) {
  141.  
  142.         // chose a Character random from this String
  143.         String AlphaNumericString = "abcdefghijklmnopqrstuvxyz";
  144.  
  145.         // create StringBuffer size of AlphaNumericString
  146.         StringBuilder sb = new StringBuilder(n);
  147.  
  148.         for (int i = 0; i < n; i++) {
  149.  
  150.             // generate a random number between
  151.             // 0 to AlphaNumericString variable length
  152.             int index
  153.                     = (int) (AlphaNumericString.length()
  154.                     * Math.random());
  155.  
  156.             // add Character one by one in end of sb
  157.             sb.append(AlphaNumericString
  158.                     .charAt(index));
  159.         }
  160.  
  161.         return sb.toString();
  162.     }
  163.  
  164.     public static void main(String[] args) {
  165. //        String[] arr = new String[200000];
  166. //        String arrays[][] = new String[25][200000];
  167. //        for(int i = 0; i < arrays.length; i++){
  168. //            for(int j = 0; j < arr.length; j++){
  169. //                arrays[i][j] = ArrayMaker.getAlphaNumericString(i);
  170. //                System.out.println(arrays[i][j]);
  171. //            }
  172. //            
  173. //        }
  174. //        for (int i = 0; i < arr.length; i++) {
  175. //            arr[i] = ArrayMaker.getAlphaNumericString(5);
  176. //            System.out.print(arr[i] + " ");
  177. //        }
  178. //        System.out.println("\nSorted:");
  179. //        long start = System.nanoTime();
  180. //        sort(arr);
  181. //        long end = System.nanoTime();
  182.        
  183.         System.out.println("Arrlen\tStrLen\tHeap\tRadix");
  184.         System.out.println("======\t======\t====\t=====");
  185.        
  186.         for (int i = 0; i < 25; i++) {
  187.             String arr2[] = new String[2000];
  188.             for (int j = 0; j < arr2.length; j++) {
  189.                 arr2[j] = ArrayMaker.getAlphaNumericString(j + 1);
  190.             }
  191.             long start = System.nanoTime();
  192.             sort(arr2);
  193.             long end = System.nanoTime();
  194.             long time = (end - start) / 1000000;
  195.             System.out.println(arr2.length + "\t" + arr2[i].length() + "\t" + time);
  196.  
  197.         }
  198.  
  199. //        for (int i = 0; i < heapSorted.length; i++) {
  200. //            System.out.print(heapSorted[i] + " ");
  201. //        }
  202. //        System.out.println("\nHeapSort time to complete: " + (end - start) / 1000000);
  203. //        System.out.println(ArrayMaker.getAlphaNumericString(2));
  204.     }
  205.  
  206. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement