Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4005906
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T08:27:02+00:00 2026-05-20T08:27:02+00:00

I have been asked this question and I think it’s doable, However I am

  • 0

I have been asked this question and I think it’s doable, However I am having a hard time coming up with an algorithm to do this. The restrictions is that you can not use any other data structure nor create another queue. Also you only can use enqueue, dequeue and peek (NOT a priority queue).

Thanx for contributing 🙂

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T08:27:03+00:00Added an answer on May 20, 2026 at 8:27 am
    Sort(Q,n):
    
      for i = 0 to n-1
        min = findMin(Q, n-i, n)
        reorder(Q, min, n)
      end for
    
    findMin(Q, k, n):
    
      min = infinity
    
      for i = 1 to n
        curr = dequeue(Q)
        if curr < min && i<=k
          min = curr
        end if
        enqueue(Q,curr)
      end for
    
      return min
    
    reorder(Q, min, n):
    
      for i = 1 to n
        curr = dequeue(Q)
        if (curr != min) // I'm assuming that there is a way to differentiate between any two items, this isn't hard to enforce
          enqueue(Q, curr)
        end if
      end for
    
      enqueue(Q,min)
    

    Ascending sort, runs in O(n^2) time, in space O(1) (i.e. the queue is O(n) space, and extra space required by the algorithm is O(1))

    Explanation:

    Essentially, we iterate through the queue n times, each time the iteration costs 2n.

    On each iteration, we go through the entire queue and pick the minimum within the relevant number of items. So at first the relevant number of items is n, since we want the minimum of them all. The next iteration we want the minimum of the first n-1 items, and then n-2 items, etc. Since the algorithm will stack these minimums at the end.

    Once we found the minimum, we need to iterate over the whole stack again in order to snatch it and stack it on the end.

    The main idea here, is that a dequeue followed by an enqueue of that same element will allow us to iterate over the queue and check minimum/maximum while preserving order.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This question has been asked before ( link ) but I have slightly different
This question seems to have been asked a lot, but I haven't seen an
This question may have been asked already - but unfortunately, I could not find
I realize this question is very likely to have been asked before, but I've
I asked this question before, Here however I think I presented the problem poorly,
I think this is a question that has been asked many times but I
I hope that this question has not been asked elsewhere, but I'm having difficulty
I'm aware that variants of this question have been asked before, but I do
This question has been asked in various guises. However. this is a slightly different
Similar questions to this one have been asked but none seem to address my

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.