Created
November 22, 2013 14:59
-
-
Save cubaraphael/7601237 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| # encoding: utf-8 | |
| import random | |
| class Sorter: | |
| def sort_numbers(self, aList): | |
| contador = 0 | |
| for i in range(1, len(aList)): | |
| contador += 1 | |
| val = aList[i] | |
| j = i - 1 | |
| while (j >= 0) and (aList[j] > val): | |
| contador += 1 | |
| aList[j+1] = aList[j] | |
| j = j - 1 | |
| aList[j+1] = val | |
| print contador | |
| """ | |
| No selection_sort ele percorre por todos os itens da lista | |
| para poder ordenar usando essa técnica. | |
| """ | |
| def selection_sort(self, aList): | |
| contador = 0 | |
| for index in range(0, len(aList)): | |
| iSmall = index | |
| for i in range(index,len(aList)): | |
| contador += 1 | |
| if aList[iSmall] > aList[i]: | |
| contador += 1 | |
| iSmall = i | |
| aList[index], aList[iSmall] = aList[iSmall], aList[index] | |
| print contador | |
| return aList | |
| def create_list_inverse(self, size): | |
| lista, contador = [], size | |
| for i in range(contador): | |
| lista.append(contador) | |
| contador -= 1 | |
| return lista | |
| def randomize_list(self, aList): | |
| random.shuffle(aList) | |
| return aList | |
| def create_list(self, size): | |
| return range(1, size+1) | |
| sorter = Sorter() | |
| lista_descentente = sorter.create_list_inverse(100) | |
| lista_ascentente = sorter.create_list(100) | |
| lista_aleatoria = sorter.randomize_list(lista_ascentente) | |
| sorter.selection_sort(lista_ascentente) | |
| sorter.selection_sort(lista_descentente) | |
| sorter.selection_sort(lista_aleatoria) | |
| print " =================== " | |
| lista_ascentente = sorter.create_list(100) | |
| lista_descentente = sorter.create_list_inverse(100) | |
| lista_aleatoria = sorter.randomize_list(lista_ascentente) | |
| sorter.sort_numbers(lista_ascentente) | |
| sorter.sort_numbers(lista_descentente) | |
| sorter.sort_numbers(lista_aleatoria) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment