Question

Develop a n contesting an election. There are five electorates (processes) and each electorate can cast their vote only once

0 0
Add a comment Improve this question Transcribed image text
Answer #1

//////This code contains both the required functions partition and quick sort
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <iostream.h>
#include <conio.h>

void quicksort(int*, int, int);
int partition(int*, int, int, int);
int median3(int*,int,int);
void swap(int &, int &);

int main()
{
int myarray[7] = {10,40,20,12,30,5,15};
quicksort(myarray, 0, 6);

for (int i = 0; i < 7; i++)
{
cout << myarray[i] << endl;
}
getche();
return 0;
}

// Quicksort controller function, it partitions the different pieces of our array.
void quicksort(int *arIntegers, int left, int right)
{
cout << "quicksort ([" << arIntegers[0] << ","
<< arIntegers[1] << ","
<< arIntegers[2] << ","
<< arIntegers[3] << ","
<< arIntegers[4] << ","
<< arIntegers[5] << ","
<< arIntegers[6] << "],"
<< left << ","
<< right << ")\n";
if (right > left)
{
int pivotIndex = median3(arIntegers,left,right);
int pivotNewIndex = partition(arIntegers, left, right, pivotIndex);

// Recursive call to quicksort to sort each half.
quicksort(arIntegers, left, pivotNewIndex-1);
quicksort(arIntegers, pivotNewIndex+1, right);
}
}

int median3(int *arIntegers,int left,int right)
{
int center = (left+right)/2;

if(arIntegers[center] < arIntegers[left])
swap(arIntegers[left],arIntegers[center]);
if(arIntegers[right] < arIntegers[left])
swap(arIntegers[left],arIntegers[right]);
if(arIntegers[right] < arIntegers[center])
swap(arIntegers[center],arIntegers[right]);

swap(arIntegers[center],arIntegers[right-1]);

return center;
}

// This function takes an array (or one half an array) and sorts it.
// It then returns a new pivot index number back to quicksort.

int partition(int *arIntegers, int left, int right, int pivot)
{
cout << "partition (" << arIntegers[0] << ","
<< arIntegers[1] << ","
<< arIntegers[2] << ","
<< arIntegers[3] << ","
<< arIntegers[4] << ","
<< arIntegers[5] << ","
<< arIntegers[6] << "],"
<< left << ","
<< right << ")\n";
int pivotValue = arIntegers[pivot];

// Swap it out all the way to the end of the array
// So we know where it always is.
swap(arIntegers[pivot], arIntegers[right]);
int storeIndex = left;

// Move through the array from start to finish comparing each to our
// pivot value (not index, the value that was located at the pivot index)
for (int i = left; i < right; i++)
{
if (arIntegers[i] <= pivotValue)
{
swap(arIntegers[i], arIntegers[storeIndex]);
storeIndex++;
}
}
swap(arIntegers[storeIndex], arIntegers[right]);
return storeIndex;
}

// Simple swap function for our in place swapping.
void swap(int &val1, int &val2)
{
int temp = val1;
val1 = val2;
val2 = temp;
}

Add a comment
Know the answer?
Add Answer to:
Develop a n contesting an election. There are five electorates (processes) and each electorate can cast...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a program that supports the three phases (setup, voting and result-tallying) which sets up and...

    Write a program that supports the three phases (setup, voting and result-tallying) which sets up and executes a voting procedure as described above. In more details: You can start with the given skeleton (lab6_skeleton.cpp). There are two structures: Participant structure, which has the following members: id -- an integer variable storing a unique id of the participant (either a candidate or a voter). name -- a Cstring for storing the name of the participant.. hasVoted -- a boolean variable for...

  • (2 bookmarks) In JAVA You have been asked to write a program that can manage candidates...

    (2 bookmarks) In JAVA You have been asked to write a program that can manage candidates for an upcoming election. This program needs to allow the user to enter candidates and then record votes as they come in and then calculate results and determine the winner. This program will have three classes: Candidate, Results and ElectionApp Candidate Class: This class records the information for each candidate that is running for office. Instance variables: first name last name office they are...

  • dont need help on the first two pictures, only need help underatanding these: number 1,2,3,4,5,6. please help:/...

    dont need help on the first two pictures, only need help underatanding these: number 1,2,3,4,5,6. please help:/ O Yes. The Condorcet winner is never the majority winner Yes. The Condorcet winner is not required to receive over 50% of the possible vote. O No. The Condorcet winner is automatically the majority winner. No. The Condorcet winner always receives over 50% of the possible vote. 7. Using this preference schedule, which candidate is the Condorcet winner? (1 point) number of votes...

  • How can we assess whether a project is a success or a failure? This case presents...

    How can we assess whether a project is a success or a failure? This case presents two phases of a large business transformation project involving the implementation of an ERP system with the aim of creating an integrated company. The case illustrates some of the challenges associated with integration. It also presents the obstacles facing companies that undertake projects involving large information technology projects. Bombardier and Its Environment Joseph-Armand Bombardier was 15 years old when he built his first snowmobile...

  • In your view,is the kind of child slavery discussed in this case absolutely wrong no matter...

    In your view,is the kind of child slavery discussed in this case absolutely wrong no matter what,or is it only relatively wrong,ie.,if one happens to live in a society(like ours) that disapproves of child slavery? Explain your view and why you hold it. Forty-five percent of the chocolate we consume in the that a portion of the Ivory Coast cocos beans that goes into United States and in the rest of the world is made from co- the chocolate we...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT