跳轉到內容

排序

來自華夏公益教科書,開放的書籍,為開放的世界


氣泡排序

[編輯 | 編輯原始碼]
常規氣泡排序
語言 通用用法
虛擬碼
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
華夏公益教科書