0

I am trying to implement a plurality voting algorithm where priorities are used to break ties. What I mean by priorities is the following. For example, we have three classes - A, B and C. But the priority goes A, C and B, starting with the highest - A to the lowest - B. So when A and C have equal votes and more than B, A will win the plurality vote because of its higher priority.

The only idea I have for the tie-breaking is to implement many if-statements. If I am not wrong, these are 10 for the case of 3 classes. But how can this be generalized so that I can implement an algorithm for K-classes? Can you please help with some code? It could be some imperative language like C, Java, JS, C#, Python, etc or pseudo-code.

Thank you very much!

roskoN
  • 333
  • 4
  • 12
  • 1
    Instead of finding the winner by iterating over all candidates and checking `if vote[i] > vote[max]`, you'd use something like `if vote[i] > vote[max] or (vote[i] = vote[max] and priority[i] > priority[max])`. – m69's been on strike for years Jan 16 '16 at 20:40

1 Answers1

1

You can sort based on number of votes first. Then iterate through the sorted list, and for those entries with equal number of votes, sort again, this time using priority.

Sandeep
  • 98
  • 8