#!/usr/bin/env python from random import randint """ This pretty little pice of code creates and prints an unsorted list with 5 till 10 integers to stdout. After this, it trys to sort the list, using the so-called "flipsort-algorithm" (which may terminate in endless time). As soon as the sorting algorithm has terminated, it will print the ordered list to stdout. """ # Checks whether a given list is ordered # # @param list list of integers to be checked # @return bool true if given list is ordered, otherwise false def is_ordered(list): if(len(list) > 1): for i in range(0, len(list) - 1): if list[i] > list [i + 1]: return False return True # The flipsort-algorithm # # @param list list of unsorted integers # @return list a ordered permutation of the given list def flipsort(list): while(not is_ordered(list)): e1 = randint(0, len(list) - 1) e2 = randint(0, len(list) - 1) list[e1], list[e2] = list[e2], list[e1] return list # Create a list with 5 - 10 random integers list = [] for i in range(randint(5, 10)): list.append(randint(0, 1000)) # Print unsorted list print "Unsorted list: %s" % list # Print sorted list (this may take a few seconds xD) print "Sorted list : %s" % flipsort(list)