cse114final.java

# cse114final.java - package Recursion import...

This preview shows pages 1–4. Sign up to view the full content.

package Recursion; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; /** * Created by Rahul on 12/11/2017. * This is for the Recursion Review questions. */ public class cse114final { /** * QUESTION 1 */ public static void selectionSort(double[] list) { selectionSort(list, 0, list.length - 1); } public static void selectionSort(double[] list, int low, int high) { if (low < high) { int indexOfMin = low; double min = list[low]; for (int i = low + 1; i <= high; i++) { if (list[i] < min) { min = list[i]; indexOfMin = i; } } list[indexOfMin] = list[low]; list[low] = min; selectionSort(list, low + 1, high); } } public static int recursiveBinarySearch(double[] list, double key) { int low = 0; int high = list.length - 1; return recursiveBinarySearch(list, key, low, high); } public static int recursiveBinarySearch(double[] list, double key, int low, int high) { if (low > high) return -low - 1; int mid = (low + high) / 2; if (key < list[mid]) return recursiveBinarySearch(list, key, low, mid - 1); else if (key == list[mid]) return mid; else return recursiveBinarySearch(list, key, mid + 1, high); } , /** * THIS IS A JUMP START FOR QUESTIONS 2 AND 3. * THE QUESTIONS ARE AVAILABLE IN THE RECURSION REVIEW. * Some helper functions are given. * The first */ //HELPER FUNCTIONS. /** * This function returns the first element of the array L. * @param L

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
* @return L[0] */ public static int hd(int[] L) { if(L.length == 0) return -1; return L[0]; } /** * This function returns the first array of the 2D array L. * @param L * @return L[0] */ public static int[] hd(int[][] L) { int[] newArray = {}; if(L.length == 0) return newArray; return L[0]; } /** * This function returns an array with all the elements of L, * with an exception of the first element. * @param L * @return L - L[0] */ public static int[] tl(int[] L) { int[] tail = new int[L.length - 1]; for (int i = 1; i < L.length; i++) { tail[i - 1] = L[i]; } return tail; } /** * This function returns an array with all the arrays of L, * with an exception of the first array. * @param L * @return L - L[0] */ public static int[][] tl(int[][] L) { int[][] tail = new int[L.length - 1][L[0].length]; for (int i = 1; i < L.length; i++) { for(int j=0; j< L[i].length; j++) tail[i - 1][j] = L[i][j]; } return tail; } /** * This appends an element E to the array L. * This method appends to the first part of L. * @param E * @param L * @return {E, L[0], ...} */ public static int[] append(int E, int[] L) { int[] appended = new int[L.length+1]; appended[0] = E; for(int i=1; i<L.length; i++) { appended[i] = L[i-1]; } if(L.length > 0) appended[L.length] = L[L.length - 1]; return appended; }
/** * This appends an array E to the 2D array L.

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern