Below is a java program for sorting and searching and it has 5 algorithms in whi
ID: 3825575 • Letter: B
Question
Below is a java program for sorting and searching and it has 5 algorithms in which it does that.
PLEASE EXPLAIN EACH LINE IN THE CODE IF POSSIBLE AS MUCH AS POSSIBLE, INCLUDING WHAT IT DOES AND WHAT IS DOES. THANK YOU.
Below is a java program for sorting and searching and it has 5 algorithms in which it does that.
PLEASE EXPLAIN EACH LINE IN THE CODE IF POSSIBLE AS MUCH AS POSSIBLE, INCLUDING WHAT IT DOES AND WHAT IS DOES. THANK YOU.
package Sorting_Searching;
import java.util.Scanner;
public class SortingSearching {
public static long aSelSort;
public static long bSelSort;
public static long aInsSort;
public static long bInsSort;
public static long aBubSort;
public static long bBubSort;
public static long aBinSearch;
public static long bBinSearch;
public static long aLinSearch;
public static long bLinSearch;
public static int[] selectionSort(int[] array) {
aSelSort = System.nanoTime();
for (int i = 0; i < array.length - 1; i++) {
int index = i;
for (int j = i + 1; j < array.length; j++)
if (array[j] < array[index])
index = j;
int sNumber = array[index];
array[index] = array[i];
array[i] = sNumber;
}
bSelSort = System.nanoTime();
return array;
}
public static int[] insertionSort(int[] arr) {
aInsSort = System.nanoTime();
int temp;
for (int i = 1; i < arr.length; i++) {
for (int j = i; j > 0; j--) {
if (arr[j] < arr[j - 1]) {
temp = arr[j];
arr[j] = arr[j - 1];
arr[j - 1] = temp;
}
}
}
bInsSort = System.nanoTime();
return arr;
}
public static int[] bubbleSort(int[] arr) {
aBubSort = System.nanoTime();
int n = arr.length;
int temp = 0;
for (int i = 0; i < n; i++) {
for (int j = 1; j < (n - i); j++) {
if (arr[j - 1] > arr[j]) {
temp = arr[j - 1];
arr[j - 1] = arr[j];
arr[j] = temp;
}
}
}
bBubSort = System.nanoTime();
return arr;
}
public static int binarySearch(int[] arr, int start, int end, int key) {
if (start < end) {
int mid = start + (end - start) / 2;
if (key < arr[mid]) {
return binarySearch(arr, start, mid, key);
} else if (key > arr[mid]) {
return binarySearch(arr, mid + 1, end, key);
} else {
return mid;
}
}
return -(start + 1);
}
public static int linearSearch(int[] arr, int key) {
aLinSearch = System.nanoTime();
int i = 0;
for (i = 0; i < 1000; i++)
if (key == arr[i])
break;
return i;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int[] arr = new int[1000];
int[] arr1 = new int[1000];
int[] arr2 = new int[1000];
int[] arr3 = new int[1000];
for (int i = 0; i < 1000; i++)
arr[i] = (int) (Math.random() * 1000 + 1);
arr1 = selectionSort(arr);
arr2 = insertionSort(arr);
arr3 = bubbleSort(arr);
System.out.print("Enter the key to search: ");
int key = scanner.nextInt();
aBinSearch = System.nanoTime();
int binSearch1 = binarySearch(arr1, 0, 999, key);
int binSearch2 = binarySearch(arr2, 0, 999, key);
int binSearch3 = binarySearch(arr3, 0, 999, key);
bBinSearch = System.nanoTime();
aLinSearch = System.nanoTime();
int linSearch1 = linearSearch(arr1, key);
int linSearch2 = linearSearch(arr2, key);
int linSearch3 = linearSearch(arr3, key);
bLinSearch = System.nanoTime();
System.out.println("Time consumed by Selection Sort: " + (bSelSort - aSelSort) + " Milisecond");
System.out.println("Time consumed by Insertion Sort: " + (bInsSort - aInsSort) + " Milisecond");
System.out.println("Time consumed by Bubble Sort: " + (bBubSort - aBubSort) + " Milisecond");
System.out.println("Time consumed by Binary Search: " + (bBinSearch - aBinSearch) + " Milisecond");
System.out.println("Time consumed by Linear Search: " + (bLinSearch - aLinSearch) + " Milisecond");
}
}
Explanation / Answer
Code with explanation:
package Sorting_Searching; //package declaration
import java.util.Scanner; //importing scanner which is used to read input from console
public class SortingSearching { //all java functions are enclosed within a class
//set of variables for each sorting function to read the start and end time
public static long aSelSort;
public static long bSelSort;
public static long aInsSort;
public static long bInsSort;
public static long aBubSort;
public static long bBubSort;
public static long aBinSearch;
public static long bBinSearch;
public static long aLinSearch;
public static long bLinSearch;
//selection sort method
public static int[] selectionSort(int[] array) {
//getting the system time before sorting begins
aSelSort = System.nanoTime();
// for each element in the array
// it is compared with the remaining set of elements
// and if any element is lower than the current element
// then the elements are swapped
for (int i = 0; i < array.length - 1; i++) {
int index = i;
for (int j = i + 1; j < array.length; j++)
if (array[j] < array[index])
index = j;
int sNumber = array[index];
array[index] = array[i];
array[i] = sNumber;
}
//getting the system time after sorting ends
bSelSort = System.nanoTime();
return array;
}
// for each element int the array, it is compared with
// every previous element and then swapped if the current element is
// lower than the previous element
public static int[] insertionSort(int[] arr) {
aInsSort = System.nanoTime();
int temp;
for (int i = 1; i < arr.length; i++) {
for (int j = i; j > 0; j--) {
if (arr[j] < arr[j - 1]) {
temp = arr[j];
arr[j] = arr[j - 1];
arr[j - 1] = temp;
}
}
}
bInsSort = System.nanoTime();
return arr;
}
// for each element in the array
// each element from the 2nd element till the current element is compared
// and the value is swapped
public static int[] bubbleSort(int[] arr) {
aBubSort = System.nanoTime();
int n = arr.length;
int temp = 0;
for (int i = 0; i < n; i++) {
for (int j = 1; j < (n - i); j++) {
if (arr[j - 1] > arr[j]) {
temp = arr[j - 1];
arr[j - 1] = arr[j];
arr[j] = temp;
}
}
}
bBubSort = System.nanoTime();
return arr;
}
// this function searches for a value and returns the index of that value in the array
// the array should be in sprte manner for this function to work properly
// this is a recursive function that will calculate the mid of start and end and checks if the value to find is less than
// or greater than the mid value, based on this the set is divided again and finally the index is returned
public static int binarySearch(int[] arr, int start, int end, int key) {
if (start < end) {
int mid = start + (end - start) / 2;
if (key < arr[mid]) {
return binarySearch(arr, start, mid, key);
} else if (key > arr[mid]) {
return binarySearch(arr, mid + 1, end, key);
} else {
return mid;
}
}
return -(start + 1);
}
// the value is searched by looping through the array one after the other and checking for equality
//finally the index is returned
public static int linearSearch(int[] arr, int key) {
aLinSearch = System.nanoTime();
int i = 0;
for (i = 0; i < 1000; i++)
if (key == arr[i])
break;
return i;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int[] arr = new int[1000];
int[] arr1 = new int[1000];
int[] arr2 = new int[1000];
int[] arr3 = new int[1000];
for (int i = 0; i < 1000; i++)
arr[i] = (int) (Math.random() * 1000 + 1);
arr1 = selectionSort(arr);
arr2 = insertionSort(arr);
arr3 = bubbleSort(arr);
System.out.print("Enter the key to search: ");
int key = scanner.nextInt();
aBinSearch = System.nanoTime();
int binSearch1 = binarySearch(arr1, 0, 999, key);
int binSearch2 = binarySearch(arr2, 0, 999, key);
int binSearch3 = binarySearch(arr3, 0, 999, key);
bBinSearch = System.nanoTime();
aLinSearch = System.nanoTime();
int linSearch1 = linearSearch(arr1, key);
int linSearch2 = linearSearch(arr2, key);
int linSearch3 = linearSearch(arr3, key);
bLinSearch = System.nanoTime();
//the time taken to complete the sorting is calculated by subtracting the start time and end time
System.out.println("Time consumed by Selection Sort: " + (bSelSort - aSelSort) + " Milisecond");
System.out.println("Time consumed by Insertion Sort: " + (bInsSort - aInsSort) + " Milisecond");
System.out.println("Time consumed by Bubble Sort: " + (bBubSort - aBubSort) + " Milisecond");
System.out.println("Time consumed by Binary Search: " + (bBinSearch - aBinSearch) + " Milisecond");
System.out.println("Time consumed by Linear Search: " + (bLinSearch - aLinSearch) + " Milisecond");
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.