Question: Add comments to everything: import java.util.Random; class BubbleSort{ public int swaps; public BubbleSort() { swaps = 0; } public void bubbleSort(int arr[]) { int n

Add comments to everything:

import java.util.Random; class BubbleSort{ public int swaps; public BubbleSort() { swaps = 0; } public 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; swaps++; } } } } } //Insertion Sort class class InsertionSort{ public int swaps; public InsertionSort() { swaps = 0; } public void insertionSort(int arr[]) { int n = arr.length; for (int i = 1; i < n; ++i) { int key = arr[i]; int j = i - 1; while (j >= 0 && arr[j] > key) { arr[j + 1] = arr[j]; j = j - 1; swaps++; } arr[j + 1] = key; swaps++; } } } //Quick Sort class class quickSort{ public int swaps; public quickSort() { swaps = 0; } public void Swap(int arr[], int i, int j) { int val = arr[i]; arr[i] = arr[j]; arr[j] = val; } public int Partition(int arr[], int first, int last) { int pivot = arr[last]; int leftIndex = first; for(int j=first; j<=last; j++) { if(arr[j] < pivot) { Swap(arr, leftIndex, j); swaps++; leftIndex++; } } Swap(arr, leftIndex, last); swaps++; return leftIndex; } public void QuickSort(int arr[], int first, int last) { int pivot; if(first < last) { pivot = Partition(arr, first, last); QuickSort(arr, first, pivot-1); QuickSort(arr, pivot+1, last); } }

} //Merge Sort class class MergeSort{ public int swaps; public MergeSort() { swaps = 0; } public void merge(int A[], int low, int mid, int high) { int rightStart = mid + 1; if (A[mid] <= A[rightStart]) return; while (low <= mid && rightStart <= high) { if (A[low] <= A[rightStart]) low++; else { int value = A[rightStart]; int index = rightStart; while (index != low) { A[index] = A[index - 1]; swaps++; index--; } A[low] = value; swaps++; low++; mid++; rightStart++; } } } //Recursive function for mergeSort public void mergeSort(int A[], int low, int high) { if (low < high) { int mid = low + (high - low) / 2; mergeSort(A, low, mid); mergeSort(A, mid + 1, high); merge(A, low, mid, high); } } }

public class Main { public static void main(String args[]) { Random rand = new Random(); int num; int arr1 [] = new int[20]; int arr2 [] = new int[20]; int arr3 [] = new int[20]; int arr4 [] = new int[20]; for(int i=0;i<20;i++) { num = rand.nextInt(100); arr1[i] = num; arr2[i] = num; arr3[i] = num; arr4[i] = num; } InsertionSort is = new InsertionSort(); MergeSort ms = new MergeSort(); BubbleSort bs = new BubbleSort(); quickSort qs = new quickSort(); is.insertionSort(arr1); bs.bubbleSort(arr2); ms.mergeSort(arr3, 0, arr3.length-1); qs.QuickSort(arr4, 0, arr4.length-1); System.out.println("Number of swaps in Insertion sort = "+is.swaps); System.out.println("Number of swaps in Bubble sort = "+bs.swaps); System.out.println("Number of swaps in Merge sort = "+ms.swaps); System.out.println("Number of swaps in Quick sort = "+qs.swaps); } }

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!