Respuesta :
Answer:
Explanation:
The following code is written in Java. It creates a method for each of the sorting algorithms and one method to reset the array and print the original, so that all algorithms can be tested with the same array.The entire program code is below.
import java.util.Scanner;
class Brainly {
static int[] arr;
public static void main(String[] args) {
// Print Unsorted Array and Call Selection Sort and print
resetArray();
SelectionSorter(arr);
System.out.print("\nSelection Sort Array: ");
for (int x : arr) {
System.out.print(x + ", ");
}
//Reset Array and call Bubble Sort and print
System.out.println('\n');
resetArray();
BubbleSorter(arr);
System.out.print("\nBubble Sort Array: ");
for (int x : arr) {
System.out.print(x + ", ");
}
//Reset Array and call Insert Sort and print
System.out.println('\n');
resetArray();
InsertSorter(arr);
System.out.print("\nInsert Sort Array: ");
for (int x : arr) {
System.out.print(x + ", ");
}
}
public static void resetArray() {
arr = new int[]{10, 14, 28, 11, 7, 16, 30, 50, 25, 18};
System.out.print("Unsorted Array: ");
for (int x : arr) {
System.out.print(x + ", ");
}
}
public static void SelectionSorter(int[] arr) {
for (int i = 0; i < arr.length - 1; i++) {
int index = i;
for (int j = i + 1; j < arr.length; j++) {
if (arr[j] < arr[index]) {
index = j;//searching for lowest index
}
}
int smallerNumber = arr[index];
arr[index] = arr[i];
arr[i] = smallerNumber;
}
}
static void BubbleSorter(int[] arr) {
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]) {
//swap elements
temp = arr[j - 1];
arr[j - 1] = arr[j];
arr[j] = temp;
}
}
}
}
public static void InsertSorter(int arr[]) {
int n = arr.length;
for (int j = 1; j < n; j++) {
int key = arr[j];
int i = j - 1;
while ((i > -1) && (arr[i] > key)) {
arr[i + 1] = arr[i];
i--;
}
arr[i + 1] = key;
}
}
}
![Ver imagen sandlee09](https://us-static.z-dn.net/files/dd3/d64ddea02addd3d085a560ff21e144b3.jpg)