Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implementation of queue using an array in python 3 #26

Merged
merged 3 commits into from
Oct 11, 2017
Merged

Implementation of queue using an array in python 3 #26

merged 3 commits into from
Oct 11, 2017

Conversation

tairosonloa
Copy link
Contributor

Fixes #20
Language: [Python version 3]

@nimit95
Copy link
Member

nimit95 commented Oct 11, 2017

What is the complexity of your enqueue operation? I think your enqueue is O(n) and in case of if queue it should be O(1)

@nimit95
Copy link
Member

nimit95 commented Oct 11, 2017

isn't dequeue now O(n) operation?

@tairosonloa
Copy link
Contributor Author

tairosonloa commented Oct 11, 2017

Hi Nimit,

I was searching if you were right or not, but I couldn't find the standard array python lib code.

Just in case, I have changed the code. Now enqueue use the .append() method, what I expected to be more efficient.

But, in any case, is very difficult to get an O(1) operations to enqueue/dequeue operations using arrays for a not predeterminate initial size. I mean:

If you reserve memory for 100 elements queue using arrays, enqueue and dequeue operations will always be O(1), because in those operations you are going to a predeterminate memory position, but you never will enqueue more than 100 elements, and also, you will have a lot of memory useless if your queue isn't full.

But, if you really want a modular queue, we can't set a predeterminate initial size(1). That is what my code does, thanks to python arrays management on memory. It reserves memory statically on the stack for the initial array, and new positions are added to like a linked list. It's really efficient, and it's flexible on runtime.

I hope you like my explanation 😄

(1) Another option could be asking for a queue size at the beginning.

@tairosonloa
Copy link
Contributor Author

And not, dequeue isn't O(n) complexity because as I explained, the python arrays management on memory as linked list.

@nimit95
Copy link
Member

nimit95 commented Oct 11, 2017

Issue says specifically array so will merge your request. But I found this https://stackoverflow.com/questions/1296511/efficiency-of-using-a-python-list-as-a-queue
Dequeue can be used for both O(1)

@nimit95 nimit95 merged commit d8e127c into CodersForLife:master Oct 11, 2017
@tairosonloa
Copy link
Contributor Author

I didn't know, thanks for the explanation. I also have pull requested to the issue #19 (stack using an array) with a very similar code, so this will apply to it too.

You learn something new every day.

@nimit95
Copy link
Member

nimit95 commented Oct 11, 2017

yeah I learnt something new too :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants