Question

Give a linear (O(n)) time algorithm sorting n values in range 0..(n^3) − 1. (Hint: represent...

Give a linear (O(n)) time algorithm sorting n values in range 0..(n^3) − 1.

(Hint: represent a value x as (i, j, k) where x = i · (n^2) + j · n + k.)

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

#include<iostream>
using namespace std;
int main()
{
   int n,p,i,j;
   cout<<"Enter the value of n : ";
   cin>>n;
  
   cout<<"Enter the number of values you want to enter : ";
   cin>>p;
  
   int arr[n*n*n]={0};
  
   cout<<"Enter the value one by one : ";
   for(i=0;i<p;i++)
   {
       cin>>j;
       arr[j]=1;
   }
  
   cout<<"Sorted values are : ";
   for(i=0;i<n*n*n;i++)
   {
       if(arr[i]==1)
       cout<<i<<" ";
   }
}

Sample Output:-

F\Codes Untitled1.exe Enter the value of n 5 Enter the number of values you want to enter6 Enter the value one by one 87 24 5

Add a comment
Know the answer?
Add Answer to:
Give a linear (O(n)) time algorithm sorting n values in range 0..(n^3) − 1. (Hint: represent...
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