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

In this task we will compare the time complexity of two sorting methods. Impleme

ID: 3886244 • Letter: I

Question

In this task we will compare the time complexity of two sorting methods. Implement first bubble sort with Java and then measure how much time it will take to sort the datasets in the files data1.txt, data2.txt, data3.txt and data4.txt (present the elapsed time separately for each dataset)? Do the sorting again for each dataset using the sort method from the Arrays class (see Java API for more details) and examine now how much time it will take to sort the datasets. Read the dataset from a file into an array. Print the elapsed time and the number of elements in an array. Measure only the time what sorting will take. Do not take into account, for example, the time what reading the files will take.

Here are the data1.txt, data2.txt, data3.txt and data4.txt files download link.

https://www.dropbox.com/sh/av7ssd1icfq627a/AADkGvzOQpCJv1llb1ppRa1oa?dl=0

Explanation / Answer

import java.io.BufferedReader;

import java.io.FileReader;

import java.io.IOException;

import java.util.ArrayList;

import java.util.Arrays;

public class TestTime {

public static void main(String[] args) throws IOException {

ArrayList<Integer> list = new ArrayList<Integer>();

BufferedReader br = null;

BufferedReader br2 = null;

br = new BufferedReader(new FileReader("E:\datafiles\data1.txt"));

// One way of reading the file

String contentLine = null;

while ((contentLine = br.readLine()) != null) {

String[] nos = contentLine.split(" ");

for (String no : nos) {

list.add(Integer.parseInt(no));

}

}

Object[] array = list.toArray();

// sorting the array using arrays.sort

long startTime1 = System.currentTimeMillis();

Arrays.sort(array);

long stopTime1 = System.currentTimeMillis();

long elapsedTime1 = stopTime1 - startTime1;

System.out

.println("the sorting time in milliseconds are using arrays.sort is :"

+ elapsedTime1);

/* for (int k = 0; k < array.length; k++) {

System.out.print((Integer) k);

}

*/

// sorting the array using bubble sort

long startTime = System.currentTimeMillis();

sort(array);

long stopTime = System.currentTimeMillis();

long elapsedTime = stopTime - startTime;

System.out

.println("the sorting time in milliseconds are using bubblesort is :"

+ elapsedTime);

}

private static void sort(Object[] array) {

for (int j = 0; j < array.length; j++) {

// initially swapped is false

boolean swapped = false;

int i = 0;

while (i < array.length - 1) {

// comparing the adjacent elements

if ((Integer) array[i] > (Integer) array[i + 1]) {

// swapping

Integer temp = (Integer) array[i];

array[i] = array[i + 1];

array[i + 1] = temp;

// Changing the value of swapped

swapped = true;

}

i++;

}

// if swapped is false then the array is sorted

// we can stop the loop

if (!swapped)

break;

}

}

}

output

the sorting time in milliseconds are using arrays.sort is :1
the sorting time in milliseconds are using bubblesort is :0

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote