Write a client program Client Sorting and in the main method:
1. Call a method, SelectionSorter() that accepts an integer array as a parameter and sorts the elements in the array using the selection sort algorithm where it picks the maximum value in the array in each pass. Print the array before it is sorted in the main method, then after it is sorted in SelectionSorter().
2. Call a method, BubbleSorter that accepts an integer array as a parameter and sorts the elements in the array in descending order (highest number first, then the second highest and so on) using the bubble sort algorithm. Print the array before it is sorted in the main method, then after it is sorted in BubbleSortero.
3. Call a method, InsertSorter() that accepts an integer array as a parameter and sorts the elements in the array using the insertion sort algorithm. Print the array before it is sorted in the main method, then after it is sorted in InsertSortero.

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
ACCESS MORE