-1

So I have this assignment where I have to compare a bunch of sorting methods and count the number of comparisons and the number of copies each algorithm performs.I have already created my sorting methods but have never made a counter before. My question is how would I declare a counter and where would I put one in my code?

Sorry completely forgot, kinda overwhelmed right now. I'm using java.

I know I would put like counter++ before the swap or comparison I'm just a little lost on the syntax

4

2 回答 2

0

Keep your sorting algorithms in an independent class, kept separate from your main app. Your counters can be field variables of that class. That way, you can run your sort, and follow up by retrieving the metrics you gathered.

MySortAlgorithms sorter=new MySortAlgorithms();
ArrayList example=sorter.quickSort(ArrayList arr);
System.out.println("Swaps=" + sorter.getSwapCount());
于 2013-03-31T01:40:20.267 回答
0

Counter can be a simple static variable in your class . Isn't it? Just add to it everytime you want to swap.

Class ObjectToBeSorted{
   static int numberOfSwaps;

   public static increment swap(){
     numberOfSwaps++
   }

   public static clearSwaps (){

      numberOfSwaps = 0;
   }


}
于 2013-03-31T14:19:21.733 回答