Advertisement
mmayoub

lab, SortingNamesWithCounter

Jul 5th, 2018
228
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.73 KB | None | 0 0
  1. public class SortingNamesWithCounter {
  2.     static long aviWork;
  3.     static long usifWork;
  4.  
  5.     public static void aviSort(String[] names) {
  6.         int counter = 0;
  7.  
  8.         int n = names.length; // number of names in the array
  9.         counter += 2; // get array length and saving to variable
  10.  
  11.         int i;
  12.         for (i = n - 1, counter += 2; i > 0; i--, counter++) {
  13.             // find and position item at index i - will not be moved from this
  14.             // index
  15.  
  16.             counter += 1; // check logical condition
  17.  
  18.             int j;
  19.             for (j = 0, counter++; j < i; j++, counter++) {
  20.                 counter++; // check for logical condition
  21.                 if (names[j].compareTo(names[j + 1]) > 0) {
  22.                     // if not correctly ordered then swap names
  23.                     String temp = names[j];
  24.                     counter++;
  25.                     names[j] = names[j + 1];
  26.                     counter++;
  27.                     names[j + 1] = temp;
  28.                     counter++;
  29.                 }
  30.                 counter += 1; // check if condition
  31.                 counter += 1; // update j
  32.             }
  33.             counter += 1; // update i
  34.             // now starting from position i the array is finally sorted
  35.         }
  36.  
  37.         aviWork = counter;
  38.     }
  39.  
  40.     public static void usifSort(String[] names) {
  41.         int counter = 0;
  42.  
  43.         int n = names.length; // number of names in the array
  44.         counter += 2; // get array length and saving to variable
  45.  
  46.         int i = 0; // start sorting from the beginning of the array
  47.         counter++;
  48.  
  49.         while (i < n - 1) { // if not all sorted
  50.             counter++; // check while logical condition
  51.             if (names[i].compareTo(names[i + 1]) > 0) {
  52.                 // if not correctly ordered then swap names
  53.                 String temp = names[i];
  54.                 counter++;
  55.                 names[i] = names[i + 1];
  56.                 counter++;
  57.                 names[i + 1] = temp;
  58.                 counter++;
  59.                 i = 0;
  60.                 counter++;// start sorting from the beginning of the array
  61.             } else {
  62.                 // now all items until position i in the array is sorted
  63.  
  64.                 i = i + 1;
  65.                 counter++;// move to the next position
  66.             }
  67.  
  68.             counter++; // check if logical condition
  69.         }
  70.  
  71.         counter++; // check while logical condition
  72.  
  73.         usifWork = counter;
  74.     }
  75.  
  76.     public static void main(String[] args) {
  77.         // number of students
  78.         int n = 24;
  79.         // get random names
  80.         String[] arr1 = Utils.getRandomNames(n);
  81.         // make a copy of the names
  82.         String[] arr2 = Utils.getNamesCopy(arr1);
  83.  
  84.         // print source data
  85.         // arr1 has the same data in the same order as arr2
  86.         Utils.printArray(arr1, "\ndata before sorting - unsorted");
  87.  
  88.         // Avi sorting the data array - arr1
  89.         aviSort(arr1);
  90.         // Usif sorting the data array - arr2
  91.         usifSort(arr2);
  92.  
  93.         // print the two sorted arrays
  94.         Utils.printArray(arr1, arr2, "\ndata after sorting - Sorted",
  95.                 "Avi array", "Usif array");
  96.  
  97.         System.out.printf("\nAvi  sorted %d names in %d basic operations\n", n,
  98.                 aviWork);
  99.         System.out.printf("Usif sorted %d names in %d basic operations\n", n,
  100.                 usifWork);
  101.     }
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement