[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

How do I finish this Java code to make it count for comparisons/exchanges in the loop?

Posted on 2009-02-10
7
Medium Priority
?
400 Views
Last Modified: 2013-12-29
Here is my code:  I need to have it do insertion and selection sort as well and also i have to have it count the comparisons/exchanges of the selection sort loops.  Please Help.... I am pulling my hair out.  

import java.util.*;
import java.io.*;

public class Sorter<T extends Comparable<T>> {
 
    public enum SortType {
      BUBBLE,
      INSERTION,
      SELECTION
    }
    public void sort(T[] array, SortType st) {
      switch(st) {
          case BUBBLE:
            doBubble(array);
            break;
            // More....
      }
     }
 
    private void doBubble(T[] array) {
      T temp;
      for(int i = 0;i < array.length;i++) {
          for(int j = array.length - 1;j > i;j--) {      
            if (array[i].compareTo(array[j]) > 0) {
                temp = array[i];
                array[i] = array[j];
                array[j] = temp;
            }
          }
      }
    }
 
    public static void main(String[] args) {
      Scanner scan = new Scanner (System.in);
      System.out.println("CS501 Homework #1 - Jennifer Pelletier");
      System.out.println("Bubble Sort");
try{
    PrintWriter out = new PrintWriter(new FileWriter("random.txt"));
      final int LENGTH = 2000;
      Integer[] ints = new Integer[LENGTH];
      for(int i = 0;i < LENGTH;i++) {
          ints[i] = (int)(Math.random() * 2000);
      out.println("Random: " + ints[i] + " = " + "Result of Sort: " + i);
      }
      System.out.println(java.util.Arrays.toString(ints));
      new Sorter<Integer>().sort(ints, SortType.BUBBLE);
      System.out.println(java.util.Arrays.toString(ints));
}

    catch(Exception e){
    e.printStackTrace();
    }
       
  }
}

0
Comment
Question by:15jen
  • 4
  • 3
7 Comments
 
LVL 92

Expert Comment

by:objects
ID: 23607676
that appears an unusual approach, by using an interface you would simplify it and allow other sorts to be easily added.

As it is you need to add a new method to do your sort, and also add another case to that switch statement. Not very OO but should at least work.


0
 
LVL 92

Expert Comment

by:objects
ID: 23607687
having all the sorts in the one class is just going to get a little unwieldy.

    public void sort(T[] array, SortType st) {
      switch(st) {
          case BUBBLE:
            doBubble(array);
            break;
          case SELECTION:
            doSelection(array);
            break;
      }
     }
 
    private void doSelection(T[] array) {



0
 
LVL 92

Expert Comment

by:objects
ID: 23607701
> l and also i have to have it count the comparisons/exchanges of the selection sort loops.

that will have to be domne inside your selection sort method

0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 92

Expert Comment

by:objects
ID: 23607705
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 23608921
Jennifer - you need to integrate the other sorts. Please post the code you're going to use for, say, the selection sort and i can help you with that
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 1500 total points
ID: 23608938
As for counting the exchanges, all you need do is increment a counter, e.g.:
        if (array[i].compareTo(array[j]) > 0) {
            swaps++;
            temp = array[i];
            array[i] = array[j];
            array[j] = temp;
        }

Open in new window

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 23620104
:-)
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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 first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses
Course of the Month18 days, 6 hours left to enroll

830 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