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
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"); } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started