Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.ArrayList;
- public class SelectionSortRecursiveManager {
- public static void main(String[] args) throws Exception {
- int[] ivalue = {7, 14, 2, -1, 35, 3};
- double[] dvalue = {9.2, 15.3, 4.1, 2.3, 4.2, 1.3};
- int i;
- //New Instance of the Generic Manager for Integers
- MyRecursiveManager<Integer> myints = new MyRecursiveManager<Integer>();
- //Add 6 int values
- for (i = 0; i <= 5; i++) myints.setvalue(ivalue[i]);
- //Print the values
- System.out.println("These are from myints");
- for (i = 0; i <= 5; i++) System.out.println(myints.getvalue(i));
- myints.SelectSort(5);
- for (i = 0; i <= 5; i++) System.out.println(myints.getvalue(i));
- //New instance of the Generic Manager for Doubles
- MyRecursiveManager<Double> mydoubles = new MyRecursiveManager<Double>();
- //Add 6 double values
- for (i = 0; i <= 5; i++) mydoubles.setvalue(dvalue[i]);
- //Print the values
- System.out.println("These are from doubles");
- for (i = 0; i <= 5; i++) System.out.println(mydoubles.getvalue(i));
- mydoubles.SelectSort(5);
- for (i = 0; i <= 5; i++) System.out.println(mydoubles.getvalue(i));
- }
- //The MyRecursiveManager Class
- static class MyRecursiveManager<T extends Comparable<T>> {
- protected ArrayList<T> values = new ArrayList<T>();
- protected int mcount;
- protected T max;
- //Constructor
- public MyRecursiveManager() {
- mcount = 0;
- }
- //Sets values into the ArrayList Values
- public int setvalue(T x) {
- values.add(mcount++, x);
- return mcount;
- }
- //Gets the values from Values ArrayList
- public T getvalue(int i) {
- if (i <= mcount) return values.get(i);
- else return values.get(0);
- }
- //Recursive SelectSort
- public void SelectSort(int high) {
- System.out.println("In SelectSort with values " + high);
- //Stopping condition for the recursive Function.
- if (high > 0) {
- //Find the Max element to high subscript
- int indexOfMax = 0;
- max = values.get(0);
- for (int i = 1; i <= high; i++) {
- if ((values.get(i)).compareTo(max) == 1) {
- max = values.get(i);
- indexOfMax = i;
- }
- }
- //Swap the largest with the last number in the list
- values.set(indexOfMax, values.get(high));
- values.set(high, max);
- //Now recursively call sort on the list that is one element shorter.
- SelectSort(high - 1);
- }
- else return;
- return;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement