mmayoub

lab, SortingNamesWithTime

Jul 5th, 2018
101
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. public class SortingNamesWithTime {
  2.     static long aviTime;
  3.     static long usifTime;
  4.  
  5.     public static void aviSort(String[] names) {
  6.         long startTime = Utils.getCurrentTime();
  7.  
  8.         int n = names.length; // number of names in the array
  9.  
  10.         for (int i = n - 1; i > 0; i--) {
  11.             // find and position item at index i - will not be moved from this
  12.             // index
  13.  
  14.             for (int j = 0; j < i; j++) {
  15.                 if (names[j].compareTo(names[j + 1]) > 0) {
  16.                     // if not correctly ordered then swap names
  17.                     String temp = names[j];
  18.                     names[j] = names[j + 1];
  19.                     names[j + 1] = temp;
  20.                 }
  21.             }
  22.  
  23.             // now starting from position i the array is finally sorted
  24.         }
  25.  
  26.         long endTime = Utils.getCurrentTime();
  27.         aviTime = endTime - startTime;
  28.     }
  29.  
  30.     public static void usifSort(String[] names) {
  31.         long startTime = Utils.getCurrentTime();
  32.  
  33.         int n = names.length; // number of names in the array
  34.  
  35.         int i = 0; // start sorting from the beginning of the array
  36.  
  37.         while (i < n - 1) { // if not all sorted
  38.             if (names[i].compareTo(names[i + 1]) > 0) {
  39.                 // if not correctly ordered then swap names
  40.                 String temp = names[i];
  41.                 names[i] = names[i + 1];
  42.                 names[i + 1] = temp;
  43.                 i = 0; // start sorting from the beginning of the array
  44.             } else {
  45.                 // now all items until position i in the array is sorted
  46.  
  47.                 i = i + 1; // move to the next position
  48.             }
  49.         }
  50.  
  51.         long endTime = Utils.getCurrentTime();
  52.         usifTime = endTime - startTime;
  53.     }
  54.  
  55.     public static void main(String[] args) {
  56.         // number of students
  57.         int n = 90;
  58.         // get random names
  59.         String[] arr1 = Utils.getRandomNames(n);
  60.         // make a copy of the names
  61.         String[] arr2 = Utils.getNamesCopy(arr1);
  62.  
  63.         // print source data
  64.         // arr1 has the same data in the same order as arr2
  65.         Utils.printArray(arr1, "\ndata before sorting - unsorted");
  66.  
  67.         // Avi sorting the data array - arr1
  68.         aviSort(arr1);
  69.         // Usif sorting the data array - arr2
  70.         usifSort(arr2);
  71.  
  72.         // print the two sorted arrays
  73.         Utils.printArray(arr1, arr2, "\ndata after sorting - Sorted",
  74.                 "Avi array", "Usif array");
  75.  
  76.         System.out.printf("\nAvi  sorted %d names in %d ms\n", n, aviTime);
  77.         System.out.printf("Usif sorted %d names in %d ms\n", n, usifTime);
  78.  
  79.     }
  80. }
RAW Paste Data