i'm pretty clueless about everything dealing with python. i need help creating a sorting algorithm that can sort names in a list alphabetically.

and example code to help get me started would be much appreciated.

Here is how:

``````>>> import random,string
>>> t=[]
>>> for i in range(20):
s=''
for j in range(random.randint(3,10)):
s=s+random.choice(string.ascii_lowercase)
print s
t.append(s)

ktpwmhc
oottmafdv
mlalyaig
redmmtv
bmoflbd
gsagulicx
qzfvmdz
lsviy
bdffmd
bdmk
dtqjnwhla
lufobzcqk
lfnnf
ppv
tghkzqxtja
bouvqq
dbccxvuke
vvmmvxl
saelhkhgya
knwjvdsv
>>> s
'knwjvdsv'
>>> t …``````

Are you looking for designing your own algorithm?
If not then use `list.sort()` or `sorted`

## All 5 Replies

Here is how:

``````>>> import random,string
>>> t=[]
>>> for i in range(20):
s=''
for j in range(random.randint(3,10)):
s=s+random.choice(string.ascii_lowercase)
print s
t.append(s)

ktpwmhc
oottmafdv
mlalyaig
redmmtv
bmoflbd
gsagulicx
qzfvmdz
lsviy
bdffmd
bdmk
dtqjnwhla
lufobzcqk
lfnnf
ppv
tghkzqxtja
bouvqq
dbccxvuke
vvmmvxl
saelhkhgya
knwjvdsv
>>> s
'knwjvdsv'
>>> t
['ktpwmhc', 'oottmafdv', 'mlalyaig', 'redmmtv', 'bmoflbd', 'gsagulicx', 'qzfvmdz', 'lsviy', 'bdffmd', 'bdmk', 'dtqjnwhla', 'lufobzcqk', 'lfnnf', 'ppv', 'tghkzqxtja', 'bouvqq', 'dbccxvuke', 'vvmmvxl', 'saelhkhgya', 'knwjvdsv']
>>> t.sort()
>>> t
['bdffmd', 'bdmk', 'bmoflbd', 'bouvqq', 'dbccxvuke', 'dtqjnwhla', 'gsagulicx', 'knwjvdsv', 'ktpwmhc', 'lfnnf', 'lsviy', 'lufobzcqk', 'mlalyaig', 'oottmafdv', 'ppv', 'qzfvmdz', 'redmmtv', 'saelhkhgya', 'tghkzqxtja', 'vvmmvxl']
>>>``````

Are you looking for designing your own algorithm?
If not then use `list.sort()` or `sorted`

Python has a pretty fast algorithm built-in called an adaptive merge sort. Here is an example:

``````# sort a list of names without changing the original list
# uses the high speed sorting algorithm built-into Python

name_list1 = ['Frank', 'Mark', 'Hank', 'Zoe', 'Bob', 'Carl', 'Al']
name_list2 = sorted(name_list1)

print "Original:"
print name_list1
print "Sorted:"
print name_list2

"""my output -->
Original:
['Frank', 'Mark', 'Hank', 'Zoe', 'Bob', 'Carl', 'Al']
Sorted:
['Al', 'Bob', 'Carl', 'Frank', 'Hank', 'Mark', 'Zoe']
"""``````

If you have to create your own sorting algorithm, go with a selection sort. It is relatively slow, but rather easy to understand.

A selection sort of a list of numbers is pretty simple. You start
with two lists, let's call the original unsorted list the start_list
and you have another list call it the end_list which is empty at
the start.

If you want to sort ascending (lowest value first) you get the lowest
value of start_list using lowest = min(start_list) and append it to
the end_list with end_list.append(lowest). Now remove this value from
the start_list with start_list.remove(lowest) and repeat until the
start_list is empty and return the end_list with the sorted values. The repeat can be done with a while loop.

If you have to make your own sort algorithm i would look at the wikipedia pages for sorts such as the bubble sort. It is a very simple sort and the pseudo-code can be very easily translated into python.
http://en.wikipedia.org/wiki/Bubble_sort

From wikipedia pseudo code

``````procedure bubbleSort( A : list of sortable items ) defined as:
do
swapped := false
for each i in 0 to length(A) - 2 inclusive do:
if A[i] > A[i+1] then
swap( A[i], A[i+1] )
swapped := true
end if
end for
while swapped
end procedure``````

Hope that helps :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.21 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.