How do you sort a string in python without using the sort function?

This is not based on efficiency, and has to be done with only a very very basic knowledge of python [Strings, Tuples, Lists basics] so no importing functions or using sort/sorted. [This is using Python 2.7.3].

For example I have a list:

unsort_list = ["B", "D", "A", "E", "C"]
sort_list = []

sort_list needs to be able to print out:

"A, B, C, D, E"

I can do it with numbers/integers, is there a similar method for alphabetical order strings? if not what would you recommend [even if it isn't efficient.] without importing or sort functions.

georg

207k48 gold badges294 silver badges375 bronze badges

asked Oct 27, 2012 at 15:23

4

Here's a very short implementation of the Quicksort algorithm in Python:

def quicksort[lst]:
    if not lst:
        return []
    return [quicksort[[x for x in lst[1:] if x <  lst[0]]]
            + [lst[0]] +
            quicksort[[x for x in lst[1:] if x >= lst[0]]]]

It's a toy implementation, easy to understand but too inefficient to be useful in practice. It's intended more as an academic exercise to show how a solution to the problem of sorting can be written concisely in a functional programming style. It will work for lists of comparable objects, in particular for the example in the question:

unsort_list = ['B', 'D', 'A', 'E', 'C']
sort_list   = quicksort[unsort_list]

sort_list
> ['A', 'B', 'C', 'D', 'E']

answered Oct 27, 2012 at 15:37

Óscar LópezÓscar López

228k35 gold badges304 silver badges377 bronze badges

2

just for fun:

from random import shuffle
unsorted_list = ["B", "D", "A", "E", "C"]

def is_sorted[iterable]:
  for a1,a2 in zip[iterable, iterable[1:]]:
     if a1 > a2: return False
  return True

sorted_list = unsorted_list
while True:
   shuffle[sorted_list]
   if is_sorted[sorted_list]: break

the average complexity should be factorial and the worst case infinite

answered Oct 27, 2012 at 16:19

Ruggero TurraRuggero Turra

16k14 gold badges86 silver badges133 bronze badges

1

Python already know which string is first and which is next depending on their ASCII values

for example:

"A"unsort_list[j]:
                temp = unsort_list[i]
                unsort_list[i] = unsort_list[j]
                unsort_list[j] = temp
    print["sorted list:{}".format[unsort_list]]

sortalfa[["B", "D", "A", "E", "C"]]

Result:

sorted list:['A', 'B', 'C', 'D', 'E']

There are many standard libraries available by which can be done using single line of code.

answered Dec 26, 2018 at 8:30

u = ["B", "D", "A", "E", "C"]
y=[]
count=65
while len[y] list_val[i+1]:
      list_val[i], list_val[i+1] = list_val[i+1], list_val[i]

print list_val

answered Sep 9, 2016 at 20:02

The more_itertools library has an implementation of a mergesort algorithm called collate.

import more_itertools as mit

iterables = ["B", "D", "A", "E", "C"]
list[mit.collate[*iterables]]
# ['A', 'B', 'C', 'D', 'E']

answered Aug 23, 2017 at 5:35

pylangpylang

36.3k11 gold badges120 silver badges110 bronze badges

I have tried something like this but I'm not sure about time complexity of program.

l=['a','c','b','f','e','z','s']
l1=[]
while l:
    min=l[0]
    for i in l:
        # here **ord** means we get the ascii value of particular character.
        if ord[min]>ord[i]:
            min=i
     l1.append[min]
     l.remove[min]
print[l1]

Eric Aya

69.2k34 gold badges176 silver badges246 bronze badges

answered Jan 25, 2019 at 17:05

Ramakrishna KRamakrishna K

431 gold badge1 silver badge7 bronze badges

def quicksort[lst]:
    if not lst:
        return []
    return [quicksort[[x for x in lst[1:] if x <  lst[0]]]
            + [lst[0]] +
            quicksort[[x for x in lst[1:] if x >= lst[0]]]]
unsort_list = ['B', 'D', 'A', 'E', 'C']
sort_list   = quicksort[unsort_list]

sort_list

['A', 'B', 'C', 'D', 'E']

Adrian Mole

46.2k125 gold badges45 silver badges73 bronze badges

answered Jan 11, 2020 at 3:37

def insertion_sort[array]:
mapped = len[array]
for i in range[1, mapped]:
    indexi = array[i]
    indexj = i - 1
    while indexj >= 0 and indexi < array[indexj]:
        array[indexj+1] = array[indexj]
        indexj = indexj - 1
    array[indexj+1] = indexi

#driver or execution code
a = [2,3,5,7,1,4,6]
insertion_sort[a]
print[a]
b = ['B', 'D', 'A', 'E', 'C']
insertion_sort[b]
print[b]

#Result

[1, 2, 3, 4, 5, 6, 7]
['A', 'B', 'C', 'D', 'E']

answered Mar 16 at 20:57

even simpler:

dc = { }
for a in unsorted_list:
  dc[a] = '1'

sorted_list = dc.keys[]

answered Oct 27, 2012 at 15:48

Aniket IngeAniket Inge

25k5 gold badges48 silver badges78 bronze badges

1

u = ["B", "D", "A", "F", "C"]

y=[]

count=65

while len[y] NumList[1]] = if[67 > 86] – It means the condition is False. So, it exits from If block, and j value incremented by 1.

Chủ Đề