Question

C++ Suppose that L is a list is of length n and it is sorted using...

C++

Suppose that L is a list is of length n and it is sorted using insertion sort. If L is already sorted in the reverse order, show that the number of comparisons is (1/2)(n2 – n) and the number of item assignments is (1/2)(n2 +3n) – 2.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

ANSWER:

GIVEN THAT:

NUMBER OF COMPARISONS AND ITEM ASSIGNMENTS:

Given that:

1. The length of the list L is n and is sorted in reverse order using insertion sort. In order to sort the list which are already reverse ordered we have to move the first element to n, 2ed element to n-1 and so on.
2. To move the first element to n using insertion sort n-1 comparisons are performed. Similarly to move the next higher element to position n-1, n-2 comparisons are performed and so on.

Therefore, to sort the n elements then the number of comparisons:

(n-1) + (n-2) + (n-3) +......... + 2 + 1 = n (n-1) /2
=(1/2) (n^2-n)

3. While sorting the reverse ordered elements using insertion sort 2(n-1). (n-1) + (n-2) + (n-3) + + 1 item assignments are performed.

But (n-1) + (n-2) + .......+ 2 + 1 = (1/2) (n2-n)

Therefore the number item assignments = 2(n-1) + (1/2) (n2-n)

= (n2 + 3n — 4)/2

=(1/2) (n^+3n)-2

Add a comment
Know the answer?
Add Answer to:
C++ Suppose that L is a list is of length n and it is sorted using...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • C++ Sorting and Searching 1. Mark the following statements as true or false. a. A sequential...

    C++ Sorting and Searching 1. Mark the following statements as true or false. a. A sequential search of a list assumes that the list elements are sorted in ascending order. b. A binary search of a list assumes that the list is sorted. 2. Consider the following list: 63 45 32 98 46 57 28 100 Using a sequential search, how many comparisons are required to determine whether the following items are in the list or not? (Recall that comparisons...

  • Need help with program. I'm stuck Objectives: In this assignment, we will practice manipulating lists of...

    Need help with program. I'm stuck Objectives: In this assignment, we will practice manipulating lists of data and arranging items in an ascending/descending order. you will also explore storing lists/arrays using different sorting algorithms including, the selection sort, bubble sort, and insertion sort algorithm. Comparison between the three algorithms are made based on the number of comparisons and item assignments (basic operations) each algorithms executes. Background: Ordering the elements of a list is a problem that occurs in many computer...

  • 7. (20 points) Assume L is a list, and assume that int n=L length() returns the number of element...

    7. (20 points) Assume L is a list, and assume that int n=L length() returns the number of elements in the list, and Bubblsort(L, 0,i) sorts the list from 0 to i usin the g the Bubble sort algorithm. Determine asymtotic running time as function of n, e(T(n), for the average case time for the following code fragments a) for( int i = 1;i < n; i* 2) Bubblsort (L,0,i); for( int i=0;i 7. (20 points) Assume L is a...

  • Puodace a char showing the number of moves required to solve the Towers of Hanoi puzle using t 30...

    Puodace a char showing the number of moves required to solve the Towers of Hanoi puzle using t 30. What is an execution frame? What is an activation record? What is contained in it? 31. Write a recursive Java method that computes the factorial of a number 2. Linear search may require up to comparisons while binary search will only require roughly comparisons 33. Sort sorts a list of values by repetitively putting a particular value into its final, sorted,...

  • 1. Consider an array of n distinct values in which the first n − 1 values are sorted, and the las...

    1. Consider an array of n distinct values in which the first n − 1 values are sorted, and the last element is not. (It could be smaller than the first element, larger than element n − 1, or anywhere in between.) Give the worst-case number of comparisons that Insertion Sort will perform in this scenario. You can give your answer in terms of big-Theta if you wish to ignore low-order terms and constants.

  • Discrete Mathematics Unsorted and Sorted Lists For linear search there was no requirement for the list...

    Discrete Mathematics Unsorted and Sorted Lists For linear search there was no requirement for the list to be organized in any manner. The linear search works for lists that are "unsorted." But what if the values in the list are given in ascending order? This would be a sorted list. With a sorted list, is there a more efficient way to find the target? Unsorted Lists (4 pts) Assume there is a sorting algorithm with order of growth O(n), where...

  • For a list of length n, insertion sort makes _key comparisons, in the worst case. None...

    For a list of length n, insertion sort makes _key comparisons, in the worst case. None of these O(nlogzn) On O) O() Question 20 The time complexity of the quick sort is in the worst case and in the average case O), O) O(nlogon), O(nlogon) (12), O(nlog.) O(nlogon). 0() O(P), (n)

  • Please provide explanation. Suppose you are given a sorted list of N elements followed by f(N)...

    Please provide explanation. Suppose you are given a sorted list of N elements followed by f(N) randomly ordered values. How would you sort the entire list if: (a) f(N) = N/20 (b) f(N) = O(log N) (c) f(N) = O( √ N)

  • Java We will look at the behavior of three different sorts on randomly generated lists of...

    Java We will look at the behavior of three different sorts on randomly generated lists of different sizes The three sorts are bubblesort, insertion sort, and quicksort. Randomly generate integers in the range 0-99 for your random numbers. You may choose your own random number generation technique (this is an exception to the no-outside-help requirement for this assignment. You must document the source of your random number generation in the code. ° Here is what your code should do: 1....

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT