# Sorted Names # by Robert Layton ARRAY_LENGTH = 4 # Init array of ARRAY_LENGTH length, "empty" until user input nameArray = [None] * ARRAY_LENGTH def insertionSort(stringArray): for a in range(1,len(stringArray)): temp = stringArray[a] b = a while b > 0 and temp < stringArray[b-1]: stringArray[b] = stringArray[b-1] b -= 1 stringArray[b] = temp return stringArray print 'NAME ENTRY' #Get user input for the names for j in range(0,ARRAY_LENGTH): nameArray[j] = str(raw_input('\nEnter Name #' + str(j))) #Show array as entered print '\n\nENTERED ARRAY' print nameArray #Insertion Sort array sortedNameArray = insertionSort(nameArray) print '\nSORTED ARRAY (DESCENDING)' print sortedNameArray print '\nSORTED ARRAY (ASCENDING)' print list(reversed(sortedNameArray))
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