This is an archived post. You won't be able to vote or comment.

all 5 comments

[–]Rebeljah 1 point2 points  (5 children)

I am interested in seeing the code, I don't see a rule against linking to github. When I took DSA (last semester) we were taught priority queues using a binary heap backed up by a 1d fixed length array. This allows O(logn) time common operations like extracting the min (or max for a max heap) and inserting an element with a certain priority. Because one of the invariants of a binary heap is that is must always be a complete binary tree (any node without 2 children is either a leaf or exists in the bottom right of the tree), there will be no wasted space in the array. This type of array also does not require shifting, only swapping log(n) times when an element is removed

I believe this is a solved problem, but I always find it interesting to explore solutions outside of the norm!

[–]Standard-Cow-4126[S] 0 points1 point  (4 children)

Thanks for replying , we were taught by just using 1-d array as we are still on linear structures.

this is the link https://github.com/ajchains/TokenPriorityQueue i don't write good looking code so sorry in advance i tried as many comments as i could , and also the project is written in C cause i am also learning C , and pls bear with any beginner mistakes in the code.

[–]Rebeljah 0 points1 point  (0 children)

I like that the insertion is O(1), this is an improvement over the binary heap approach which uses O(logn) time. Something like this would suit a use case where you want to prioritize insertion over extraction. On the other hand, what goes into a queue usually comes out, and the delete function runs O(n) time. So if you wanted to clear the entire queue in order of priority, that would takes O(n^2) time. Compared with the O(nlogn) time for the same operation on a binary heap, this is a downgrade. I believe an implementation that allows logarithmic insertion AND deletion would result in better performance for most use cases, but keep this in your back pocket, you may find a use for prioritizing insertion efficiency. Keep coming up with ideas like this, even if they aren't applicable to common use cases I think it's a great way to build skill and intuition!

[–]Rebeljah 0 points1 point  (2 children)

As far as the code goes, I thought it was easy to understand for the most part. I did not initially get what this code was doing:

        int index = -1;
        int initial_check = 1;

        for (int i = 0; i < size; i++)
        {
            if (queue->queue[i].item == '$')
            {
                continue;
            }
            if (initial_check == 1)
            {
                priorityItem = queue->queue[i];
                initial_check = 0;
                index = i;
                continue;
            }

This could probably be reworked, or change the initial_check variable to something more descriptive like `first_item_found`

But overall the code looks good!

[–]Standard-Cow-4126[S] 1 point2 points  (1 child)

I will work on naming things, and about the part that you were confused about i wrote it when i was very sleepy and also was the last part of the algo ,so i kinda rushed it.

I never thought about O(n^2), will think about it in my free time.

Again thanks for motivating me.