We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

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

15jen
15jen asked
on
Medium Priority
415 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();
    }
       
  }
}

Comment
Watch Question

Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
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.


Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
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) {



Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
> 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

Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
CERTIFIED EXPERT
Top Expert 2016

Commented:
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
CERTIFIED EXPERT
Top Expert 2016
Commented:
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

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
CERTIFIED EXPERT
Top Expert 2016

Commented:
:-)
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.