1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
Yuki888 [10]
3 years ago
14

Assume the existence of an UNSORTED ARRAY of n characters. You are to trace the CS111Sort algorithm (as described here) to reord

er the elements of a given array. The CS111Sort algorithm is an algorithm that combines the SelectionSort and the InsertionSort following the steps below: 1. Implement the SelectionSort Algorithm on the entire array for as many iterations as it takes to sort the array only to the point of ordering the elements so that the last n/2 elements are sorted in increasing (ascending) order. 2. Implement the InsertionSort Algorithm to sort the first half of the resulting array elements so that these elements are sorted in decreasing (descending) order.
Computers and Technology
1 answer:
makvit [3.9K]3 years ago
6 0

Answer:

class Main {

  public static void main(String[] args) {

      char arr[] = {'T','E','D','R','W','B','S','V','A'};

      int n = arr.length;

      System.out.println("Selection Sort:");

      System.out.println("Iteration\tArray\tComparisons");

      long comp1 = selectionSort(arr);

      System.out.println("Total comparisons: "+comp1);

      System.out.println("\nInsertion Sort:");

      System.out.println("Iteration\tArray\tComparisons");

      long comp2 = insertionSort(arr);

      System.out.println("Total Comparisons: "+comp2);

      System.out.println("\nOverall Total Comparisons: "+(comp1+comp2));

  }

  static long selectionSort(char arr[]) {

      // applies selection sort for n/2 elements

      // returns number of comparisons

      int n = arr.length;

      long comparisons = 0;

 

      // One by one move boundary of unsorted subarray

      for (int i = n-1; i>=n-n/2; i--) {

              // Find the minimum element in unsorted array

              int max_idx = i;

              for (int j = i-1; j>=0; j--) {

                      // there is a comparison everytime this loop returns

                      comparisons++;

                      if (arr[j] > arr[max_idx])

                              max_idx = j;

              }

              // Swap the found minimum element with the first

              // element

              char temp = arr[max_idx];

              arr[max_idx] = arr[i];

              arr[i] = temp;

              System.out.print(n-1-i+"\t");

              printArray(arr);

              System.out.println("\t"+comparisons);

      }

     

      return comparisons;

  }

  static long insertionSort(char arr[]) {

      // applies insertion sort for n/2 elements

      // returns number of comparisons

      int n = arr.length;

      n = n-n/2;   // sort only the first n/2 elements

      long comparisons = 0;

      for (int i = 1; i < n; ++i) {

          char key = arr[i];

          int j = i - 1;

          /* Move elements of arr[0..i-1], that are

                  greater than key, to one position ahead

                  of their current position */

          while (j >= 0) {

              // there is a comparison everytime this loop runs

              comparisons++;

              if (arr[j] > key) {

                  arr[j + 1] = arr[j];

              } else {

                  break;

              }

              j--;

          }

          arr[j + 1] = key;

          System.out.print(i-1+"\t");

          printArray(arr);

          System.out.println("\t"+comparisons);

      }

      return comparisons;

  }  

  static void printArray(char arr[]) {

      for (int i=0; i<arr.length; i++)

          System.out.print(arr[i]+" ");

  }

}

Explanation:

Explanation is in the answer.

You might be interested in
1. How fast do human beings walk?
zepelin [54]

Answer:

a.about 3 or 4 miles per hour

8 0
3 years ago
Read 2 more answers
PLS HURRY!!!<br> Look at the image below!
max2010maxim [7]

Answer:

Ctrl+c

Explanation:

6 0
3 years ago
Read 2 more answers
Mail merge documents use ______ to represent personal information that stored in a table.
Mice21 [21]
Mandatory
Mark brainliest please
6 0
3 years ago
Listening to the audience refers to what in the context of slide presentations
Anit [1.1K]

Answer: D

Explanation: This concept refers to listening for various cues, such as confusion, interest, or boredom.

8 0
3 years ago
Read 2 more answers
WHAT IS A GOOD APP FOR REMOVING VIRUSES AND IT YOU DONT HAVE TO PAY MUCH FOR IT ????? PLEASE HELP ME
pishuonlain [190]

Answer:

Best free virus removal and free malware removal tools

Avira Free Antivirus – Offers a larger package of free security tools than most competitors, including real-time AV, malware removal, and a VPN. Bitdefender Antivirus Free Edition: Award-winning free version

8 0
3 years ago
Read 2 more answers
Other questions:
  • Points Possible: 6, Points Correct: 4
    6·1 answer
  • The most important protocol at the internet layer is ____.
    13·1 answer
  • How do careers of firefighters and police officer death fire in what ways are they similar​
    6·1 answer
  • 20 POINTS!
    15·1 answer
  • Select the correct answer.
    13·2 answers
  • A _____________ is designed for a individual user.
    9·1 answer
  • Rewrite the following using if else statement:
    14·1 answer
  • Take a minute to reflect on your thoughts and learning so far and discuss:
    13·1 answer
  • What are placeholders in Java?
    8·1 answer
  • What is the relationship between the binary number system and computer hardware?
    9·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!