
Database System Concepts
7th Edition
ISBN: 9780078022159
Author: Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher: McGraw-Hill Education
expand_more
expand_more
format_list_bulleted
Question

Transcribed Image Text:2.2-3
Consider linear search again (see Exercise 2.1-3). How many elements of the in-
put sequence need to be checked on the average, assuming that the element being
searched for is equally likely to be any element in the array? How about in the
worst case? What are the average-case and worst-case running times of linear
search in O-notation? Justify your answers.
![2.2-2
Consider sorting n numbers stored in array A by first finding the smallest element
of A and exchanging it with the element in A[1]. Then find the second smallest
element of A, and exchange it with A[2]. Continue in this manner for the first n - 1
elements of A. Write pseudocode for this algorithm, which is known as selection
sort. What loop invariant does this algorithm maintain? Why does it need to run
for only the first n − 1 elements, rather than for all n elements? Give the best-case
and worst-case running times of selection sort in O-notation.](https://content.bartleby.com/qna-images/question/032de2f6-d35c-4a3a-a9a2-daefbabd8c32/ddb2e22a-bbe1-4c40-aaa7-7749b1f3eb9c/8jdt33t_thumbnail.png)
Transcribed Image Text:2.2-2
Consider sorting n numbers stored in array A by first finding the smallest element
of A and exchanging it with the element in A[1]. Then find the second smallest
element of A, and exchange it with A[2]. Continue in this manner for the first n - 1
elements of A. Write pseudocode for this algorithm, which is known as selection
sort. What loop invariant does this algorithm maintain? Why does it need to run
for only the first n − 1 elements, rather than for all n elements? Give the best-case
and worst-case running times of selection sort in O-notation.
Expert Solution

This question has been solved!
Explore an expertly crafted, step-by-step solution for a thorough understanding of key concepts.
This is a popular solution
Trending nowThis is a popular solution!
Step by stepSolved in 2 steps

Knowledge Booster
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.Similar questions
- discrete matharrow_forward= Exercise 2. Present an O(n) algorithm that sorts n positive integer numbers a1, a2, • An which are known to be bounded by n² - 1 (so 0 ≤ ai ≤ n² − 1, for every i 1,..., n. Use the idea of Radix Sort (discussed in class and presented in Section 8.3 in the textbook). Note that in order to obtain O(n) you have to do Radix Sort by writing the numbers in a suitable base. Recall that the runtime of Radix Sort is O(d(n+k)), where d is the number of digits, and k is the base, so that the number of digits in the base is also k. The idea is to represent each number in a base k chosen so that each number requires only 2 "digits," so d = 2. Explain what is the base that you choose and how the digits of each number are calculated, in other words how you convert from base 10 to the base. Note that you cannot use the base 10 representation, because n² – 1 (which is the largest possible value) requires log₁0 (n² - 1) digits in base 10, which is obviously not constant and therefore you would not…arrow_forwardQuicksort SPLIT (the 2-pointer algorithm covered in class) is applied to the integer array [4,3,5,7,9,2,1], using the first entry as the pivot. Show the series of item swaps that are performed in the split process, and the array after each of these swaps, up to and including the step of placing the pivot at its correct location. You only need to show the split of the original array, you are not required to continue working on the subarrays after the split.arrow_forward
- Consider an array of integers. Write the pseudocode in Java for either the selection sort, insertion sort, or bubble sort algorithm. Include loop invariants in your pseudocode. I need help with the above instruction for the bubble sort algorithm, please. I appreciate your help. Thank you.arrow_forwardDetermine how many times the innermost loop will be iterated when the following algorithm segment is implemented and run. (Assume that m and n are positive integers.) for j:=1 to m for k:=1 to n [Statements in body of inner loop. None contain branching statements that lead outside the loop.] next k next j (Hint: See Example 9.2.5 in the "Read It" link.) For each iteration of the outer loop there are X iterations of the inner loop. Therefore, the answer is H Xarrow_forwardGiven an array of positive and negative integers, write the main ideas of an algorithm to segregate them without changing the relative order of elements. The output should contain all positive numbers follow negative numbers while maintaining the same relative ordering. Example: Input: [9, -3, 5, -2, -8, -6, 1, 3] Output: [-3, -2, -8, -6, 9, 5, 1, 3] Determine the running time complexity of this algorithmarrow_forward
- Prime numbers can also be generated by an algorithm known as the Sieve of Eratosthenes . The algorithm for this procedure is presented here. Write a program that implements this algorithm so as to find all prime numbers up to n = 150. Sieve of Eratosthenes Algorithm To Display All Prime Numbers Between 1 and n Step 1: Define an array of integers P . Set all elements P i to 0, 2 <= i <= n. Step 2: Set i to 2. Step 3: If i > n , the algorithm terminates. Step 4: If P i is 0 (value i is a prime number), then i is prime and display its value. Step 5: For all positive integer values of j , such that i * j ≤ n , set P i ∗ j to 1 (non - prime). Step 6: Add 1 to i and go to step 3arrow_forwardin javaarrow_forwardQuicksort is a powerful divide-and-conquer sorting algorithm that can be described in just four lines ofpseudocode. The key to Quicksort is the PARTITION(A, p, r) procedure, which inputs elementsptorof array A,and chooses the final element x = A[r] as the pivot element. The output is an array where all elementsto the left ofxare less thanx, and all elements to the right of x are greater than x. In this question, we will use the Lomuto Partition Method from class and assume that the pivot isalwaysthe last (right-most) element of the input array. Question: Let A be an array withn= 2k−1 elements, where k is some positive integer. Determine a formula (in terms of n) for the minimum possible number of total comparisons required by Quicksort, as well as a formula for the maximum possible number of total comparisons required by Quicksort. Use your formulas to show that the running time of Quicksort is O(nlogn) in the best case and O(n2) in the worst case.arrow_forward
- Consider sorting n numbers stored in array A[1:n] by first finding the smallestelement of A[1:n] and exchanging it with the element in A[1]. Then find thesmallest element of A[2:n], and exchange it with A[2]. Then find the smallestelement of A[3:n], and exchange it with A[3]. Continue in this manner for thefirst n-1 elements of A. Write pseudocode for this algorithm, which is knownas selection sort. What loop invariant does this algorithm maintain? Why does itneed to run for only the first n-1 elements, rather than for all n elements? Give theworst-case running time of selection sort in big theta notation. Is the best-case runningtime any better?arrow_forwardRegarding familiarity with operating systems, what benefits does understanding assembly language provide?arrow_forwardWrite a program to sort an array of random elements using quicksort as follows: Sort the arrays using pivot as the first element of the array Sort the arrays using pivot as the median of the first, last, and middle elements of the array Sort the arrays using pivot as the first element of the array. However,, when the size of any sub-list reduces to less than 20, sort the sub-list using insertion sort. Sort the array using pivot as the median of the first, last and middle elements of the array. When the size of any sub-list reduces to less than 20, sort the sub-list using insertion sort. Calculate and display the CPU time for each of the preceding four steps. Example of the median of the first, last and middle elements: 1 2 3 4 5 6 7 8 0 (median of 1, 5, 0 is 1) 8 0 1 2 3 4 5 6 7 (median of 8, 3, 7 is 7) To calculate the CPU time, use the header , and clock_t type. You need to cast the duration to float and divide it by CLOCKS_PER_SEC. Depends on the CPU of your computer, your…arrow_forward
arrow_back_ios
SEE MORE QUESTIONS
arrow_forward_ios
Recommended textbooks for you
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education

Database System Concepts
Computer Science
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:McGraw-Hill Education

Starting Out with Python (4th Edition)
Computer Science
ISBN:9780134444321
Author:Tony Gaddis
Publisher:PEARSON

Digital Fundamentals (11th Edition)
Computer Science
ISBN:9780132737968
Author:Thomas L. Floyd
Publisher:PEARSON

C How to Program (8th Edition)
Computer Science
ISBN:9780133976892
Author:Paul J. Deitel, Harvey Deitel
Publisher:PEARSON

Database Systems: Design, Implementation, & Manag...
Computer Science
ISBN:9781337627900
Author:Carlos Coronel, Steven Morris
Publisher:Cengage Learning

Programmable Logic Controllers
Computer Science
ISBN:9780073373843
Author:Frank D. Petruzella
Publisher:McGraw-Hill Education