Advertisement
Guest User

Untitled

a guest
Oct 26th, 2014
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.61 KB | None | 0 0
  1. public class Sorting {
  2.    
  3.     /*
  4.      *  TODO: Implement the compare method
  5.      */
  6.     public static int Compare(String s1, String s2) {
  7.       for(int i=0; i<s1.length(); i++)
  8.       {
  9.        char x = s1.charAt(i);
  10.        char y = s2.charAt(i);
  11.        int a= (int) x;
  12.        int b= (int) y;
  13.        
  14.        if (a == b)
  15.        {
  16.          return 1;
  17.        }
  18.            
  19.  
  20.       }
  21. return 0;
  22.     }
  23.    
  24.    
  25.     /*
  26.      *  TODO: Implement the linearSearch method
  27.      */
  28.  
  29.     /*
  30.      *  TODO: Implement the binarySearch method
  31.      */
  32.    
  33.     public static int bubbleSort(String[] input_array) {
  34.         // TODO implement this
  35.         return 0;
  36.     }
  37.  
  38.     public static int combSort(String[] input_array) {
  39.         // TODO implement this
  40.         return 0;
  41.     }
  42.  
  43.     public static void plotBubbleSortTest(int N_MAX) {
  44.         /*
  45.          * bubble_sort_results[N] = the number of comparisons made
  46.          * when sorting an array of size N.
  47.          */
  48.         int[] bubble_sort_results = new int[N_MAX];
  49.        
  50.         /*
  51.          * For each array size between 1 (the smallest array size)
  52.          * and N_MAX (an upper limit passed to this method), we will:
  53.          * 1) create a random test array
  54.          * 2) sort it, and store the number of comparisons in bubble_sort_results
  55.          * MAKE SURE THAT YOUR METHOD IS ACTUALLY SORTING THE TEST ARRAY!!!!!!
  56.          */
  57.         for (int i = 1; i < N_MAX; i++) {
  58.             String[] test_array = ArrayUtilities.getRandomNamesArray(i);
  59.             bubble_sort_results[i] = bubbleSort(test_array);
  60.         }
  61.        
  62.         // create a plot window
  63.         // The argument passed to  PlotWindow is the title of the window
  64.         PlotWindow pw = new PlotWindow("Bubble Sort!");
  65.        
  66.         // add a plot to the window using our results array
  67.         /*
  68.          *  The first argument for addPlot is a name for your data set
  69.          *  The second argument for addPlot is an array of integers,
  70.          *  In position "N" in the array, you should put the number of
  71.          *  comparisons that your algorithm made, when sorting an array
  72.          *  of size N. For example, bubble_sort_results[100] will contain
  73.          *  the number of comparisons made for sorting an array of 100 elements
  74.          */
  75.         pw.addPlot("BubbleSort", bubble_sort_results);
  76.     }
  77.    
  78.     public static void plotCombSortTest(int N_MAX) {
  79.         // the results array
  80.         int[] comb_sort_results = new int[N_MAX];
  81.        
  82.         // test sorting for arrays from size 1 to N_MAX
  83.         // MAKE SURE THAT YOUR METHOD IS ACTUALLY SORTING THE TEST ARRAY!!!!!!
  84.         for (int i = 1; i < N_MAX; i++) {
  85.             String[] test_array = ArrayUtilities.getRandomNamesArray(i);
  86.             comb_sort_results[i] = combSort(test_array);
  87.         }
  88.         // create a plot window
  89.         PlotWindow pw = new PlotWindow("Comb Sort!");
  90.         // add a plot to the window using our results array
  91.         pw.addPlot("CombSort", comb_sort_results);
  92.     }  
  93.    
  94.     /*
  95.      * TODO:  implement the plotCombBubble method
  96.      * This method should plot the results from both sorting methods
  97.      * in the same window.
  98.      * Hint: Create a single PlotWindow, but use the addPlot method twice,
  99.      * once for each sorting algorithm
  100.      */
  101.  
  102.     public static void main(String[] args) {
  103.  
  104.   String s1 = "Shye";
  105.   String s2 = "Maude";
  106.  System.out.print(Compare(s1,s2));
  107.         // uncomment the following lines when you want to test your sorting methods
  108.         //plotBubbleSortTest(100);
  109.         //plotCombSortTest(100);
  110.         //plotCombBubble(100);
  111.     }
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement