Python: sort list of numbers without built-in sorting, min, max function

If I have a list that changes in length each time, and I want to sort it from lowest to highest, how would I do it?

If I have: [-5, -23, 5, 0, 23, -6, 23, 67]

I want: [-23, -6, -5, 0, 5, 23, 23, 67]

I'll start with this:

 data_list = [-5, -23, 5, 0, 23, -6, 23, 67] new_list = [] minimum = data_list[0] # arbitrary number in list for x in data_list: if x < minimum: minimum = value new_list.append(i) 

BUT this only goes through once, and I get:

 new_list = [-23] 

This is where I am stuck.

How can I continue the cycle until len(new_list) = len(data_list) (i.e. all numbers will not be in the new list) with everything sorted without using the built-in functions max, min, sort? I am not sure whether to create a new list.

+7
source share
17 answers

I think you are trying to do something like this:

 data_list = [-5, -23, 5, 0, 23, -6, 23, 67] new_list = [] while data_list: minimum = data_list[0] # arbitrary number in list for x in data_list: if x < minimum: minimum = x new_list.append(minimum) data_list.remove(minimum) print new_list 
+25
source

Here is what I am trying to do. (Insertion sort is not the best way to sort, but it does the job)

 def sort(list): for index in range(1,len(list)): value = list[index] i = index-1 while i>=0: if value < list[i]: list[i+1] = list[i] list[i] = value i -= 1 else: break 
+5
source
 l = [64, 25, 12, 22, 11, 1,2,44,3,122, 23, 34] for i in range(len(l)): for j in range(i + 1, len(l)): if l[i] > l[j]: l[i], l[j] = l[j], l[i] print l 

Output:

 [1, 2, 3, 11, 12, 22, 23, 25, 34, 44, 64, 122] 
+5
source

This strictly follows your requirements not to use sort() , min() , max() , but also uses Python best practices without inventing the wheel.

 data_list = [-5, -23, 5, 0, 23, -6, 23, 67] import heapq heapq.heapify(data_list) new_list = [] while data_list: new_list.append(heapq.heappop(data_list))) 

I suggest looking in the Python library for heapq.py to find out how it works. Heapsort is a pretty fun sorting algorithm as it allows you to sort an infinite stream, i.e. You can quickly get the smallest item, but also effectively add new items to the sorted data.

+4
source
 def bubble_sort(seq): """Inefficiently sort the mutable sequence (list) in place. seq MUST BE A MUTABLE SEQUENCE. As with list.sort() and random.shuffle this does NOT return """ changed = True while changed: changed = False for i in xrange(len(seq) - 1): if seq[i] > seq[i+1]: seq[i], seq[i+1] = seq[i+1], seq[i] changed = True return None if __name__ == "__main__": """Sample usage and simple test suite""" from random import shuffle testset = range(100) testcase = testset[:] # make a copy shuffle(testcase) assert testcase != testset # we've shuffled it bubble_sort(testcase) assert testcase == testset # we've unshuffled it back into a copy 

From: http://rosettacode.org/wiki/Bubble_Sort#Python

+1
source

try sorting the list, char have ascii code, the same can be used to sort char list.

 aw=[1,2,2,1,1,3,5,342,345,56,2,35,436,6,576,54,76,47,658,8758,87,878] for i in range(aw.__len__()): for j in range(aw.__len__()): if aw[i] < aw[j] :aw[i],aw[j]=aw[j],aw[i] 
+1
source
 data = [3, 1, 5, 2, 4] n = len(data) for i in range(n): for j in range(1,n): if data[j-1] > data[j]: (data[j-1], data[j]) = (data[j], data[j-1]) print(data) 
+1
source

Here is a not very efficient sorting algorithm :)

 >>> data_list = [-5, -23, 5, 0, 23, -6, 23, 67] >>> from itertools import permutations >>> for p in permutations(data_list): ... if all(i<=j for i,j in zip(p,p[1:])): ... print p ... break ... (-23, -6, -5, 0, 5, 23, 23, 67) 
0
source

My method is

 s = [-5, -23, 5, 0, 23, -6, 23, 67] nl = [] for i in range(len(s)): a = min(s) nl.append(a) s.remove(a) print nl 
0
source

Decision

 mylist = [1, 6, 7, 8, 1, 10, 15, 9] print(mylist) n = len(mylist) for i in range(n): for j in range(1, ni): if mylist[j-1] > mylist[j]: (mylist[j-1], mylist[j]) = (mylist[j], mylist[j-1]) print(mylist) 
0
source

Here is a more readable example of the Insert Sort algorithm.

 a = [3, 1, 5, 2, 4] for i in a[1:]: j = a.index(i) while j > 0 and a[j-1] > a[j]: a[j], a[j-1] = a[j-1], a[j] j = j - 1 
0
source
 def my_sort(lst): a = [] for i in range(len(lst)): a.append(min(lst)) lst.remove(min(lst)) return a def my_revers_sort(lst):#in revers!!!!! a = [] for i in range(len(lst)): a.append(max(lst)) lst.remove(max(lst)) return a 
0
source

You can do this easily with the min () function

 'def asc(a): b=[] l=len(a) for i in range(l): x=min(a) b.append(x) a.remove(x) return b print asc([2,5,8,7,44,54,23])' 
0
source
 # Your current setup data_list = [-5, -23, 5, 0, 23, -6, 23, 67] new_list = [] # Sort function for i in data_list: new_list = [ x for x in new_list if i > x ] + [i] + [ x for x in new_list if i <= x ] 
0
source
 n = int(input("Input list lenght: ")) lista = [] for i in range (1,n+1): print ("A[",i,"]=") ele = int(input()) lista.append(ele) print("The list is: ",lista) invers = True while invers == True: invers = False for i in range (n-1): if lista[i]>lista[i+1]: c=lista[i+1] lista[i+1]=lista[i] lista[i]=c invers = True print("The sorted list is: ",lista) 
0
source

How can I continue the loop until len (new_list) = len (data_list)

 while len(new_list) != len(data_list): # ... 

Maybe so?

And no, there is no need to create a new list; most sorting algorithms work by modifying the list in place.

What you're probably trying to do is Sort sort with a separate list. See the Wikipedia article for more information on this sorting algorithm, and you will see how it works with a single list and how efficient it is (spoiler: it isnt).

-1
source
 def getIndexOfMaximum(list1): index = 0 emptyList = [] value = list1[0] c = 0 while (c == 0): for cell in list1: index += 1 if (cell >= value): value = cell hold = index -1 if (len(list1) == index): emptyList += [value] del list1[hold] index = 0 value = 0 if (len(list1) == 1): newList = emptyList + list1 del list1[index] c = 1 return newList print(getIndexOfMaximum([2,5,8,7,44,54,23])) #TRY THIS!!! 
-3
source

All Articles