简体   繁体   中英

Java bubble sorter needs to count swaps and comparisons

I am fairly new to Java. I am creating sorting algorithms on an int array such that each method keeps a count of the number of swaps and comparisons they make. I am having trouble with it displaying the Original Order, Sorted Order, Swaps and Comparisons. Not sure what I have to do to fix it. Any help would be greatly appreciated.

public class IntBubbleSorter {

    public static void bubbleSort(int[] array)
    {
        int lastPos;
        int index;
        int temp;
        int count = 0;
        int count2 = 0;

        for (lastPos = array.length - 1; lastPos >= 0; lastPos--)
          {
            for (index = 0; index <= lastPos - 1; index++)
            {
                count2++;

                if (array[index] > array[index + 1])
                {
                    count++;
                    temp = array[index];
                    array[index] = array[index + 1];
                    array[index + 1] = temp;
                }
            }
          }
        System.out.print("\n Swaps:" + count);

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


public class SortingTest {

    public static void main(String[] args) 
    {
        int[] values = { 1,53,86,21,49,32,90,65,33,11,34,68,54,32,78,80,35,22,96,59,265,44324,123,3123,25435};

        System.out.println("Original Order: ");
        for (int element : values) 
            System.out.print(element + " ");

        IntBubbleSorter.bubbleSort(values);

        System.out.println("\nSorted order: ");
        for (int element : values) 
            System.out.print(element + " ");

        System.out.println();
    }
}

//This is what it is outputting

Original Order: 
1 53 86 21 49 32 90 65 33 11 34 68 54 32 78 80 35 22 96 59 265 44324 123 3123 25435 
 Swaps:80
 Comparisons:300
Sorted order: 
1 11 21 22 32 32 33 34 35 49 53 54 59 65 68 78 80 86 90 96 123 265 3123 25435 44324 

based on your comment you could make your counting variables static and put your output in the main method like so:

public class IntBubbleSorter {

    public static int count = 0;

    public static int count2 = 0;

    public static void bubbleSort(int[] array)
    {
        int lastPos;
        int index;
        int temp;
        count = 0;
        count2 = 0;

        for (lastPos = array.length - 1; lastPos >= 0; lastPos--)
          {
            for (index = 0; index <= lastPos - 1; index++)
            {
                count2++;

                if (array[index] > array[index + 1])
                {
                    count++;
                    temp = array[index];
                    array[index] = array[index + 1];
                    array[index + 1] = temp;
                }
            }
          }
    }
}


public class SortingTest {

    public static void main(String[] args) 
    {
        int[] values = { 1,53,86,21,49,32,90,65,33,11,34,68,54,32,78,80,35,22,96,59,265,44324,123,3123,25435};

        System.out.println("Original Order: ");
        for (int element : values) 
            System.out.print(element + " ");

        IntBubbleSorter.bubbleSort(values);

        System.out.println("\nSorted order: ");
        for (int element : values) 
            System.out.print(element + " ");

        System.out.println();

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

        System.out.print("\n Comparisons:" + IntBubbleSorter.count2);
    }
}

But I should mention that static should be used with care.

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