Question

Consider the following problem Input: array of integers. Check, whether it is sorted in descending order How to solve this problem in parallel? Propose an algorithm Calculate its work, span and parallelism
0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include<iostream>
#include<climits>
using namespace std;
int main()
{
int i,n;
cout<<"Enter size of array : ";
cin>>n;
int arr[n];
cout<<"Enter "<<n<<" elements : ";
for(i=0;i<n;i++)
cin>>arr[i];
int prev=INT_MAX;
for(i=0;i<n;i++)
{
if(arr[i]<prev)
{
prev=arr[i];
continue;
}
else
break;
}
if(i==n)
cout<<"Array is in descending order\n";
else
cout<<"Array not in descending order\n";
}

Enter size of array 5 Enter 5 elements 5 4 3 2 1 rray is in descending order Process returned θ (0x0) execution time : 8.585

/*Solution in parallelism, that is to check whether an array of integers are sorted in descending order or not

without storing the elements in array

*/

function main()

{

read n //where n denotes the size of the array

prev=MAX // prev is an integer variable which stores previous visited value of the input

for i=0 to n-1

read currentValue //val is an integer variable that stores current value of the input

if(currentValue<prev) //If previous value is greater than the current value then array is sorted upto this point

prev=currentValue //Update prev value to compare it with the next input

continue;

else

break;

//If the for loop iteration iterated n times

//then array is in descending order otherwise not

if(i==n)

print "Array is in descending order"

else

print "Array is not in descending order"

}

Parallelism:

Since the elements are not stored in a array it is evaluated for the task while taking input that is it is solved using parallelism.

Work:

Since the loop is iterating n times so the work span is O(n)

Add a comment
Know the answer?
Add Answer to:
Consider the following problem Input: array of integers. Check, whether it is sorted in descending order...
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
  • The input is an array of N integers ( sorted ) and an integer X. The...

    The input is an array of N integers ( sorted ) and an integer X. The algorithm returns true if X is in the array and false if X is not in the array. Describe an algorithm that solves the problem with a worst case of O(log N) time.

  • Consider the following problem: Input: a list of n-1 integers and these integers are in the...

    Consider the following problem: Input: a list of n-1 integers and these integers are in the range of 1 to n. There are no duplicates in list. One of the integers from 1 to n is missing in the list. Output: find the missing integer Let the input array be [2, 4, 1, 6, 3, 7, 8]. Elements in this list are in the range of 1 to 8. There are no duplicates, and 5 is missing. Your algorithm needs...

  • You are given an array A of integers in sorted order. However, you do not know...

    You are given an array A of integers in sorted order. However, you do not know the length n of the array. Assume that in our programming language arrays are implemented in such a way that you receive an out-of-bounds error message whenever you wish to access an element A[i] with i>n. For simplicity we assume that the error message simply returns the value INT_MAX and that every value in the array is smaller than INT_MAX. (a) Design an algorithm...

  • Given two arrays A and B of n integers both of which are sorted in ascending...

    Given two arrays A and B of n integers both of which are sorted in ascending order. Write an algorithm to check whether or not A and B have an element in common. Find the worst case number of array element comparisons done by this algorithm as a function of n and its Big-O complexity

  • Suppose that we are given a sorted array of distinct integers A[1, ......,  n] and we want...

    Suppose that we are given a sorted array of distinct integers A[1, ......,  n] and we want to decide whether there is an index i for which A[i] = i. Describe an efficient divide-and-conquer algorithm that solves this problem and explain the time complexity. 1. Describe the steps of your algorithm in plain English. 2. Write a recurrence equation for the runtime complexity. 3. Solve the equation by the master theorem.

  • 1. Please write a Divide-and-Conquer Java algorithm solving the following problem: Given an "almost sorted" array...

    1. Please write a Divide-and-Conquer Java algorithm solving the following problem: Given an "almost sorted" array of distinct integers, and an integer x, return the index of x in the array. If the element x is not present in the array, return -1. "Almost sorted" means the following. Assume you had a sorted array A[0…N], and then split it into two pieces A[0…M] and A[M+1…N], and move the second piece upfront to get the following: A[M+1]…A[N]A[0]…A[M]. Thus, the "almost sorted"...

  • Write a program in MIPS assembly language that implements the DESCENDING bubble sort algorithm to sort a variable-sized array of signed 32-bit integers

    Write a program in MIPS assembly language that implements the DESCENDING bubble sort algorithm to sort a variable-sized array of signed 32-bit integers (words)that are read from the console. Be reminded that in a descending sort, the integers are sorted from the largest to the smallest. A “special value” 99999 will beused to signify the end of the input sequence. This value is not to be considered part of the input data set. However, any value greater than 99999 that...

  • 6. Consider the following basic problem. You're given an array A consisting of n integers A[1],...

    6. Consider the following basic problem. You're given an array A consisting of n integers A[1], A[2], , Aln]. You'd like to output a two-dimensional n-by-n array B in which B[i, j] (for i <j) contains the sum of array entries Ali] through Aj]-that is, the sum A[i] Ai 1]+ .. +Alj]. (The value of array entry B[i. Λ is left unspecified whenever i >j, so it doesn't matter what is output for these values.) Here's a simple algorithm to...

  • Please use Java recursion knowledge to solve. Given the following sorted array of integers: 1 7...

    Please use Java recursion knowledge to solve. Given the following sorted array of integers: 1 7 8 11 15 22 38 What are the "middle" elements that the binary search algorithm would examine/compare to as it divides the array into sections when looking for the number 7? 1 7 8 11 15 22 38

  • You are given an input array of integers: [8, 3, 7, 1, 2, 10, 5]. (a)...

    You are given an input array of integers: [8, 3, 7, 1, 2, 10, 5]. (a) Build a min-heap for the input data. Draw a figure just to show the min-heap. (b) Using the min-heap built above, sort the input data in non-descending order using the heap sort algorithm.

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