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
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