查找主元素算法(java编写)

最近在学习算法,做到一个查找主元素的题目,自己写了个算法,时间代价为O^2,望大家指点


下面贴出源码:

public static String NewFind(int [] a)
	{
		if(a.length==0)
		{
			return "Array is empty!";
		}
		else if(a.length==1)
		{
			return Integer.toString(a[0]);
		}
		else
		{
			int count=1;
			String msg="NOT FOUND";
			for(int i=0;i(a.length/2))
						{
							msg=Integer.toString(a[i]);
							return msg;
						}
					}
				}
				count=1;
			}
			return msg;
		}
	}


你可能感兴趣的:(算法学习)