Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

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

Posted on 2009-02-16
3
Medium Priority
?
262 Views
Last Modified: 2013-12-29
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;
0
Comment
Question by:15jen
  • 2
3 Comments
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 450 total points
ID: 23652661
>>return swaps;

You are already counting the swaps. So you only need to do something like
System.out.printf("The code performed %d swap(s)\n", insertionsort(5));

Open in new window

0
 
LVL 92

Accepted Solution

by:
objects earned 1050 total points
ID: 23653889
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;
                  }
                  System.out.println("Number of swaps = "+swaps);
                  return swaps;

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 23654618
15jen, i'm interested to know why you accepted that answer - why would you want a hard-coded println in a sort method?
0

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Here is a helpful source code for C++ Builder programmers that allows you to manage and manipulate HTML content from C++ code, while also handling HTML events like onclick, onmouseover, ... Some objects defined and used in this source include: …
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses
Course of the Month13 days, 15 hours left to enroll

581 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question