ActionScript实现顺序查找,二分查找

写的比较潦草,欢迎指正….批评是我前进的动力……

顺序查找实现 Sequential Search

	/**
	 * 顺序查找实现 Sequential Search
	 */
	 public static function sequentialSearch(k:int, a:Array):int {
	 	var index:int = -1;
	 	for(var i:int = 0; i

二分查找实现

/**
	 * 二分查找实现
	 */
	public static function binarySearch(k:int, a:Array):int {
		var startIndex:int = 0;
		var endIndex:int = a.length - 1;
		var midIndex:int;
		var index:int = -1;

		while(startIndex <= endIndex) {
			midIndex = int((startIndex + endIndex)/2);
			if(k > a[midIndex]) {
				startIndex = midIndex + 1;
			}else {
				endIndex = midIndex - 1;
			}
			if(k == a[midIndex]) {
				index = midIndex;
			}
		}//end of while
		return index;
	}//end of function binarySearch
This entry was posted in ActionScript and tagged , , , . Bookmark the permalink.

One Response to ActionScript实现顺序查找,二分查找

  1. 系分。软件设计师 都得考的东西 。。。

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>