Jump to content

Sorting

From Wikibooks, open books for an open world


Bubble Sort

[edit | edit source]
Regular Bubble Sort
Language General Usage
Pseudocode
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

Insertion Sort

[edit | edit source]
Language General Usage
Pseudocode
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