简体   繁体   中英

Java swap and comparisons in Bubble sort

public class SortingArray
{
   public static void main(String[] args)
   {
      int[] array1 = { 1, 2, 3, 4, 5};
      bubbleSort(array1);
   }

   public static void bubbleSort(int[] array2)
   {      
      // Display the array's contents.
      System.out.println("Original order: ");
      for (int element : array2)
         System.out.print(element + " ");

      int lastPos;     // Position of last element to compare
      int index;       // Index of an element to compare
      int temp;        // Used to swap to elements
        int count = 0;
      int count2 = 1;

      // The outer loop positions lastPos at the last element
      // to compare during each pass through the array. Initially
      // lastPos is the index of the last element in the array.
      // During each iteration, it is decreased by one.
      for (lastPos = array2.length - 1; lastPos >= 0; lastPos--)
      {
         // The inner loop steps through the array, comparing
         // each element with its neighbor. All of the elements
         // from index 0 thrugh lastPos are involved in the
         // comparison. If two elements are out of order, they
         // are swapped.
         for (index = 0; index <= lastPos - 1; index++)
         {
            count2++;
            // Compare an element with its neighbor.
            if (array2[index] > array2[index + 1])
            {
                    count++;
               // Swap the two elements.
               temp = array2[index];
               array2[index] = array2[index + 1];
               array2[index + 1] = temp;
            }
         }
      }
      count2++;

      // Display the array's contents.
      System.out.println("\nSorted order: ");
      for (int element : array2)
         System.out.print(element + " ");

      System.out.print("\n Swaps:" + count);
      System.out.print("\n Comparisons:" + count2);


   }

}

I am trying to count the comparisons and swaps in a simple bubble sort program, and I know I have the right number of swaps counted (0 in this case), but I cannot figure out how to to keep track of the comparisons. I have to use this specific code with only slight modifications. Could someone help me with understand how many comparisons will be made with this hard coded int array. Right now when I run the program I get 12 and I think that is right but I am not 100 percent positive.

Edit: The original code does correctly implement bubble sort.

So, keep your original code, just use the below code as a reference to get the comparison count correct.

Anyway, just for reference, here is another common implementation, with correct swap and comparison counts:

public class SortingArray
{
   public static void main(String[] args)
   {
      int[] array1 = { 5, 4, 3, 2, 1};
      bubbleSort(array1);
   }

   public static void bubbleSort(int[] array2)
   {      
      // Display the array's contents.
      System.out.println("Original order: ");
      for (int element : array2)
         System.out.print(element + " ");

      //int lastPos;     // Position of last element to compare
      int index;       // Index of an element to compare
      int temp;        // Used to swap to elements
      int count = 0;
      int count2 = 0;
      boolean swapped = true;


      while (swapped == true)
      {
         swapped = false;
         // The inner loop steps through the array, comparing
         // each element with its neighbor. All of the elements
         // from index 0 thrugh lastPos are involved in the
         // comparison. If two elements are out of order, they
         // are swapped.
         for (index = 0; index < array2.length - 1; index++)
         {
            count2++;
            // Compare an element with its neighbor.
            if (array2[index] > array2[index + 1])
            {
               count++;
               // Swap the two elements.
               temp = array2[index];
               array2[index] = array2[index + 1];
               array2[index + 1] = temp;
               swapped = true;
            }

         }
      }
      //count2++;

      // Display the array's contents.
      System.out.println("\nSorted order: ");
      for (int element : array2)
         System.out.print(element + " ");

      System.out.print("\n Swaps:" + count);
      System.out.print("\n Comparisons:" + count2);


   }

}

output:

Original order: 
5 4 3 2 1 
Sorted order: 
1 2 3 4 5 
 Swaps:10
 Comparisons:20

For your sorted example:

int[] array1 = { 1, 2, 3, 4, 5};

output:

Original order: 
1 2 3 4 5 
Sorted order: 
1 2 3 4 5 
 Swaps:0
 Comparisons:4

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM