I guess 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)
#Added parenthesis
Answer from John La Rooy on Stack OverflowI guess 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)
#Added parenthesis
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]
sorting a list WITHOUT sort
How to sort an array without using another array?
Sorting arrays in python
arrays - Fastest way to sort in Python - Stack Overflow
Q1. Why is sorting important in Python?
Q2. Can we sort a list without sort()?
Q4. Which methods can replace sort()?
Videos
so, I started studying python via class, therefore we have assignments and I got stuck.we were given a list of names, and we need to write a code that prints the list in alphabetical order.
I think I can solve it with if, elif, else but I don't know where to start even. I've been stuck on this for 2 days now.
EDIT: we are NOT allowed to use any techniques they haven't taught us. so no sort of any kind, no use of a for while loop, not in range....
You are given an array of integers and an index x.
Without sorting Re-arrange the array as below:
elements less than array[x], followed by elements equal to array[x], fol-
lowed by elements greater than array[x]
Array, a = [3,5,2,6,8,4,4,6,4,4,3] and x = 5
Write a Python Program that re-arranges the above given array exactly
as shown below without using a sorting routine of any kind
output array = [3,2,3,4,4,4,4,5,6,8,6]
Here You are not allowed to use an extra array to solve the problem
I have an array of variable numbers.
I have another array that I want to be the of labels for the numbers array.
myList = [T1_sum, T2_sum, T3_sum, T4_sum, T5_sum, T6_sum, T7_sum, T8_sum, T9_sum, T10_sum]
mylables = ["cats", "dogs", "monkey", "fish", "rabbit", "owl", "pig", "goat", "cow", "bull"]
I want to sort the array by highest to lowest value. I did this by:
sortedlist = sorted(myList, reverse=True)
However, how do I rearrange the labels so that they are in line with the values?
I need to keep it so that a `sortedlabellist` is its own individual array (for future things). so i will probably have to link the values to the labels and then pull out a `sortedlabellist` separately?
If you are interested in asymptotic time, then counting sort or radix sort provide good performance.
However, if you are interested in wall clock time you will need to compare performance between different algorithms using your particular data sets, as different algorithms perform differently with different datasets. In that case, its always worth trying quicksort:
Copydef qsort(inlist):
if inlist == []:
return []
else:
pivot = inlist[0]
lesser = qsort([x for x in inlist[1:] if x < pivot])
greater = qsort([x for x in inlist[1:] if x >= pivot])
return lesser + [pivot] + greater
Source: http://rosettacode.org/wiki/Sorting_algorithms/Quicksort#Python
Since you know the range of numbers, you can use Counting Sort which will be linear in time.