本帖最后由 常在河边走_ 于 2013-9-5 22:02 编辑
- 鸡尾酒排序,也就是定向冒泡排序, 鸡尾酒搅拌排序, 搅拌排序 (也可以视作选择排序的一种变形), 涟漪排序, 来回排序,
- 是冒泡排序的一种变形。此算法与冒泡排序的不同处在于排序时是以双向在序列中进行排序。
- */
- public function cocktailSortArr(result:Array):Array {
- var i:int = 0;
- var n:int = result.length;
- var top:int = n - 1;
- var bottom:int = 0;
- var swapped:Boolean = true;
- while(swapped) { // if no elements have been swapped, then the list is sorted
- swapped = false;
- var temp:Number;
- for(i = bottom; i < top;i++) {
- if(result > result[i + 1]) { // test whether the two elements are in the correct order
- temp = result;// let the two elements change places
- result = result[i + 1];
- result[i + 1] = temp;
- swapped = true;
- }
- }
- // decreases top the because the element with the largest value in the unsorted
- // part of the list is now on the position top
- top = top - 1;
- for(i = top; i > bottom;i--) {
- if(result < result[i - 1]) {
- temp = result;
- result = result[i - 1];
- result[i - 1] = temp;
- swapped = true;
- }
- }
- // increases bottom because the element with the smallest value in the unsorted
- // part of the list is now on the position bottom
- bottom = bottom + 1;
- }
- return result;
- }
|