Here, a single-loop Bubble Sort in Python:
def bubbly_sortish(data):
for _ in xrange(len(data)**2):
i, j = _/len(data), _%len(data)
if i<j and data[i] > data[j]:
data[i], data[j] = data[j], data[i]
A = [5, 1, 2, 3, 5, 6, 10]
bubbly_sortish(A)
print A
Of course this is a joke. But this shows the number of loops has little to do with algorithm complexity.
Now, if you're asking if it is possible to sort an array with O(n) comparisons, no, it's not possible. The lower bound is Ξ©(n log n) for comparison-based sorting algorithms.
Answer from Juan Lopes on Stack Overflowalgorithm - How to sort an array in a single loop? - Stack Overflow
computer science - How do you sort a list with a while loop in Python? - Stack Overflow
Sort a list in ascending order using loop
How to use for and while loop to sort in alphabetical order using python? - Stack Overflow
Here, a single-loop Bubble Sort in Python:
def bubbly_sortish(data):
for _ in xrange(len(data)**2):
i, j = _/len(data), _%len(data)
if i<j and data[i] > data[j]:
data[i], data[j] = data[j], data[i]
A = [5, 1, 2, 3, 5, 6, 10]
bubbly_sortish(A)
print A
Of course this is a joke. But this shows the number of loops has little to do with algorithm complexity.
Now, if you're asking if it is possible to sort an array with O(n) comparisons, no, it's not possible. The lower bound is Ξ©(n log n) for comparison-based sorting algorithms.
int list[] = { 45, 78, 22, 96, 10, 87, 68, 2 };
for (int i = 1; i < list.length; i++) {
if (list[i] < list[i - 1]) {
list[i] = list[i] + list[i - 1];
list[i - 1] = list[i] - list[i - 1];
list[i] = list[i] - list[i - 1];
i = 0;
}
}
System.out.print("Sorted array is : ");
for (int i = 0; i < list.length; i++) {
System.out.print(list[i] + " ");
}
You can create an empty list that would store your sorted numbers
a = [12,0,39,50,1]
kk = len(a)
new_a = []
i = 0
while i < kk:
xx = min(a) ## This would retreive the minimum value from the list (a)
new_a.append(xx) ## You store this minimum number in your new list (new_a)
a.remove(xx) ## Now you have to delete that minimum number from the list a
i += 1 ## This starts the whole process again.
print(new_a)
Please, note that I used the original length of the list a (kk) for the while statement so as not to stop the iteration because the length of the list a decreases as we delete the minimum numbers.
Following is the implementation of basic sorting using two while loops. In every iteration, the minimum element (considering ascending order) from the unsorted subarray is picked and moved to the sorted subarray. :
a=[12,0,39,50,1]
i=0
while i<len(a):
key=i
j=i+1
while j<len(a):
if a[key]>a[j]:
key=j
j+=1
a[i],a[key]=a[key],a[i]
i+=1
print(a)
you can do so:
for f in sorted(fruitList):
print(f)
and with while:
while fruitList:
print(min(fruitList))
fruitList.remove(min(fruitList))
But I mean, using "while" is a bad idea
P.S. sort() is not a function, It is a list method
The build in sort rutines does not require usage of neither for or While
If you want to learn for/while, look at some tutorials or something.
https://wiki.python.org/moin/ForLoop
To sort your list do something like this
fruitList = ["pear","apple","strawberry","banana","orange"]
print sorted(fruitList)
print fruitList
fruitList.sort()
print fruitList
output :
['apple', 'banana', 'orange', 'pear', 'strawberry']
['pear', 'apple', 'strawberry', 'banana', 'orange']
['apple', 'banana', 'orange', 'pear', 'strawberry']
I am trying to write a code to sort a random list of numbers between 0 and 1 without using any sorting functions. I can only use if, for loops, and while loops. Any advice on how I can do this?
[SOLVED]
I am currently coding a 3D-esque renderer (like the really old 3D games where you can only look side to side and not up and down). For some stuff related to objects depth, I need to render everything in the order of furthest away to closest. I have a list of depth values that specify how far away a wall has to render, and right now all I am doing is looping through that list without any sorting, and due to how that list was made, it makes all the walls render in the order from left to right. When using the list of depths to render the pixels, I know where the pixel should appear horizontally due to using the current index in the depth-list loop as the x position. If I however loop through the depth-list in the order of largest to smallest, then I won't be able to use the indices to specify the x position of the pixel that has to be colored in. I need to know what the index is in the unsorted depth-list for the current element in the depth-list that I am trying to render. Does anyone know how I could accomplish that?