-1

In Java, how do I take Bubble Sort, Insertion Sort, Merge Sort, Shell Sort, and Quick Sort and make a counter to tell me how many times they preform a comparison or copy.

I have all of the sorters made I just don't know how to make a counter.

Any ideas?

4

1 回答 1

2

It's rather simple, unless I am much mistaken.

For X Sort:

private int copyCounter = 0;
private int comparisonCounter = 0;

public void XSort(){

     // Sorting logic start
     ....
     //Perform comparison operation
     comparisonCounter++;
     ....
     //Perform copy operation
     copyCounter++;
     ....
     //Sorting logic ends

     //Print the values of copyCounter here
}

Once your method completes execution you will have the required counts. Assuming you have all the sorts implemented as separate classes you will need to modify your sort method in individual classes as above.

于 2013-03-30T20:28:21.063 回答