import java.util.Scanner;
public class AllSortingAlgorithms {
//Selection Sort
public static void selectionSort(int[] arr) {
int n = arr.length;
for (int i = 0; i < n - 1; i++) {
int min = i;
for (int j = i + 1; j < n; j++) {
if (arr[j] < arr[min]) {
min = j;
}
}
int temp = arr[min];
arr[min] = arr[i];
arr[i] = temp;
}
}
//Bubble Sort
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;
}
}
}
}
//Insertion Sort
public static 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--;
}
arr[j + 1] = key;
}
}
//Merge Sort
public 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] 0) {
System.out.println("Enter the size of the array");
int n = sc.nextInt();
System.out.println("Enter the elements of the array");
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = sc.nextInt();
}
System.out.println("Enter the sorting algorithm to be used");
System.out.println("1. Selection Sort");
System.out.println("2. Bubble Sort");
System.out.println("3. Insertion Sort");
System.out.println("4. Merge Sort");
System.out.println("5. Quick Sort");
int choice = sc.nextInt();
switch (choice) {
case 1:
selectionSort(arr);
break;
case 2:
bubbleSort(arr);
break;
case 3:
insertionSort(arr);
break;
case 4:
mergeSort(arr, 0, n - 1);
break;
case 5:
quickSort(arr, 0, n - 1);
break;
default:
System.out.println("Invalid choice");
break;
}
System.out.println("The sorted array is");
printArray(arr);
t--;
}
}
}
}