Is my algorithm correct?
Not exactly. Routing protocols do not route. A router can route without even using a routing protocol. Routing protocols simply exchange routes with other routers. The router will determine which routes populate the routing table from various sources, including routing protocols, based on things like reliability of the prefix source, e.g. AD, etc., but the router will route packets based on what is already in the routing table.
Also, the router only has the destination address, not the mask of the network where the destination is, so it doesn't extract a network prefix from the destination address.
The router will strip off the layer-2 frame, examine the layer-3 destination address on the packet, and perform a lookup in its routing table, looking for the longest prefix that matches the destination address. If it doesn't find a match, it drops the packet. If it finds a match it will have the outgoing interface, and it will build a new frame for the next interface and forward the new frame out the interface.
Based on your comment, you seem to be confusing how the routing table is populated with how it is used by the router to route packets.
A router will look at all the route sources (directly connected networks, static routes, and routing protocols) to determine candidates for the routing table. Each routing protocols maintains its own tables of routes received from other routers. Candidate routes from the routing protocols are the routes in the routing protocol's tables with the best metric to a destination network.
If a candidate has no opponents in the other sources, it is installed in the routing table.
If there are multiple candidates using the same source, e.g. routing protocol, it compares the metrics for the candidates, and it selects the one with the lowest metric to install in the routing table.
If there are multiple candidates from multiple sources, it uses something like Cisco's AD (Administrative distance), which is the reliability of the source, to select the candidate to install in the routing table because metrics from different routing protocols cannot be compared.