In Java write a program to implement Quicksort Also implemen
In Java, write a program to implement Quicksort. Also implement one of the slow sorts (Bubble, Insertion...). After you have tested both, generate a very large array (30,000 or more) of bytes and sort increasingly larger portions of the array (5000, 10000, ..., 30000) with both algorithms. Draw a graph of execution time versus number of elements for both algorithms.
Solution
import java.util.Random;
 import java.util.Arrays;
 public class Main{
// Bubble sort function
     static void bubbleSort(byte[] arr) {
        int n = arr.length;
         byte temp = 0;
         for(int i=0; i < n; i++){
             for(int j=1; j < (n-i); j++){
                 if(arr[j-1] > arr[j]){
                      //swap elements
                      temp = arr[j-1];
                      arr[j-1] = arr[j];
                      arr[j] = temp;
                  }
              }
         }
}
    static void quickSort(byte[] arr, int low, int high) {
        if (arr == null || arr.length == 0)
            return;
       if (low >= high)
            return;
       // pick the pivot
        int middle = low + (high - low) / 2;
        byte pivot = arr[middle];
       // make left < pivot and right > pivot
        int i = low, j = high;
        while (i <= j) {
            while (arr[i] < pivot) {
                i++;
            }
           while (arr[j] > pivot) {
                j--;
            }
           if (i <= j) {
                byte temp = arr[i];
                arr[i] = arr[j];
                arr[j] = temp;
                i++;
                j--;
            }
        }
       // recursively sort two sub parts
        if (low < j)
            quickSort(arr, low, j);
       if (high > i)
            quickSort(arr, i, high);
    }
public static void main(String[] args) {
        //creating an array of bytes of length 30000 and filling it with random bytes.
        byte[] b = new byte[30000];
        new Random().nextBytes(b);
        //iterating for different values of array lengths and comparing time.
        for(int j=1; j<=6; j++){
            byte[] b1 = Arrays.copyOfRange(b, 0, 5000*j);
            long startTime = System.currentTimeMillis();
            quickSort(b1, 0, 5000*j-1);
            long endTime   = System.currentTimeMillis();
            long totalTime = endTime - startTime;
            System.out.println(\"Time for array of first \"+5000*j+\" elements for quickSort: \"+totalTime +\" milli seconds.\");
            byte[] b2 = Arrays.copyOfRange(b, 0, 5000*j);
            startTime = System.currentTimeMillis();
            bubbleSort(b2);
            endTime   = System.currentTimeMillis();
            totalTime = endTime - startTime;
            System.out.println(\"Time for array of first \"+5000*j+\" elements for bubbleSort: \"+totalTime+\" milli seconds.\");
        }
     }
 }


