Quantcast
Viewing all articles
Browse latest Browse all 54

What can I do to make this generic sorting method work?

Hello. This question is kind of long... so bear with me please.

I have to convert a SelectionSort method that was covered in my book that was built to sort arrays, and make it generic so that I can enter either doubles or ints into it and have it work.

It doesn't seem to allow generic arrays, so I'm attempting to use an ArrayList. The problem here is since the int and doubles are now in Integer and Double wrappers, it breaks the SelectionSort method.

I've attempted to fix it, but I'm having no luck. I'll post the original SelectionSort method below, and then the class and driver that I'm creating.

Original SelectionSort:
Code:

public class SelectionSort {
        private int[] data;
        private static final Random generator = new Random();
       
        public SelectionSort(int size) {
                data = new int[size];
               
                for(int i = 0; i < size; i++) {
                        data[i] = 10 + generator.nextInt(90);
                }
        }
       
        public void sort() {
                int smallest;
               
                for(int i = 0; i < data.length - 1; i++) {
                        smallest = i;
                       
                        for(int index = i + 1; index < data.length; index++) {
                                if(data[index] < data[smallest]) {
                                        smallest = index;
                                }
                        }
                       
                        swap(i, smallest);
                }
        }
       
        public void swap(int first, int second) {
                int temporary = data[first];
                data[first] = data[second];
                data[second] = temporary;
        }
       
}

My simple driver program:
Code:

public class GenericsDriver {

        public static void main(String[] args) {
               
                SelectionSort<Integer> intSort = new SelectionSort<Integer>();
               
                intSort.AddGrade(100);
                intSort.AddGrade(90);
                intSort.AddGrade(50);
                intSort.AddGrade(80);
                intSort.AddGrade(95);
               
                intSort.PrintNumbers();
               
                //sort here
               
                intSort.PrintNumbers();
               
                SelectionSort<Double> doubleSort = new SelectionSort<Double>();
               
                doubleSort.AddGrade(100.1);
                doubleSort.AddGrade(90.4);
                doubleSort.AddGrade(50.7);
                doubleSort.AddGrade(100.2);
                doubleSort.AddGrade(100.5);
               
                doubleSort.PrintNumbers();
               
                //sort here
               
                doubleSort.PrintNumbers();

        }

}

The new class and my attempt to repurpose the SelectionSort method:
Code:

import java.util.*;

public class SelectionSort <T> {

        private Array<T> numbers;
       
        public SelectionSort() {
                numbers = new ArrayList<T>();
        }
       
        public void AddGrade(T number) {
                numbers.add(number);
        }
       
        public void PrintNumbers() {
                System.out.println(numbers.toString());

        }
       
        public <T extends Comparable<T>> selectionSort() {
                int smallest;
               
                for(int  i = 0; i < numbers.size(); i++) {
                        smallest = i;
                       
                        for(int index = i + 1; index < numbers.size(); index++) {
                                if(numbers.) {
                                       
                                }
                        }
                }
        }
       
        public void swap(int first, int second) {
               
        }
       
}

As you can see... I haven't had any luck with sort or swap within my new class. I can't get it to work. My instructions have a hint that I should use <T extends Comparable<T>> in my sort method...but nothing is giving me the ability to use a .compareTo() method.

Could someone please give me some guidance here? Thanks.

Viewing all articles
Browse latest Browse all 54

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>