跳转到内容

排序

来自维基教科书,开放的书籍,为开放的世界


冒泡排序

[编辑 | 编辑源代码]
常规冒泡排序
语言 通用用法
伪代码
n ← MaxIndex - 1
FOR i ← 1 TO n
    FOR j ← 1 TO n
		IF MyList[j] > MyList[j + 1]
			THEN
				Temp ← MyList[j]
				MyList[j] ← MyList[j + 1]
				MyList[j + 1] ← Temp
		ENDIF
	ENDFOR
	n ← n - 1   // this means the next time round the inner loop, we don't
		        // look at the values already in the correct positions.
ENDFOR
VB.NET

插入排序

[编辑 | 编辑源代码]
语言 通用用法
伪代码
FOR Pointer ← 2 TO NumberOfitems
	ItemToBeInserted ← List[Pointer]
	CurrentItem ← Pointer - 1 // pointer to last item in sorted part of list
	WHILE (List[CurrentItem] > ItemToBeInserted) AND (CurrentItem > 0)
		List[CurrentItem + 1] ← List[CurrentItem] // move current item down
		CurrentItem ← CurrentItem - 1 // look at the item above
	END WHILE
	List[CurrentItem + 1] ← ItemToBeInserted // insert item
ENDFOR
VB.NET
华夏公益教科书