Question
Discrete math question

2. Consider to the following two algorithms procedure SortA(a1,a2, ..., an: a list of real numbers with n 2 2) 1, for j := 2 to n 2. i:= 1 3. while aj > ai 4. 5. m: 6. or k 0toj -i-1 7. i:-i+1 aj-k:aj-k-1 ai := m
media%2F185%2F185eab87-3132-4979-93d3-e5
0 0
Add a comment Improve this question Transcribed image text
Answer #1
comparisons are made in while loop.
This is an example of insertion sort.

number of comparisons for j=2 is 1
number of comparisons for j=3 is 2
number of comparisons for j=4 is 3
...
number of comparisons for j=n is n-1

total number of comparisons = 1+2+3+...+(n-1)
=\sum_{i=1}^{n-1}1

formula\;for\;sum\;of\;first\;n-1\;natural\;numbers\;is\;\frac{n(n-1)}{2}

so,\;total\;number\;of\;comparisons=\frac{n(n-1)}{2}

Add a comment
Know the answer?
Add Answer to:
Discrete math question 2. Consider to the following two algorithms procedure SortA(a1,a2, ..., an: a list...
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
  • 28. Show that there are 12 pairs of numbers (a1,az) with 0<aj < 4,0 <a2 <6...

    28. Show that there are 12 pairs of numbers (a1,az) with 0<aj < 4,0 <a2 <6 so that x=a1 (mod 4) x = 22 (mod 6) has a solution.

  • The input consists of n numbers a1, a2, . . . , an and a target...

    The input consists of n numbers a1, a2, . . . , an and a target value t. The goal is to determine in how many possible ways can we add up two of these numbers to get t. Formally, your program needs to find the number of pairs of indices i, j, i < j such that ai+aj = t. For example, for 2, 7, 3, 1, 5, 6 and t = 7, we can get t in two...

  • Let n be a positive integer. We sample n numbers a1, a2,..., an from the set...

    Let n be a positive integer. We sample n numbers a1, a2,..., an from the set {1,...,n} uniformly at random, with replacement. We say that picks i and j with are a match if ai = aj, i < j. What is the expected total number of matches? Use indicators.

  • float useless(A){ n = A.length; if (n==1) { return A[@]; let A1,A2 be arrays of size...

    float useless(A){ n = A.length; if (n==1) { return A[@]; let A1,A2 be arrays of size n/2 for (i=0; i <= (n/2)-1; i++){ A1[i] = A[i]; A2[i] = A[n/2 + i]; for (i=0; i<=(n/2)-1; i++){ for (j=i+1; j<= (n/2)-1; j++){ if (A1[i] == A2[j]) A2[j] = 0; b1 = useless(A1); b2 = useless (A2); return max(b1,b2); What is the asymptotic upper bound of the code above?

  • 2 Double summation Let a1, A2, A3, ... be a sequence of real numbers, and let...

    2 Double summation Let a1, A2, A3, ... be a sequence of real numbers, and let n > 1 be an integer. Which of the following are always equal? пі пп nn nn « ££« Žia. E£« L« i=1 j=1 i=1 j=1 i= 1 i=1 j=1 j=1 i=j

  • (C programming) Given a sequence of numbers a1, a2, a3, ..., an, find the maximum sum...

    (C programming) Given a sequence of numbers a1, a2, a3, ..., an, find the maximum sum of a contiguous subsequence of those numbers. Note that, a subsequence of one element is also a contiquous subsequence. Input The input consists of multiple datasets. Each data set consists of: n a1 a2 . . an You can assume that 1 ≤ n ≤ 5000 and -100000 ≤ ai ≤ 100000. The input end with a line consisting of a single 0. Output...

  • You are given a set of integer numbers A = {a1, a2, ..., an}, where 1...

    You are given a set of integer numbers A = {a1, a2, ..., an}, where 1 ≤ ai ≤ m for all 1 ≤ i ≤ n and for a given positive integer m. Give an algorithm which determines whether you can represent a given positive integer k ≤ nm as a sum of some numbers from A, if each number from A can be used at most once. Your algorithm must have O(nk) time complexity.

  • Question 1: Complexity Take a look at the following algorithm written in pseudocode: procedure mystery(a1, a2,...

    Question 1: Complexity Take a look at the following algorithm written in pseudocode: procedure mystery(a1, a2, …, an: integer) i := 1 while (i < n and ai ≤ ai+1) i := i + 1 if i == n then print “Yes!” else print “No!” What property of the input sequence {an} does this algorithm test? What is the computational complexity of this algorithm, i.e., the number of comparisons being computed as a function of the input size n? Provide...

  • Question 2 7 pts Theorem If A1, A2, .., A, are sets for n > 2,...

    Question 2 7 pts Theorem If A1, A2, .., A, are sets for n > 2, then (A, UA, U... A.) = (A) n(A)n... n(A) Upload Choose a File Question 3 6 pts o el DLL

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