Link to home
Start Free TrialLog in
Avatar of 15jen
15jen

asked on

How do I make a method to count swaps in my code and then display them?

Here are the swaps++>>>>>>>>>>>
public int insertionsort(int elements) {
                  int inside, outside, swaps = 0;
                  for (outside = 1; outside < elements; outside++) {
                        int temp = randomnumbers[outside];
                        inside = outside;
                        while ((inside > 0) && (randomnumbers[inside - 1] >= temp)) {
                              randomnumbers[inside] = randomnumbers[inside - 1];
                              --inside;
                              swaps++;
                        }
                        randomnumbers[inside] = temp;
                  }
                  return swaps;
SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
15jen, i'm interested to know why you accepted that answer - why would you want a hard-coded println in a sort method?