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

Categories: Flex; Tagged with: ; @ October 19th, 2008 16:48

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

顺序查找实现 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

ActionScript实现插入排序[直接插入排序 Insertion Sort],交互排序排序[单向双向冒泡排序 Bubble Sort]

Categories: Flex; Tagged with: ; @ October 19th, 2008 16:41

上学时数据结构就一直没学好, 死皮赖脸连抄带蒙, 补考时才考过. 早知道现如今脑子不够使, 那时候一定好好学….

排序的算法比较多,大概可分为:
* 插入排序
* 冒泡排序
* 选择排序
* 快速排序
* 堆排序
* 归并排序
* 基数排序
* 希尔排序
在这我就找俩最简单的练练手先, 写的不一定对, 欢迎指正, 批评才是我前进的动力…

直接插入排序  Insertion Sort

	 }
	/**
	 * 插入排序[直接插入排序 Insertion Sort]
	 */ 
	 public static function insertionSort(arrayToBeSorted:Array):void {
	 	for(var i:int = 0; i0; j--) {
		 		if(arrayToBeSorted[j-1]>arrayToBeSorted[i]) {
		 			arrayToBeSorted[j] = arrayToBeSorted[j-1];
		 		}else { 
		 			break;
		 		}
		 		trace(j);
		 	}
		 	arrayToBeSorted[j] = temp;
	 	}
	 	trace(arrayToBeSorted);
	 }
	 

交互排序[冒泡排序 Bubble Sort]

 /**
	 * 交互排序[冒泡排序 Bubble Sort]
	 */ 
	 public static function bubbleSort(arraytoBeSorted:Array):void {
	 	var startIndex:int = 0;
	 	var tempIndex:int;
	 	for(var j:int = arraytoBeSorted.length - 1; j>0; j--) {
		 	for(var i:int = arraytoBeSorted.length - 1; i>startIndex; i--) {
		 		if(arraytoBeSorted[i-1]>arraytoBeSorted[i]) {
		 			//swap(arraytoBeSorted[i], arraytoBeSorted[i-1]);
		 			var temp:int = arraytoBeSorted[i];
		 			arraytoBeSorted[i] = arraytoBeSorted[i-1];
		 			arraytoBeSorted[i-1] = temp;
		 			tempIndex = i;
		 		}
		 	}//end of for
	 		startIndex = tempIndex;
	 		trace("StartIndex: " + startIndex);
	 	trace("冒泡法排序结果: " + arraytoBeSorted);
		 }//end of for	
	 }

双向冒泡:

 /**
	 * 交互排序[双向冒泡]
	 */
	 public static function doubleDirectionBubbleSort(arrayToBeSorted:Array):void {
	 	var j:int;
	 	var k:int;
	 	var tempIndex:int = arrayToBeSorted.length;
	 	
	 	var startIndex:int = 0;
	 	var endIndex:int = arrayToBeSorted.length - 1;
	 	
	 	var change:Boolean = true;
	 	while(startIndexstartIndex; j--) {
	 			change = false;
	 			if(arrayToBeSorted[j]arrayToBeSorted[k]) {
	 				var temp:int = arrayToBeSorted[k];
	 				arrayToBeSorted[k] = arrayToBeSorted[k-1];
	 				arrayToBeSorted[k-1] = temp;
	 				change = true;
	 			}
	 			tempIndex = k;
	 			trace("重的下沉: " + arrayToBeSorted +" Changed: " + change.toString() + " StrtIndex: " + startIndex + " EndIndex:" + k);
	 		}//end of for
	 		endIndex = tempIndex;
	 		trace("startIndex: " + startIndex + " endIndex: " + endIndex);	
	 		trace("双向冒泡排序后: " + arrayToBeSorted);
	 	}//end of while
	 }//end of function 



// Proudly powered by Apache, PHP, MySQL, WordPress, Bootstrap, etc,.