Question: import java.io.*; import java.util.Scanner; public class SortingAlgorithm{ public static void bubbleSort(int arr[]){ int n = arr.length; for (int i = 0; i < n-1; i++)

import java.io.*; import java.util.Scanner; public class SortingAlgorithm{ public static void bubbleSort(int arr[]){ int n = arr.length; for (int i = 0; i < n-1; i++) for (int j = 0; j < n-i-1; j++) if (arr[j] > arr[j+1]){

int temp = arr[j]; arr[j] = arr[j+1]; arr[j+1] = temp; } } public static void selectionsort(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; } }

public static void insertionsort(int arr[]){ int n = arr.length; for (int i=1; i

while (j>=0 && arr[j] > key){ arr[j+1] = arr[j]; j = j-1; } arr[j+1] = key; } }

public void printArray(int arr[]){ int n = arr.length; for (int i=0; i

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

Scanner input=new Scanner(new File(args [0])); SortingAlgorithm ob1 = new SortingAlgorithm(); SortingAlgorithm ob2 = new SortingAlgorithm(); SortingAlgorithm ob3 = new SortingAlgorithm(); int rows = input.nextInt(), cols = input.nextInt(); int arr[] = new int[cols]; for(int i = 0; i < cols; ++i) { arr[i] = input.nextInt(); } ob1.bubbleSort(arr); ob2.selectionsort(arr); ob3.insertionsort(arr); System.out.println("Sorted array after bubble sort"); ob1.printArray(arr); System.out.println("Sorted array after selection sort"); ob2.printArray(arr); System.out.println("Sorted array after insertion sort"); ob3.printArray(arr); } }

data1.data

2 10 2 20 198 116 99 48 202 121 105 95 114 96 158 127 184 155 146 78 165 196

So i'm suppose to record the sorting speed in nano and milliseconds. How would i do that? thanks!

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!