Question

Do problem 4-20: Give an efficient algorithm to rearrange an array of n keys so that...

Do problem

4-20: Give an efficient algorithm to rearrange an array of n keys so that all the negative keys precede all the nonnegative keys. Your algorithm must be in-place, meaning you cannot allocate another array to temporarily hold the items. How fast is your algorithm?

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

This problem can be solved in O(N) time.

We need two pointers, I and J starting from left and right. If Element from left is positive and Element from Right is Negative, then, we need to swap those two elements

FUN REARRANGE(A[])
   N = LEN(A)
   I = 0
   J = N-1
   WHILE I<J
       WHILE I<N && A[I]<0
           I++
       WHILE J>=0 && A[J]>0
           J--
       IF I<N && J>=0
           T = A[I]
           A[I] = A[J]
           A[J] = T

Add a comment
Know the answer?
Add Answer to:
Do problem 4-20: Give an efficient algorithm to rearrange an array of n keys so that...
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
  • 6. Give an efficient algorithm to rearrange an array of n keys so that all negative...

    6. Give an efficient algorithm to rearrange an array of n keys so that all negative keys precede all nonne- gative keys. Your algorithm must be in-place meaning you cannot allocate another array to temporarily hold the items.

  • Design an algorithm to rearrange elements of a given array of n real numbers so that...

    Design an algorithm to rearrange elements of a given array of n real numbers so that all its negative elements precede all its positive elements. Your algorithm should be both time efficient and space efficient. (run the code in a programming language and present the running result)

  • Suppose you have an array of n elements containing three distinct keys, true, false, and maybe....

    Suppose you have an array of n elements containing three distinct keys, true, false, and maybe. Give an O(n) algorithm to rearrange the list so that all false elements precede the maybe elements, which in turn precede all true elements. You may use only constant extra space.

  • Let T be a heap storing n keys. Give an efficient algorithm for reporting all the...

    Let T be a heap storing n keys. Give an efficient algorithm for reporting all the keys in T that are smaller than or equal to a given query key x (which is not necessarily in T). For example, given the heap of Figure 5.6 and query key x = 7, the algorithm should report 4, 5, 6, 7. Note that the keys do not need to be reported in sorted order. Ideally, your algorithm should run in O(k) time,...

  • Subject: Algorithm need this urgent please thank you. 4. Give pseudocode for an algorithm that will solve the following problem. Given an array A[1..n) that contains every number between 1 and n +...

    Subject: Algorithm need this urgent please thank you. 4. Give pseudocode for an algorithm that will solve the following problem. Given an array A[1..n) that contains every number between 1 and n +1 in order, except that one of the numbers is missing. Find the miss sorted ing mber. Your algorithm should run in time (log n). (Hint: Modify Binary search). A pseudocode means an algorithm with if statements and loops, etc. Don't just write a paragraph. Also, if your...

  • Problem 3 (20 points): An array A of size (n) contains floating-point items. 1. Implement a...

    Problem 3 (20 points): An array A of size (n) contains floating-point items. 1. Implement a Divide & Conquer algorithm to return the number of items with values less than a given value (x). (5 points) 2. Test your algorithm by generating an array A of size n = 1024 random floating-point numbers with each number R between 0 and 1, i.e. 0.0 <R< 1.0. Run the algorithm to find the percentage of the numbers in the array with values...

  • 5. (20 pts.) Copper Pipes Bubbles has a copper pipe of length n inches and an...

    5. (20 pts.) Copper Pipes Bubbles has a copper pipe of length n inches and an array of nonnegative integers that contains prices of all pieces of size smaller than n. He wants to find the maximum value he can make by cutting up the pipe and selling the pieces. For example, if length of the pipe is 8 and the values of different pieces are given as following, then the maximum obtainable value is 22 (by cutting in two...

  • The zigzag sorting problem takes an array data of size n and outputs a per- mutation...

    The zigzag sorting problem takes an array data of size n and outputs a per- mutation where data[1] <data[2] > data[3] = data[4] > data[5] S..., all the way to the end of the array. (In general data[i] = data[i+1] if i is odd and data[i] > data[i+1] if i is even.) Answer the following questions about developing algorithms for the zigzag sorting problem. 1. Give pseudocode for a brute force algorithm for zigzag sorting. Your pseudocode just needs to...

  • Problem Definition: Problem: Given an array of integers find all pairs of integers, a and b,...

    Problem Definition: Problem: Given an array of integers find all pairs of integers, a and b, where a – b is equal to a given number. For example, consider the following array and suppose we want to find all pairs of integers a and b where a – b = 3 A = [10, 4, 6, 16, 1, 6, 12, 13] Then your method should return the following pairs: 4, 1 15, 12 13, 10 A poor solution: There are...

  • Analysis of Algorithms Fall 2013 Do any (4) out of the following (5) problems 1. Assume n-3t is a...

    Analysis of Algorithms Fall 2013 Do any (4) out of the following (5) problems 1. Assume n-3t is a power of 3 fork20. Solve accurately the following recursion. If you cannot find the exact solution, use the big-O notation. Tu) T(n)Tin/3)+2 2. Suppose that you have 2 differeut algorithms to solve a giveu probleen Algorithm A has worst-case time complexity e(n2) and Algorithm B has worst-case time complexity e(nlog n). Which of the following statements are true and which are...

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