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
Please identify three examples of how the United States is heteronormative
juin [17]

Answer:

Well, let's say you're out at a bar. A friend of yours sees a cute guy, but she's hesitant to make the first move because she assumes the guy would be turned off by that - because we're socialized to believe that women shouldn't approach men like that, lest they come off as desperate. That's heteronormativity.

When she finally gets the courage to go up and talk to him, he begins to call her infantilizing pet names (like honey, baby, and sweetheart) - which would be fine if they'd agreed upon it, but he's just assuming she's cool with it! That's heteronormativity.

When she begins to get uncomfortable by these names, she gets up and leaves. But the guy says something like, "Oh, come on! Women are so sensitive. What's your problem?" That's heteronormativity.

5 0
3 years ago
Marco makes $65,000 a year as a graphic designer. He makes a charitable donation of $1,000 each year. He also has $5,000 of busi
iogann1982 [59]
The correct answer is $59,000. 
5 0
3 years ago
Read 2 more answers
Which one of the following is considered a peripheral? A Software B Mouse C USB connector D Motherboard
Bogdan [553]

A mouse is considered that out of the options listed.

5 0
3 years ago
Read 2 more answers
what is a paragraph on what i can write on what i learned from watching basic keyboard shortcuts using control functions video
finlep [7]

Answer:

Write the following nonsensical paragraph:

Explanation:

I learned how to navigate the computer efficiently. I learned how to use Ctrl + (key) to quickly do (something). ( Now reword that multiple times with different commands. ) Now I can harness my inner laziness to browse the computer at a faster speed.

6 0
3 years ago
You are provided with several client program classes that implement a graphical simulation of a 2D world of animals. You will wr
NARA [144]
Https://zoo.cs.yale.edu/classes/cs112/2012-spring/as/as9/ visit this website it gives you the answers
4 0
3 years ago
Other questions:
  • When sociologists say that it is difficult to predict all the results of social change, what are they referring to?
    5·1 answer
  • To add and remove chart elements, you can use the add chart element button in the charts layout group on the ____ tab.
    6·1 answer
  • What was the first carbonated drink to be introduced in the US?
    6·1 answer
  • What two different passwords can be set to lock down a computer from unauthorized access?
    13·1 answer
  • How does form get its power natural gas
    14·1 answer
  • Describe the positive and negative effects of Internet​
    6·2 answers
  • What does a spam e-mail normally promise you?
    9·1 answer
  • Im bored, any anime recommendations? i probably watched whatever youre gonna say
    14·2 answers
  • Write a test program that creates two Rectangle objects—one with width 4 and height 40 and the other with width 3.5 and height 3
    5·1 answer
  • Speed(?)
    5·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!