Skip to content

Commit b863bac

Browse files
committed
moore voting added
1 parent f5ba63a commit b863bac

File tree

3 files changed

+104
-0
lines changed

3 files changed

+104
-0
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ TODO: question14.c
4949
- [Quick Sort](/arrays/question5.c)
5050
- [Bubble Sort](/arrays/question6.c)
5151
- [Counting Sort](/arrays/question7.c)
52+
- [find a pair in an array whose sum is X](/arrays/question8.c)
53+
- [Moore Voting algorithm to find majority element in an array](/arrays/question9.c)
5254

5355

5456
# Topic1: Introduction

arrays/question8.c

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,4 +69,51 @@ int main(){
6969
findPairs(a,length, sum);
7070

7171
}
72+
/*
73+
METHOD3: using quick sort technique
74+
but finding if a pair exists or not
75+
*/
76+
#include <stdio.h>
77+
#include <stdlib.h>
7278

79+
int cmpfunc(const void*a, const void*b){
80+
return (*(int*)a-*(int*)b);
81+
}
82+
83+
int findPairs(int arr[], int size, int sum){
84+
int left, right;
85+
86+
qsort(arr,size,sizeof(int),cmpfunc);
87+
88+
left = 0;
89+
right = size - 1;
90+
while(left<right){
91+
if(arr[left]+arr[right]==sum){
92+
return 1;
93+
}else if(arr[left]+arr[right]<sum){
94+
left++;
95+
}else{
96+
right--;
97+
}
98+
}
99+
}
100+
101+
int main(){
102+
int size, index, sum, *arr;
103+
printf("enter number of elements in the array\n");
104+
scanf("%d",&size);
105+
//allocate memory for array
106+
arr = (int *)malloc(sizeof(int)*size);
107+
printf("enter the elements of the array\n");
108+
for(index=0; index<size; index++){
109+
scanf("%d",&arr[index]);
110+
}
111+
printf("enter the sum value\n");
112+
scanf("%d",&sum);
113+
if(findPairs(arr,size,sum)){
114+
printf("Pairs found\n");
115+
}else{
116+
printf("Pairs not found\n");
117+
}
118+
119+
}

arrays/question9.c

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
Given an array A of size n, find an element that occurs more than n/2 times
3+
4+
MOORE VOTING ALGORITHM
5+
6+
In this algo every element votes for itself. If a different element in encountered then the vote collected
7+
for previous element is used to cancel out the vote for this element.
8+
When votes is zero and new element is found voter changes
9+
The number of votes have to be greater than or equal to 1 to satisfy the condition given in the problem
10+
always
11+
If it is greater than or equal to one, we loop the array again to find how many times is the voter occuring.
12+
13+
Time complexity:
14+
O(n)
15+
16+
*/
17+
#include <stdio.h>
18+
#include <stdlib.h>
19+
20+
int main(){
21+
22+
int index,voter, votes=0, counter=0;
23+
int a[] = {2,2,5,6,2,2};
24+
int length = sizeof(a)/sizeof(a[0]);
25+
//checking voter and votes
26+
for(index = 0; index<length; index++){
27+
if(votes == 0){
28+
voter = a[index];
29+
votes++;
30+
}else{
31+
if(voter == a[index]){
32+
votes++;
33+
}else{
34+
votes--;
35+
}
36+
}
37+
}
38+
//checking if that element is really repeating more than n/2 times or not
39+
if(votes >= 1){
40+
41+
for(index=0; index<length; index++){
42+
if(a[index]==voter){
43+
counter++;
44+
}
45+
}
46+
47+
if(counter > length/2){
48+
printf("the element that occurs more than n/2 times is %d\n", voter);
49+
}else{
50+
printf("no element found\n");
51+
}
52+
53+
}
54+
55+
}

0 commit comments

Comments
 (0)