Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Need help with my code it is saying that I am missing public static void main(String[] args) and I am not sure where that should

Need help with my code it is saying that I am missing public static void main(String[] args) and I am not sure where that should go in my code.

public class MySort { public static void insertSort(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; } arr[j + 1] = key; } }

public static void selectSort(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 quickSort(int[] arr) { quickSortRecursive(arr, 0, arr.length); }

private static void quickSortRecursive(int[] arr, int begin, int end) { if (begin < end) { int pi = pivot(arr, begin, end); // edited line

quickSortRecursive(arr, begin, pi - 1); // Before pi quickSortRecursive(arr, pi + 1, end); // After pi } }

private static int pivot(int[] arr, int begin, int end) { int pivot = arr[end]; int i = (begin-1); for (int j=begin; j

public static void mergeSort(int[] arr) { quickSortRecursive(arr, 0, arr.length); }

private static void merge(int[] arr, int l, int m, int r) { int n1 = m - l + 1; int n2 = r - m; int L[] = new int[n1]; int R[] = new int[n2]; for (int i = 0; i < n1; ++i) L[i] = arr[l + i]; for (int j = 0; j < n2; ++j) R[j] = arr[m + 1 + j]; int i = 0, j = 0; int k = l; while (i < n1 && j < n2) { if (L[i] <= R[j]) { arr[k] = L[i]; i++; } else { arr[k] = R[j]; j++; } k++; } while (i < n1) { arr[k] = L[i]; i++; k++; } while (j < n2) { arr[k] = R[j]; j++; k++; } }

private static void mergeSortRecursive(int [] arr, int l, int r) { if (l < r) { int m = (l + r) / 2; mergeSortRecursive(arr, l, m); mergeSortRecursive(arr, m + 1, r);

merge(arr, l, m, r); } } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Professional SQL Server 2012 Internals And Troubleshooting

Authors: Christian Bolton, Justin Langford

1st Edition

1118177657, 9781118177655

More Books

Students also viewed these Databases questions

Question

What is the principle of thermodynamics? Explain with examples

Answered: 1 week ago