Vocab

recursive methods

  • are methods that call themselves over and over
  • the parts are a base call and recursive call
  • eventually, the base case will be reached, stopping recursion
  • recursive methods hve call stack that checks how many times a recursive function is called
public static int multiply(int a, int b) {
    if (b==0) {
        return 0;
    }
    else {
        return multiply(a, b - 1 ) + a; 
    }
}
multiply(2,2);
4
  • halves an array over and over until a value is found
  • this is more efficient than linear search
import java.util.*;

class Main {

    int binarySearch(int arr[], int l, int r, int x)
    {
 
        if (r >= l && l <= arr.length - 1) {
 
            int mid = l + (r - l) / 2;
 
            if (arr[mid] == x)
                return mid;

            if (arr[mid] > x)
                return binarySearch(arr, l, mid - 1, x);
 
            return binarySearch(arr, mid + 1, r, x);
        }
 
        return -1;
    }
 
    public static void main(String args[])
    {

        Main ob = new Main();

        int arr[] = { 2, 3, 4, 10, 40 };

        int n = arr.length;

        int x = 10;
 
        int result = ob.binarySearch(arr, 0, n - 1, x);
 
        if (result == -1)

            System.out.println("Element not present");

        else
 
            System.out.println("Element found at index "
                               + result);
    }
}
GFG.main(null);
Element found at index 3

Linear recursion

  • function that only makes one call to itself everytime func runs
int factorial (int n) { //ex of linear
    if (n==0) {
        return 1;
    }
    return n* factorial (n-1); 
}
factorial(5);
120

Selection sort

  • finds minimum element from the unsorted and put it at the end of the sorted
import java.io.*;
public class Main
{
    void sort(int arr[])
    {
        int n = arr.length;
 
        for (int i = 0; i < n-1; i++)
        {
            
            int min_idx = i;
            for (int j = i+1; j < n; j++)
                if (arr[j] < arr[min_idx])
                    min_idx = j;
 
            int temp = arr[min_idx];
            arr[min_idx] = arr[i];
            arr[i] = temp;
        }
    }
 
    void printArray(int arr[])
    {
        int n = arr.length;
        for (int i=0; i<n; ++i)
            System.out.print(arr[i]+" ");
        System.out.println();
    }

    public static void main(String args[])
    {
        Main ob = new Main();
        int arr[] = {5,14,2,90,41};
        ob.sort(arr);
        System.out.println("Sorted array");
        ob.printArray(arr);
    }
}
Main.main(null);
Sorted array
2 5 14 41 90 

Merge Sort

  • used to sort arraylists
  • splits array into 2 halves, calls itself, then merges them, to do this, the merge function is used
class MergeSort {
    void merge(int arr[], int l, int m, int r)
    {
        int n1 = m - l + 1;
        int n2 = r - m;
 
        int L[] = new int[n1];
        int R[] = new int[n2];
 
        for (int i = 0; i < n1; ++i)
            L[i] = arr[l + i];
        for (int j = 0; j < n2; ++j)
            R[j] = arr[m + 1 + j];
 
        int i = 0, j = 0;
 
        int k = l;
        while (i < n1 && j < n2) {
            if (L[i] <= R[j]) {
                arr[k] = L[i];
                i++;
            }
            else {
                arr[k] = R[j];
                j++;
            }
            k++;
        }

        while (i < n1) {
            arr[k] = L[i];
            i++;
            k++;
        }

        while (j < n2) {
            arr[k] = R[j];
            j++;
            k++;
        }
    }
 
    void sort(int arr[], int l, int r)
    {
        if (l < r) {
            int m = l + (r - l) / 2;

            sort(arr, l, m);
            sort(arr, m + 1, r);

            merge(arr, l, m, r);
        }
    }
 
    static void printArray(int arr[])
    {
        int n = arr.length;
        for (int i = 0; i < n; ++i)
            System.out.print(arr[i] + " ");
        System.out.println();
    }

    public static void main(String args[])
    {
        int arr[] = { 12, 11, 13, 5, 6, 7 };
 
        System.out.println("Given Array");
        printArray(arr);
 
        MergeSort ob = new MergeSort();
        ob.sort(arr, 0, arr.length - 1);
 
        System.out.println("\nSorted array");
        printArray(arr);
    }
}
MergeSort.main(null);
Given Array
12 11 13 5 6 7 

Sorted array
5 6 7 11 12 13