Created
May 26, 2017 03:01
-
-
Save pinglunliao/aa7aaa1e7db38e0f28dc5ca8d05b2bfa to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import random; | |
unsortData = random.sample(range(100), 10) | |
def selection_sort(L): | |
N = len(L) | |
exchanges_count = 0 | |
for i in range(N-1): | |
min_index = i | |
for j in range(i+1, N): | |
if L[min_index] > L[j]: | |
min_index = j | |
if min_index != i: | |
L[min_index], L[i] = L[i], L[min_index] | |
exchanges_count += 1 | |
print('iteration #{}: {}'.format(i, L)) | |
print('Total {} swappings'.format(exchanges_count)) | |
return L | |
print "Original Data:", unsortData | |
sortData = selection_sort(unsortData); | |
print "Sorted Data:", sortData |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment