####SELECTION SORTING#### def create_list(): pass def selection_sort(in_values): comps = 0 swaps = 0 values = list(in_values) for i in range(len(values)): guess = i for j in range(i + 1, len(values)): comps += 1 if values[j] < values[guess]: guess = j swaps += 1 values[i], values[guess] = values[guess], values[i] return values, comps, swaps lst = [2, 3, 34, 46, 765, 234, 12, 9, 43, 321] #print("Original List: ", lst) lst, c, s = selection_sort(lst) #print("Sorted List: ", lst) #print("Comparisons Count: ", c) #print("Swaps Count: ", s) ####INSERTION SORTING#### lst = [17, 2, 3, 42, 9] def insertion_sort(in_list): values = list(in_list) comps = 0 swaps = 0 for i in range(len(values)): while values[i] < values[i-1] and i > 0: comps += 1 swaps += 1 values[i-1], values[i] = values[i], values[i-1] i -= 1 return values, comps, swaps lst = [2, 3, 34, 46, 765, 234, 12, 9, 43, 321] lst2 = [2, 3, 34, 46, 765, 234, 12, 9, 43, 321] print("Original List: ", lst) lst, c, s = selection_sort(lst) print("Sorted List: ", lst) print("Comparisons Count: ", c) print("Swaps Count: ", s) print("-----------------------------------------------------------------------------------") print("Original List: ", lst2) lst2, c, s = insertion_sort(lst2) print("Sorted List: ", lst2) print("Comparisons Count: ", c) print("Swaps Count: ", s)
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question