Advertisement
Guest User

Untitled

a guest
Apr 25th, 2015
221
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 9.50 KB | None | 0 0
  1. package comparesorting;
  2. import java.util.*;
  3.  
  4. /**
  5.  *
  6.  * @author Alex
  7.  */
  8. public class CompareSorting {
  9.  
  10.     public static void main(String[] args) {
  11.        
  12.          for (int k = 1; k<= 100; k++)
  13.         {
  14.         int size = 1000000;     // change this number to change the size of the random array
  15.         int[] a = new int[size];
  16.         int[] temp = new int[a.length];  // empty temporary array, the same size and type as a[]
  17.  
  18.         // fill the array with random integers
  19.         for (int i = 0; i< a.length; i++)
  20.             a[i] = (int)(Math.random()*100000 +1);
  21.        
  22.         // write the array to a data file
  23.         // WARNING the text file will be 5.7 MB for 1 million items
  24.         //writeLines(a, "before.txt");
  25.        
  26.         // get the start time in nanoseconds
  27.         long startTime = System.nanoTime();
  28.  
  29.         //call mergesort to sort the entire array
  30.         mergeSort(a, temp, 0, (a.length - 1));
  31.        
  32.         quickSort(a, 0, a.length - 1);
  33.        
  34.         bubbleSort(a);
  35.        
  36.         selectionSort(a);
  37.        
  38.         System.out.println(a);
  39.  
  40.         // get the end time in nanoseconds
  41.         long endTime = System.nanoTime();
  42.  
  43.         // calculate elapsed time in nanoseconds
  44.         long duration = endTime - startTime;
  45.  
  46.         // print the elapsed time in seconds   (nanaoseconds/ 1 billion)
  47.       //  System.out.printf("%12.8f %n", (double)duration/100000000) ;
  48.        
  49.         // write the sorted array to a data file
  50.         // WARNING the file will be 5.7 MB for 1 million items
  51.         //writeLines(a, "after.txt");
  52.        
  53.        
  54.         }
  55.          
  56.          
  57.      
  58.     }
  59.    
  60.     // a method to print the elements of an integer array on one line
  61.     public static void printtArray(int[] a) {
  62.  
  63.         // iterate and print the array on one line
  64.         for (int i = 0; i < a.length; i++) {
  65.             System.out.print(a[i] + " ");
  66.         }
  67.  
  68.         System.out.println();
  69.  
  70.     } // end printArray()
  71.     //************************************************************************
  72.  
  73.     // the recursive quicksort method, which calls the partition method
  74.     public static void quickSort(int[] a, int startIndex, int endIndex) {
  75.         int pivotIndex;      // the index of pivot returned by the quicksort partition
  76.  
  77.         // if the set has more than one element, then partition
  78.         if (startIndex < endIndex) {
  79.             // partition and return the pivotIndex
  80.             pivotIndex = partition(a, startIndex, endIndex);
  81.             // quicksort the low set
  82.             quickSort(a, startIndex, pivotIndex - 1);
  83.             // quiclsort the high set
  84.             quickSort(a, pivotIndex + 1, endIndex);
  85.         } // end if
  86.     } // end quickSort()
  87.     //************************************************************************
  88.  
  89.     // This method performs quicksort partitioning on a set of elements in an array.
  90.     public static int partition(int[] a, int startIndex, int endIndex) {
  91.  
  92.         int pivotIndex;             // the index of the chosen pivot element
  93.         int pivot;                  // the value of the chosen pivot
  94.         int midIndex = startIndex;  // boundary element between high and low sets
  95.  
  96.         // select the center element in the set as the pivot by integer averaging
  97.         pivotIndex = (startIndex + endIndex) / 2;
  98.         pivot = a[pivotIndex];
  99.  
  100.         // put the pivot at the end of the set so it is out of the way
  101.         swap(a, pivotIndex, endIndex);
  102.  
  103.         // iterate the set, up to but not including last element
  104.         for (int i = startIndex; i < endIndex; i++) {
  105.             // if a[i] is less than the pivot
  106.             if (a[i] < pivot) {
  107.  
  108.                 // put a[i] in the low half and increment current Index
  109.                 swap(a, i, midIndex);
  110.                 midIndex = midIndex + 1;
  111.             } // end if
  112.         } // end for
  113.        
  114.         // partitioning complete -- move pivot from end to middle
  115.         swap(a, midIndex, endIndex);
  116.  
  117.         // return index of pivot
  118.         return midIndex;
  119.        
  120.     } // end partition
  121.     //************************************************************************
  122.  
  123.     // This method swaps two elements in an integer array
  124.     public static void swap(int[] a, int first, int second) {
  125.        
  126.         int c;  // a catalyst variable used for the swap
  127.  
  128.         c = a[first];
  129.         a[first] = a[second];
  130.         a[second] = c;
  131.  
  132.     } // end Swap()
  133.     //************************************************************************
  134.    
  135.     // *** MERGE SORT **************** /
  136.    
  137.      public static void mergeSort(int[] a, int[] temp, int low, int high) {
  138.         //  low is the low index of the part of the array to be sorted
  139.         //  high is the high index of the part of the array to be sorted
  140.        
  141.         int mid;  // the middle of the array – last item in low half
  142.        
  143.         // if high > low then there is more than one item in the list to be sorted
  144.         if (high > low) {
  145.  
  146.             // split into two halves and mergeSort each part
  147.  
  148.             // find middle (last element in low half)  
  149.             mid = (low+high)/2;
  150.             mergeSort(a, temp, low, mid );
  151.             mergeSort(a, temp, mid+1, high);
  152.            
  153.             // merge the two halves back together, sorting while merging
  154.             merge(a, temp, low, mid, high);
  155.         } // end if
  156.  
  157.         return;
  158.     }// end mergerSort()
  159.     //********************************************************************
  160.    
  161.    
  162.     /* This method merges the two halves of the set being sorted back together.
  163.      * the low half goes from a[low] to a[mid]
  164.      * the high half goes from a[mid+1] to a[high]
  165.      * (High and low only refer to index numbers, not the values in the array.)
  166.      *
  167.      * The work of sorting occurs as the two halves are merged back into one
  168.      * sorted set.
  169.      *
  170.      * This version of mergesort copies the set to be sorted into the same
  171.      * locations in a temporary array, then sorts them as it puts them back.
  172.      * Some versions of mergesort sort into the temporary array then copy it back.
  173.      */
  174.     public static void merge(int[] a, int[] temp, int low, int mid, int high) {
  175.         //  low is the low index of the part of the array to be sorted
  176.         //  high is the high index of the part of the array to be sorted
  177.         //  mid is the middle of the array – last item in low half
  178.        
  179.         // copy the two sets from a[] to the same locations in the temporary array
  180.         for (int i = low; i <= high; i++) {
  181.             temp[i] = a[i];
  182.         }
  183.  
  184.         //set up necessary pointers
  185.         int lowP = low;         // pointer to current item in low half
  186.         int highP = mid + 1;    // pointer to current item in high half
  187.         int aP = low;           // pointer to where each item will be put back in a[]
  188.  
  189.         // while the pointers have not yet reached the end of either half
  190.         while ((lowP <= mid) && (highP <= high)) {
  191.  
  192.             // if current item in low half <= current item in high half
  193.             if (temp[lowP] <= temp[highP]) {
  194.                 // move item at lowP back to array a and increment low pointer
  195.                 a[aP] = temp[lowP];
  196.                 lowP++;
  197.             } else {
  198.                 // move item at highP back to array a and increment high pointer
  199.                 a[aP] = temp[highP];
  200.                 highP++;
  201.             } // end if..else
  202.            
  203.             // increment pointer for location in original array
  204.             aP++;
  205.         } // end while
  206.  
  207.         /* When the while loop is done, either the low half or the high half is done.
  208.          * We now simply move back everything in the half not yet done.
  209.          * Remember, each half is already in order itself.
  210.          */
  211.        
  212.         // if lowP has reached end of low half, then low half is done, move rest of high half
  213.         if (lowP > mid)
  214.             for (int i = highP; i <= high; i++) {
  215.                 a[aP] = temp[i];
  216.                 aP++;
  217.             } // end for
  218.         else // high half is done, move rest of low half
  219.        
  220.             for (int i = lowP; i <= mid; i++) {
  221.                 a[aP] = temp[i];
  222.                 aP++;
  223.             }// end for
  224.        
  225.         return;
  226.     } // end merge()
  227.     // *************************************************************
  228.    
  229.     public static void displayArray(int[] a) {
  230.  
  231.         for (int i = 0; i < a.length; i++)
  232.             System.out.print(a[i] + " ");
  233.         System.out.println();
  234.        
  235.     } // end displayArray()
  236.    
  237.    
  238.     /* --------- bubble sort --------- */
  239.    
  240.      public static int[] bubbleSort(int[] arr){
  241.         int temp;
  242.         for(int i=0; i < arr.length-1; i++){
  243.  
  244.             for(int j=1; j < arr.length-i; j++){
  245.                 if(arr[j-1] > arr[j]){
  246.                     temp=arr[j-1];
  247.                     arr[j-1] = arr[j];
  248.                     arr[j] = temp;
  249.                 }
  250.             }
  251.            
  252.         }
  253.         return arr;
  254.     }
  255.      
  256.      
  257.      public static void selectionSort(int[] arr) {
  258.       int i, j, minIndex, tmp;
  259.       int n = arr.length;
  260.       for (i = 0; i < n - 1; i++) {
  261.             minIndex = i;
  262.             for (j = i + 1; j < n; j++)
  263.                   if (arr[j] < arr[minIndex])
  264.                         minIndex = j;
  265.             if (minIndex != i) {
  266.                   tmp = arr[i];
  267.                   arr[i] = arr[minIndex];
  268.                   arr[minIndex] = tmp;
  269.             }
  270.       }
  271. }
  272.  
  273.    
  274. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement