Skip to content

Commit 2f08112

Browse files
committed
Initial working Draft- BoyerMajorityVoting Algo
0 parents  commit 2f08112

File tree

6 files changed

+100
-0
lines changed

6 files changed

+100
-0
lines changed

Algos/.classpath

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<classpath>
3+
<classpathentry kind="src" path="src"/>
4+
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.7"/>
5+
<classpathentry kind="output" path="bin"/>
6+
</classpath>

Algos/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/bin

Algos/.project

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<projectDescription>
3+
<name>Algos</name>
4+
<comment></comment>
5+
<projects>
6+
</projects>
7+
<buildSpec>
8+
<buildCommand>
9+
<name>org.eclipse.jdt.core.javabuilder</name>
10+
<arguments>
11+
</arguments>
12+
</buildCommand>
13+
</buildSpec>
14+
<natures>
15+
<nature>org.eclipse.jdt.core.javanature</nature>
16+
</natures>
17+
</projectDescription>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
eclipse.preferences.version=1
2+
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
3+
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.7
4+
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
5+
org.eclipse.jdt.core.compiler.compliance=1.7
6+
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
7+
org.eclipse.jdt.core.compiler.debug.localVariable=generate
8+
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
9+
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
10+
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
11+
org.eclipse.jdt.core.compiler.source=1.7
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package algos;
2+
3+
4+
/*
5+
* USAGE: find the majorty elemnt in a LIST.
6+
*/
7+
public class BoyerMooreVoteAlgo {
8+
private int [] IntergerList;
9+
10+
public BoyerMooreVoteAlgo (int[] IntegerList) {
11+
this.setIntergerList(IntegerList);
12+
}
13+
14+
/*
15+
* We will sweep down the sequence starting with the first one
16+
* As we sweep we maintain a pair consisting of a current candidate and a counter.
17+
* Initially, the current candidate is unknown (first value is assigned) and counter is 0.
18+
* When we move the pointer forward over an element e:
19+
* - If the counter is 0, we set the current candidate to e and we set the counter to 1.
20+
* - if the counter is not 0, we increment or decrement the counter according to whether e is the current candiate;
21+
* When we are done, the current candidate is the majority element, if there is a majority.
22+
*/
23+
public int FindMajority() {
24+
int currentValue = IntergerList[0];
25+
int counter = 0;
26+
for (int i = 0; i< IntergerList.length; i++) {
27+
if (currentValue == IntergerList[i]) {
28+
++counter;
29+
} else if (counter == 0) {
30+
currentValue = IntergerList[i];
31+
++ counter;
32+
} else {
33+
--counter;
34+
}
35+
System.out.println("Current max Value is " + currentValue + " value in the array is " + IntergerList[i] + " Counter = "+ counter);
36+
}
37+
38+
return currentValue;
39+
}
40+
41+
public int [] getIntergerList() {
42+
return IntergerList;
43+
}
44+
45+
public void setIntergerList(int [] intergerList) {
46+
IntergerList = intergerList;
47+
}
48+
49+
}

Algos/src/calls/TestAlgos.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package calls;
2+
import algos.*;
3+
public class TestAlgos {
4+
5+
public static void main(String[] args) {
6+
// TODO Auto-generated method stub
7+
TestBoyerMooreVoteAlgo();
8+
9+
}
10+
11+
public static void TestBoyerMooreVoteAlgo () {
12+
int [] TestArray = {1,1,1,3,3,2,2,3,3,3,2,3,3};
13+
BoyerMooreVoteAlgo boyer = new BoyerMooreVoteAlgo(TestArray);
14+
System.out.println("max occurence elemnt in the array is " + boyer.FindMajority());
15+
}
16+
}

0 commit comments

Comments
 (0)