Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

translate this code into Java #include <iostream> #include <fstream> #include <c

ID: 3783958 • Letter: T

Question

translate this code into Java

#include <iostream>

#include <fstream>

#include <cstdlib>

#include <ctime>

using namespace std;

long length = 1000;

const long max_length = 100000;

int list[max_length];

void read()

{

    ifstream fin("random.dat", ios::binary);

    for (long i = 0; i < length; i++)

    {

        fin.read((char*)&list[i], sizeof(int));

    }

    fin.close();

}

void bubbleSort()

{

    int temp;

    for(long i = 0; i < length; i++)

    {

        for(long j = 0; j< length-i-1; j++)

        {

            if (list[j] > list[j+1])

            {

                temp        = list[j];

                list[j]     = list[j+1];

                list[j+1] = temp;

            }

        }

    }

}

void insertionSort()

{

    int temp;

    for(long i = 1; i < length; i++)

    {

        temp = list[i];

        long j;

        for(j = i-1; j >= 0 && list[j] > temp; j--)

        {

            list[j+1] = list[j];

        }

        list[j+1] = temp;

    }

}

long partition(long left, long right)

{

    int pivot_element = list[left];

    int lb = left, ub = right;

    int temp;

    while (left < right)

    {

        while(list[left]< = pivot_element)

            left++;

        while(list[right]> pivot_element)

            right--;

        if (left < right)

        {

            temp        = list[left];

            list[left] = list[right];

            list[right] = temp;

        }

    }

    list[lb] = list[right];

    list[right] = pivot_element;

    return right;

}

void quickSort(long left, long right)

{

    if (left < right)

    {

        long pivot = partition(left, right);

        quickSort(left, pivot-1);

        quickSort(pivot+1, right);

    }

}

int main()

{

    double t1, t2;

    for (length = 1000; length <= max_length; )

    {

        cout << " Length : " << length << ' ';

        read();

        t1 = clock();

        bubbleSort();

        t2 = clock();

        cout << "Bubble Sort : " << (t2 - t1)/CLK_TCK << " sec ";

        read();

        t1 = clock();

        insertionSort();

        t2 = clock();

        cout << "Insertion Sort : " << (t2 - t1)/CLK_TCK << " sec ";

        read();

        t1 = clock();

        quickSort(0, length - 1);

        t2 = clock();

        cout << "Quick Sort : " << (t2 - t1)/CLK_TCK << " sec ";

        switch (length)

        {

        case 1000 :

            length = 5000;

            break;

        case 5000 :

            length = 10000;

            break;

        case 10000 :

            length = 20000;

            break;

        case 20000 :

            length = 50000;

            break;

        case 50000 :

            length = 100000;

            break;

      case 100000 :

            length = 100001;

            break;

        }

    }

    return 0;

}

Explanation / Answer


import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.IOException;

public class Chegg {

   int length = 1000;
   final static long max_length = 100000;
   int list[] = new int[(int)max_length];
   final static int CLK_TCK = 100000007; // set it to CPU clock per cycle

   void read() throws IOException{
       DataInputStream fin = new DataInputStream(new FileInputStream("random.dat"));
   for (int i = 0; i < length; i++)
   {
   list[i] = fin.readInt();
   }
   fin.close();
   }

   void bubbleSort() {
       int temp;
       for (long i = 0; i < length; i++) {
           for (int j = 0; j < length - i - 1; j++) {
               if (list[j] > list[j + 1]) {
                   temp = list[j];
                   list[j] = list[j + 1];
                   list[j + 1] = temp;
               }
           }
       }
   }

   void insertionSort() {
       int temp;
       for (int i = 1; i < length; i++) {
           temp = list[i];
           int j;
           for (j = i - 1; j >= 0 && list[j] > temp; j--) {
               list[j + 1] = list[j];
           }
           list[j + 1] = temp;
       }
   }

   int partition(int left, int right)
   {
   int pivot_element = list[left];
   int lb = left, ub = right;
   int temp;
   while (left < right)
   {
   while(list[left] <= pivot_element)
   left++;
   while(list[right] > pivot_element)
   right--;
   if (left < right)
   {
   temp = list[left];
   list[left] = list[right];
   list[right] = temp;
   }
   }
   list[lb] = list[right];
   list[right] = pivot_element;
       return right;
   }

   void quickSort(int left, int right) {
       if (left < right) {
           int pivot = partition(left, right);
           quickSort(left, pivot - 1);
           quickSort(pivot + 1, right);
       }
   }

   public static void main(String[] args) throws IOException {
      
   double t1, t2;
   for (int length = 1000; length <= max_length; )
   {
   System.out.println(" Length : " + length);
   Chegg obj = new Chegg();
   obj.read();
   t1 = System.nanoTime();
   obj.bubbleSort();
   t2 = System.nanoTime();
   System.out.println("Bubble Sort : " + (t2 - t1)/CLK_TCK + " sec");
   obj.read();
   t1 = System.nanoTime();
   obj.insertionSort();
   t2 = System.nanoTime();
   System.out.println("Insertion Sort : " + (t2 - t1)/CLK_TCK + " sec");
   obj.read();
   t1 = System.nanoTime();
   obj.quickSort(0, length - 1);
   t2 = System.nanoTime();
   System.out.println("Quick Sort : " + (t2 - t1)/CLK_TCK + " sec");
   switch (length)
   {
   case 1000 :
   length = 5000;
   break;
   case 5000 :
   length = 10000;
   break;
   case 10000 :
   length = 20000;
   break;
   case 20000 :
   length = 50000;
   break;
   case 50000 :
   length = 100000;
   break;
   case 100000 :
   length = 100001;
   break;
   }
   }
   }
}