Selection sortSelection sort is a sort algorithm that works as follows:
\n public static void selectionSort (int[] numbers)\n {\n int min, temp;
The naïve algorithm, iterating through a list of n unsorted items, has a worst-case, average-case, and best-case run-time of &Theta(n2), assuming that comparisons can be done in constant time. Thus it is outperformed on almost-sorted lists by insertion sort.
Heapsort greatly improves the basic algorithm by using a heap data structure to speed up finding and removing the lowest datum.
ImplementationsHere is a simple implementation of selection sort in C (from pd lecture notes):\nint find_min_index (float [], int, int);\nvoid swap (float [], int, int);\n \n/* selection sort on array v of n floats */\n \nvoid selection_sort (float v[], int n) {\n int i;\n \n /* for i from 0 to n-1, swap v[i] with the minimum\n * of the i'th to the n'th array elements\n */\n for (i=0; iHere's another implementation in Python:
\ndef selsort(iterable):\n """A simple selection-sort implementation in python"""\n seq = list(iterable)\n return [seq.pop(seq.index(min(seq))) for i in xrange(len(seq))]\n\n\n Category:Sort algorithms |
||
"The graveyards are full of indispensable men." - Charles de Gaulle (1890-1970) |
