Skip to content

Commit

Permalink
moore voting added
Browse files Browse the repository at this point in the history
  • Loading branch information
arorarahul committed Feb 23, 2017
1 parent f5ba63a commit b863bac
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ TODO: question14.c
- [Quick Sort](/arrays/question5.c)
- [Bubble Sort](/arrays/question6.c)
- [Counting Sort](/arrays/question7.c)
- [find a pair in an array whose sum is X](/arrays/question8.c)
- [Moore Voting algorithm to find majority element in an array](/arrays/question9.c)


# Topic1: Introduction
Expand Down
47 changes: 47 additions & 0 deletions arrays/question8.c
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,51 @@ int main(){
findPairs(a,length, sum);

}
/*
METHOD3: using quick sort technique
but finding if a pair exists or not
*/
#include <stdio.h>
#include <stdlib.h>

int cmpfunc(const void*a, const void*b){
return (*(int*)a-*(int*)b);
}

int findPairs(int arr[], int size, int sum){
int left, right;

qsort(arr,size,sizeof(int),cmpfunc);

left = 0;
right = size - 1;
while(left<right){
if(arr[left]+arr[right]==sum){
return 1;
}else if(arr[left]+arr[right]<sum){
left++;
}else{
right--;
}
}
}

int main(){
int size, index, sum, *arr;
printf("enter number of elements in the array\n");
scanf("%d",&size);
//allocate memory for array
arr = (int *)malloc(sizeof(int)*size);
printf("enter the elements of the array\n");
for(index=0; index<size; index++){
scanf("%d",&arr[index]);
}
printf("enter the sum value\n");
scanf("%d",&sum);
if(findPairs(arr,size,sum)){
printf("Pairs found\n");
}else{
printf("Pairs not found\n");
}

}
55 changes: 55 additions & 0 deletions arrays/question9.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
Given an array A of size n, find an element that occurs more than n/2 times
MOORE VOTING ALGORITHM
In this algo every element votes for itself. If a different element in encountered then the vote collected
for previous element is used to cancel out the vote for this element.
When votes is zero and new element is found voter changes
The number of votes have to be greater than or equal to 1 to satisfy the condition given in the problem
always
If it is greater than or equal to one, we loop the array again to find how many times is the voter occuring.
Time complexity:
O(n)
*/
#include <stdio.h>
#include <stdlib.h>

int main(){

int index,voter, votes=0, counter=0;
int a[] = {2,2,5,6,2,2};
int length = sizeof(a)/sizeof(a[0]);
//checking voter and votes
for(index = 0; index<length; index++){
if(votes == 0){
voter = a[index];
votes++;
}else{
if(voter == a[index]){
votes++;
}else{
votes--;
}
}
}
//checking if that element is really repeating more than n/2 times or not
if(votes >= 1){

for(index=0; index<length; index++){
if(a[index]==voter){
counter++;
}
}

if(counter > length/2){
printf("the element that occurs more than n/2 times is %d\n", voter);
}else{
printf("no element found\n");
}

}

}

0 comments on commit b863bac

Please sign in to comment.