简体   繁体   中英

Trouble with creating a Selection Sorter counting swaps and comparisons

I am having trouble trying to figure out how many swaps and comparisons for an int array using a Selection Sort in Java. I am confused on where in the loops the swap and comparisons count. Any guidance would be greatly appreciated.

    public class IntSelectionSorter {

public static int count = 0;
public static int count2 = 0;

public static void selectionSort(int[] array)
{
    int startScan;
    int index;
    int minIndex;
    int minValue;

    for (startScan = 0; startScan < (array.length - 1); startScan++)
    {
        minIndex = startScan;
        minValue = array[startScan];

        for (index = startScan + 1; index < array.length; index++)
        {
            count2++;
            if (array[index] < minValue)
            {
                minValue = array[index];
                count++;
                minIndex = index;
            }
        }
        array[minIndex] = array[startScan];
        count++;
        array[startScan] = minValue;
        count++;
    }
}

}

count2 seems to be right. count should be changed as follows:

for (startScan = 0; startScan < (array.length - 1); startScan++)
{
    minIndex = startScan;
    minValue = array[startScan];

    for (index = startScan + 1; index < array.length; index++)
    {
        count2++;
        if (array[index] < minValue)
        {
            minValue = array[index];
            // count++; delete this
            minIndex = index;
        }
    }
    if (minIndex != startScan) {
        array[minIndex] = array[startScan];
        count++;
        array[startScan] = minValue;
    }
}

basically, increment count only when there's is a need to swap, ie when there's another number in the array after startScan which is smaller than the value at startScan.

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 Bubble Sort Swaps:" + IntBubbleSorter.count);
    System.out.print("\n Bubble Sort Comparisons:" + IntBubbleSorter.count2);

    System.out.println();

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

    IntSelectionSorter.selectionSort(values);

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

    System.out.println();

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

    System.out.println();

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

    IntInsertionSorter.insertionSort(values);

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

    System.out.println();

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

}

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