Question

Rewrite this code in Java please #include <iostream> #include <fstream> #include <cstdlib> #include <ctime> using namespace...

Rewrite this code in Java please

#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 << "\nLength\t: " << length << '\n';

        read();

        t1 = clock();

        bubbleSort();

        t2 = clock();

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

        read();

        t1 = clock();

        insertionSort();

        t2 = clock();

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

        read();

        t1 = clock();

        quickSort(0, length - 1);

        t2 = clock();

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

        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;

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Here is the coverted Java version for you:

import java.util.*;
class SortingTime
{
static int length = 1000;
final static int max_length = 100000;
static int list[] = new int[max_length];
public static void read()
{
Scanner in = new Scanner(System.in);
in = new Scanner("random.dat");
for (int i = 0; i < length; i++)
{
list[i] = in.nextInt();
}
}
public static void bubbleSort()
{
int temp;
for(int 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;
}
}
}
}
public static 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;
}
}
public static 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;
}
public static 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)
{
double t1, t2;
for (length = 1000; length <= max_length; )
{
System.out.println("\nLength\t: " + length);
read();
t1 = System.currentTimeMillis();;
bubbleSort();
t2 = System.currentTimeMillis();;
System.out.println("Bubble Sort\t: " + (t2 - t1) + " sec");
read();
t1 = System.currentTimeMillis();;
insertionSort();
t2 = System.currentTimeMillis();;
System.out.println( "Insertion Sort\t: " + (t2 - t1) + " sec");
read();
t1 = System.currentTimeMillis();
quickSort(0, length - 1);
t2 = System.currentTimeMillis();
System.out.println( "Quick Sort\t: " + (t2 - t1) + " 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;
}
}
}
}

Add a comment
Know the answer?
Add Answer to:
Rewrite this code in Java please #include <iostream> #include <fstream> #include <cstdlib> #include <ctime> using namespace...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Merge Sort: Time Complexity: O(n log(n)) #include "stdafx.h" #include <iostream> #include <time.h> #include <stdlib.h> using namespace...

    Merge Sort: Time Complexity: O(n log(n)) #include "stdafx.h" #include <iostream> #include <time.h> #include <stdlib.h> using namespace std; void combine(int *a, int low, int high, int mid) {        int i, j, k, c[100000];        i = low;        k = low;        j = mid + 1;        while (i <= mid && j <= high)        {               if (a[i] < a[j])               {                      c[k] = a[i];                      k++;                      i++;               }               else               {                     ...

  • I need help with this code: #include <iostream> #include <cstdlib> #include <string> using namespace std; void...

    I need help with this code: #include <iostream> #include <cstdlib> #include <string> using namespace std; void dump(int ar[], int size) { cout << "\nDUMP [ "; for (int i=0; i<=size; i++) { cout << ar[i] << " "; } cout << "]\n\n"; } void quicksort(int ar[], int start, int end) { cout << "TOP OF SORT ===========================" << endl; dump(ar, start, end); int pivot = ar[end]; int left = start; int right = end - 1; int tmp; cout <<...

  • quickSort function. Calling previous functions already implemented. This is a bit different type of quicksort function...

    quickSort function. Calling previous functions already implemented. This is a bit different type of quicksort function where my partition function has 3 parameters instead of 4. So I need to write a function quicksort that actually puts all 3 of my functions together and finalizes the sorting process "There should be almost nothing done besides calling the other 3 functions and recursively calling itself (except to check for basecase) class quicksort { private: int left; int right; int* array;   ...

  • I want to compare the runtimes and swap operations times among quick Sort, selection Sort and...

    I want to compare the runtimes and swap operations times among quick Sort, selection Sort and shell Sort here is my code: But when I create a 1000,000 size array, I can't get the result of the operations times and runtime. what's wrong with my code? and I also want to copy the array. Because I want to use same array for three sort. And for the shell Sort, I haven't learn it in my class. Can anyone help me...

  • JAVA- Trace the recursive quick sort and partition methods in Lab6.java for this list of numbers:...

    JAVA- Trace the recursive quick sort and partition methods in Lab6.java for this list of numbers: 47 71 15 35 66 61 44 26 68 56 18 19 36 84 69 55 1. Find the value of pivot 2. Show the result after partitionIt() is called first time 3. Show the value of partition 4. Show the content of the array ///////////////////////////// Lab6.java class ArrayIns { private long[] theArray; // ref to array theArray private int nElems; // number of...

  •    moviestruct.cpp #include <iostream> #include <fstream> #include <cstdlib> #include <ostream> #include <fstream> #include <cstdlib> #include <cstring>...

       moviestruct.cpp #include <iostream> #include <fstream> #include <cstdlib> #include <ostream> #include <fstream> #include <cstdlib> #include <cstring> using namespace std; typedef struct{ int id; char title[250]; int year; char rating[6]; int totalCopies; int rentedCopies; }movie; int loadData(ifstream &infile, movie movies[]); void printAll(movie movies[], int count); void printRated(movie movies[], int count); void printTitled(movie movies[], int count); void addMovie(movie movies[],int &count); void returnMovie(movie movies[],int count); void rentMovie(movie movies[],int count); void saveToFile(movie movies[], int count, char *filename); void printMovie(movie &m); int find(movie movies[], int...

  • Can someone help me with this please: #include <iostream> #include <cstdlib> #include <string> #include <cstring> using namespace std; #define ARSIZ 150000 int debug=0; void du...

    Can someone help me with this please: #include <iostream> #include <cstdlib> #include <string> #include <cstring> using namespace std; #define ARSIZ 150000 int debug=0; void dump(int ar[], int len) { for(int i=0; i<len; i++) { cout<<" DUMP: data = : "<< ar[i] << endl; } } void sort(int *ar, int length) { if(length == 1 || length == 0) return; cout << "BREAKING DOWN: " << endl; //for(int i=0; i<length; i++) // cout << " DUMP: DATA = : " <<...

  • Hello, I want to check if my C++ code is correct and follows the requeriments described...

    Hello, I want to check if my C++ code is correct and follows the requeriments described thanks. Requeriments: Assignment Sorting Benchmark each of the sorting methods listed below. Insertion Sort Bubble Sort Selection Sort Heap Sort. Quick Sort. Merge Sort. Benchmark each of the above sorting methods for data sizes of 10000, 20000, 30000, 40000 and 50000. Display the results in a table as shown below. The table should have rows and columns. However, the rows and columns need not...

  • C++ Time the sequential search and the binary search methods several times each for randomly generated...

    C++ Time the sequential search and the binary search methods several times each for randomly generated values, then record the results in a table. Do not time individual searches, but groups of them. For example, time 100 searches together or 1,000 searches together. Compare the running times of these two search methods that are obtained during the experiment. Regarding the efficiency of both search methods, what conclusion can be reached from this experiment? Both the table and your conclusions should...

  • C++. Difficulty with quickSort function. Code will not run quickSort function. The code I'm having trouble...

    C++. Difficulty with quickSort function. Code will not run quickSort function. The code I'm having trouble with is in bold. -------------------------------------------------------------------------------------------------driverProgram.cpp #include #include #include #include #include "quickSort.cpp" using namespace std; int main() { const int MIN_SIZE = 4; //Array size const int SIZE = 25; int theArray[SIZE] = {11, 22, 33, 44, 55, 66, 77, 88, 99, 12, 13, 14, 15, 16, 17, 18, 19, 18, 19, 20, 21, 22, 23, 24, 25}; cout << "List of 25 items: ";...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT