Skip to content

Instantly share code, notes, and snippets.

@pinglunliao
Created May 26, 2017 03:01
Show Gist options
  • Save pinglunliao/aa7aaa1e7db38e0f28dc5ca8d05b2bfa to your computer and use it in GitHub Desktop.
Save pinglunliao/aa7aaa1e7db38e0f28dc5ca8d05b2bfa to your computer and use it in GitHub Desktop.
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