Advertisement
Luninariel

RecursiveSelectSort - Example

Mar 8th, 2019
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.89 KB | None | 0 0
  1. import java.util.ArrayList;
  2.  
  3. public class SelectionSortRecursiveManager {
  4.  
  5.     public static void main(String[] args) throws Exception {
  6.         int[] ivalue = {7, 14, 2, -1, 35, 3};
  7.         double[] dvalue = {9.2, 15.3, 4.1, 2.3, 4.2, 1.3};
  8.  
  9.         int i;
  10.  
  11.         //New Instance of the Generic Manager for Integers
  12.         MyRecursiveManager<Integer> myints = new MyRecursiveManager<Integer>();
  13.  
  14.         //Add 6 int values
  15.         for (i = 0; i <= 5; i++) myints.setvalue(ivalue[i]);
  16.  
  17.         //Print  the values
  18.         System.out.println("These are from myints");
  19.         for (i = 0; i <= 5; i++) System.out.println(myints.getvalue(i));
  20.  
  21.         myints.SelectSort(5);
  22.  
  23.         for (i = 0; i <= 5; i++) System.out.println(myints.getvalue(i));
  24.  
  25.         //New instance of the Generic Manager for Doubles
  26.         MyRecursiveManager<Double> mydoubles = new MyRecursiveManager<Double>();
  27.  
  28.         //Add 6 double values
  29.         for (i = 0; i <= 5; i++) mydoubles.setvalue(dvalue[i]);
  30.  
  31.         //Print the values
  32.         System.out.println("These are from doubles");
  33.         for (i = 0; i <= 5; i++) System.out.println(mydoubles.getvalue(i));
  34.  
  35.      mydoubles.SelectSort(5);
  36.  
  37.         for (i = 0; i <= 5; i++) System.out.println(mydoubles.getvalue(i));
  38.  
  39.     }
  40.  
  41.     //The MyRecursiveManager Class
  42.  static  class MyRecursiveManager<T extends Comparable<T>> {
  43.         protected ArrayList<T> values = new ArrayList<T>();
  44.         protected int mcount;
  45.         protected T max;
  46.  
  47.         //Constructor
  48.         public MyRecursiveManager() {
  49.             mcount = 0;
  50.         }
  51.  
  52.         //Sets values into the ArrayList Values
  53.         public int setvalue(T x) {
  54.             values.add(mcount++, x);
  55.             return mcount;
  56.         }
  57.  
  58.         //Gets the values from Values ArrayList
  59.         public T getvalue(int i) {
  60.             if (i <= mcount) return values.get(i);
  61.             else return values.get(0);
  62.         }
  63.  
  64.         //Recursive SelectSort
  65.         public void SelectSort(int high) {
  66.             System.out.println("In SelectSort with values " + high);
  67.  
  68.             //Stopping condition for the recursive Function.
  69.             if (high > 0) {
  70.                 //Find the Max element to high subscript
  71.                 int indexOfMax = 0;
  72.                 max = values.get(0);
  73.                 for (int i = 1; i <= high; i++) {
  74.                     if ((values.get(i)).compareTo(max) == 1) {
  75.                         max = values.get(i);
  76.                         indexOfMax = i;
  77.                     }
  78.                 }
  79.                 //Swap the largest with the last number in the list
  80.                 values.set(indexOfMax, values.get(high));
  81.                 values.set(high, max);
  82.                 //Now recursively call sort on the list that is one element shorter.
  83.                 SelectSort(high - 1);
  84.             }
  85.             else return;
  86.  
  87.             return;
  88.         }
  89.  
  90.     }
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement