java - SelectionSort and BubbleSort - how to count number of comparisons and number swapping? -


First of all, I have seen a similar question related to C ++, but I did not quite understand it - plus my question Java is about.

Actually I have coded two methods that can use selection and bubble on the array. I believe that the methods work properly (I have run the tests and they have sorted all the numbers in ascending order), I am not sure that I am counting the number and number swap correctly. If someone can test my code below and give some feedback, then I would be very grateful.

Note: I can zip my Java project files and send them to someone.

BubbleSort Method:

  public string bubble sort (int [] number) {System.out.println ("***** * | Bubble Sort * * ***** "); StringBuilder Root = New StringBuilder (); For (int i = 0; i & lt; = numbers.length - 1; i ++) {Original Array .append (number [i] + ""); } System.out.println ("Basic Array:" + Original Array); Int temporary; // Temporary Variable // Set Boolean Variable true, // To allow the first pass. Boolean Pass = True; Int compare = 0; Int swap = 0; // When a pass can be made, while (pass) {// Set the Boolean value to false, // a number swap indicator / pass = false; For (int i = 0; i & lt; numbers.length - 1; i ++) {// 1. The comparison of comparing comparisons increases; ++; If (number [i] & gt; number [i + 1]) {temp = numbers [i]; Number [i] = number [i + 1]; Number [i + 1] = temporary; // Increase the amount of swap made by 1, // to put the numbers in the correct order. Swap ++; Pass = true; }}} // Create a stringbuilder object - / / to keep output of the serial numbers. Stringbilder sb = new stringbiller (); // Now for each subsequent number in the array for the loop - / / // StringBuilder object through the sorted array (int i = 0; i & lt; numbers lamp; i ++) {sb.append ( Number [i] + ""); } // Return the last result of the sorted array Return "Sorted array (acc):" + sb.toString () + "\ nComparation created:" + Comparison + "\ nSwapped:" + Swap; }  SelectionSort Method   
  public string selection source (int [] number) {System.out.println ("* * **** | Sort selection ****** "); StringBuilder Root = New StringBuilder (); Int compare = 0; Int swap = 0; For (int i = 0; i & lt; = numbers.length - 1; i ++) {Original Array .append (number [i] + ""); } System.out.println ("Basic Array:" + Original Array); // declare variables to keep first element int first; // // To declare the temporary variable, // swapping integers to be used in floating floating; For (int x = numbers.length - 1; x & gt; 0; x--) {first = 0; Compare ++; For (int y = 1; y & lt; = x; y ++) {// Comparison ++; If (numbers [y] & gt; numbers [first]) {first = y; // compare ++; Swap ++; } Temporary = number [first]; Numbers [first] = numbers [x]; Number [x] = temporary; // swap ++; }} // Create a stringbuilder object - / / Output of sorted numbers. Stringbilder sb = new stringbiller (); // Now for each subsequent number in the array for the loop - / / // StringBuilder object through the sorted array (int i = 0; i & lt; numbers lamp; i ++) {sb.append ( Number [i] + ""); } // Return the last result of the sorted array Return "Sorted array (acc):" + sb.toString () + "\ nComparation created:" + Comparison + "\ nSwapped:" + Swap; }  

- & gt; (n * (n-1)) / 2

item assignment (swap) - & gt; 3 * (N-1) for selection:

To sort selection:

Key Comparison - & gt; (n * (n-

item function (swap) -> (n * (n-1)) / 4

(Note that n is the number of your array size)

Comments

Popular posts from this blog

excel vba - How to delete Solver(SOLVER.XLAM) code -

github - Teamcity & Git - PR merge builds - anyway to get HEAD commit hash? -

ios - Replace text in UITextView run slowly -