CS3343/3341
 Analysis of Algorithms 
Spring 2012
  Recursive Selection Sort  


Question 6 in Recitation 2: Recursive Selection Sort

The programs below create an array A of 15 random doubles. They use a recursive function max to find the largest element in this array.

For this question, you are asked to write a recursive selection sort function by making additions and changes to either the Java or the C progam below (or both) in the following steps:

  1. Replace the recursive function max with a new recursive function maxInd (in Java, or maxind in C) that will return the integer index of the largest element in the array. (For the new task, we no longer care what the value of the largest element is.)

  2. Test the function maxInd with a separate run.

  3. Add a function selectionSort (in Java, or selectionsort in C) that will sort the array into order. Keep in mind that an array of length 1 is already sorted. (In Java, you may want to rename your class, from Findmax to, say, SelectionSort.)

    1. This function should first find the index of the largest element of A from 0 to n-1, inclusive, (that is, 0 <= i < n).

    2. Exchange the element found in the previous step with the (n-1)st element, using a separate function exchange.

    3. Use the same selection sort function recursively to sort the portion of A from 0 to n-2, inclusive.

    4. Test the final program by sorting an array of 15 random numbers.

    5. Be sure to include the source code and the results of the run above in your submission for Recitation 2.

Change Findmax below to a Selection Sort
Change max to maxInd (or maxind)
add exchange and selectionSort (or selectionsort) functions
change the class name to SelectionSort (in Java)
JavaC
public class Findmax {

   final static int N = 15;




   public static void main(String[] args) {
      double[] A = new double[N]; // random numbers

      init(A, N); // rand #s for A[0], ..., A[N-1]
      printit(A, N); // print them
      System.out.println("Max element:" + max(A, N));
   }

   static double max(double[] A, int n) {
      double maxel;
      if (n == 1) return A[0];
      maxel = max(A, n-1);
      if (A[n-1] > maxel) return A[n-1];
      return maxel;
   }

   static void init(double[] A, int n) {

      for (int i = 0; i < n; i++)
         // 0 < A[i] < 1, uniformly distributed
         A[i] = Math.random();
   }

   static void printit(double[] A, int n) {
      for (int i = 0; i < n; i++)
         System.out.println("A[" + i + "]: " + A[i]);
   }
}
A[0]: 0.05296796292816863 A[1]: 0.9476081128036812 A[2]: 0.4324621164425462 A[3]: 0.5121866683825645 A[4]: 0.852699404627387 A[5]: 0.9261347279185631 A[6]: 0.49011941728218267 A[7]: 0.7412717309732726 A[8]: 0.8267814329433709 A[9]: 0.3777698237675361 A[10]: 0.09880164730739871 A[11]: 0.13732387666850032 A[12]: 0.8065651999152179 A[13]: 0.18945082858646745 A[14]: 0.7082388737745514 Max element:0.9476081128036812
#include <stdio.h>
#include <stdlib.h> // for rand()
#define N 15
void init(double [], int);
void printit(double [], int);
double max(double [], int);

void main(void) {
   double A[N]; // array of random numbers
   srand(27182818); // initialize the RNG
   init(A, N); // rand #s for A[0], ..., A[N-1]
   printit(A, N); // print them
   printf("Max element: %20.16f\n", max(A, N));
}

double max(double A[], int n) {
   double maxel;
   if (n == 1) return A[0];
   maxel = max(A, n-1);
   if (A[n-1] > maxel) return A[n-1];
   return maxel;
}

void init(double A[], int n) {
   int i;
   for (i = 0; i < n; i++)
      // 0 < A[i] < 1, uniformly distributed
      A[i] = (double)rand()/(double)RAND_MAX;
}

void printit(double A[], int n) {
   int i;
   for (i = 0; i < n; i++)
      printf("A[%2i]:%20.16f\n", i, A[i]);
}

A[ 0]: 0.4493269116847435 A[ 1]: 0.6526975467115164 A[ 2]: 0.5089003236540129 A[ 3]: 0.8506893072513348 A[ 4]: 0.1337862327386561 A[ 5]: 0.0328305410374098 A[ 6]: 0.9785053999994441 A[ 7]: 0.7247469721942893 A[ 8]: 0.4170996506778056 A[ 9]: 0.8085312856400997 A[10]: 0.8870747312377555 A[11]: 0.9881582446341208 A[12]: 0.9362502637953731 A[13]: 0.8741503031338334 A[14]: 0.5737268238206054 Max element: 0.9881582446341208


Revision date: 2011-12-02. (Please use ISO 8601, the International Standard Date and Time Notation.)