-
Notifications
You must be signed in to change notification settings - Fork 208
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
Conversation
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) |
isn't dequeue now O(n) operation? |
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. |
And not, dequeue isn't O(n) complexity because as I explained, the python arrays management on memory as linked list. |
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 |
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. |
yeah I learnt something new too :) |
Fixes #20
Language: [Python version 3]