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 Overflow
🌐
w3resource
w3resource.com β€Ί python-exercises β€Ί numpy β€Ί numpy-sort-elements-of-a-large-array-using-for-loop-and-optimization.php
Numpy - Sort elements of a large array using For loop and Optimization
September 1, 2025 - Write a NumPy program that creates a large NumPy array and write a function to sort its elements using a for loop. Optimize it using NumPy's sort() function. ... import numpy as np # Generate a large 1D NumPy array with random integers large_array ...
Discussions

algorithm - How to sort an array in a single loop? - Stack Overflow
So I was going through different sorting algorithms. But almost all the sorting algorithms require 2 loops to sort the array. The time complexity of Bubble sort & Insertion sort is O(n) for Bes... More on stackoverflow.com
🌐 stackoverflow.com
computer science - How do you sort a list with a while loop in Python? - Stack Overflow
How do you sort a list with a while loop? Having a bit of a problem, thanks very much in advance. a = [12,0,39,50,1] first = a[0] i = 0 j = 1 while i More on stackoverflow.com
🌐 stackoverflow.com
Sort a list in ascending order using loop
β€˜β€™'Write a Python program to sort a list in ascending order using loop. Input:- [100,10,1,298,65,483,49876,2,80,9,9213] Output:- [1,2,9,10,65,80,100,298,483,9213,49876] Using bubble sort lst = [100,10,1,298,65,483,49… More on discuss.python.org
🌐 discuss.python.org
1
0
August 7, 2023
How to use for and while loop to sort in alphabetical order using python? - Stack Overflow
I'm still trying to learn on python. I've created a list of fruit as follow fruitList = ["pear","apple","strawberry","banana","orange"] I would like to print them out in alphabetical order using for More on stackoverflow.com
🌐 stackoverflow.com
🌐
Codez Up
codezup.com β€Ί home β€Ί selection sort using for loop in python
Selection Sort using For loop in Python | Codez Up
March 13, 2021 - The argument which we pass to this function is an unordered list that is passed to this above function to perform Selection Sorting algorithm on this list and return sorted list back to function call.
🌐
Tutorialspoint
tutorialspoint.com β€Ί python β€Ί python_sort_arrays.htm
Python - Sort Arrays
import array as arr a = arr.array('i', ... sort method. We shall use it in the next example. First, declare an array and obtain a list object from it, using tolist() method....
🌐
Tutorial Gateway
tutorialgateway.org β€Ί python-program-to-sort-array-in-descending-order
Python Program to Sort Array in Descending Order
April 3, 2025 - Within the loop, the if statement (if (dearr[i] < dearr[j])) checks each item is less than other array items and assigns them to the temp variable. # Sort Array Descending import numpy as np dearr = np.array([11, 46, 22, 89, 77, 98, 55, 181, ...
Find elsewhere
🌐
Python.org
discuss.python.org β€Ί python help
Sort a list in ascending order using loop - Python Help - Discussions on Python.org
August 7, 2023 - β€˜β€™'Write a Python program to sort a list in ascending order using loop. Input:- [100,10,1,298,65,483,49876,2,80,9,9213] Output:- [1,2,9,10,65,80,100,298,483,9213,49876] Using bubble sort lst = [100,10,1,298,65,483,49876,2,80,9,9213] for i in range(len(lst)): for j in range(len(lst)-i-1): if lst[j] > lst[j+1]: lst[j], lst[j+1] = lst[j+1], lst[j] print(β€˜The sorted list is’, lst) print(β€œ*******”)β€˜β€™β€™ am unable to understand this code for j in range(len(lst)-i-1): what -i -1 is doing
🌐
Tutorial Gateway
tutorialgateway.org β€Ί python-program-to-sort-array-in-ascending-order
Python Program to Sort Array in Ascending Order
August 20, 2026 - import numpy as np orarr = np.array([33, 4, 76, 98, 2, 88, 17, 122, 61]) print("***Sorting Numpy Array in Ascending Order***") print("Original Array = ", orarr) length = len(orarr) for i in range(length): for j in range(i + 1, length): if (orarr[i] > orarr[j]): temp = orarr[i] orarr[i] = orarr[j] orarr[j] = temp print("Array in Ascending Order = ", orarr)
🌐
Tutorial Gateway
tutorialgateway.org β€Ί python-program-to-sort-list-in-ascending-order
Python Program to Sort List in Ascending Order
August 21, 2026 - In this program, we are using Nested For Loop to iterate each number in a List, and sort them in ascending order. For more examples, refer to the Python Basic Programs.
🌐
YouTube
youtube.com β€Ί watch
How to sort a list in python using For Loop - YouTube
Watch this video from Joey to learn how to sort a list in python using for loopThis is a simple sorting process of sorting a list in python without sort() me...
Published: March 12, 2022
🌐
Dot Net Perls
dotnetperls.com β€Ί sort-python
Python - sort Examples - Dot Net Perls
May 30, 2025 - These are views that transform lists without modifying them. Here, we apply the sorted() view and then the reversed() view and print the results. Info We iterate over the reversed view with a for-loop.
🌐
Upgrad
upgrad.com β€Ί home β€Ί tutorials β€Ί software & tech β€Ί sort array in python
Effortless Array Sorting in Python: A Step-by-Step Guide
November 13, 2024 - For those keen on diving deeper into Python and mastering its intricacies, upGrad offers an array of courses tailored for professionals eager to upskill. Python provides the sort() method for lists and the sorted() function for all iterables to order elements. The sort() method modifies the list in place and doesn't create a new sorted list. Thus, it returns None. A bubble sort or insertion sort algorithm can be implemented using loops to manually sort an array.
🌐
Educative
educative.io β€Ί answers β€Ί how-to-sort-an-array-that-contains-1-to-n-values-in-python
How to sort an array that contains 1 to n values in Python
November 29, 2021 - Since the elements in the list are from 1 to n, we can fill the array one by one, like so: ... We use a for in range loop and go until n-1, where n is the length of the list, which can be calculated with len(lst).
🌐
EDUCBA
educba.com β€Ί home β€Ί software development β€Ί software development tutorials β€Ί python tutorial β€Ί python sort array
Python Sort Array | Examples on How Sort Array works in Python
April 13, 2023 - Now outer loop runs again, and inner loops for all values till all the values are sorted. β€˜len’ function calculates the length of the array, a number of times array will run, len – 1 used as array started from 0.
Address: Unit no. 202, Jay Antariksh Bldg, Makwana Road, Marol, Andheri (East),, 400059, Mumbai
🌐
Reddit
reddit.com β€Ί r/learnpython β€Ί how can i loop through a list in the order of smallest to largest, whilst still having an index for each element be their original index in the unsorted list?
r/learnpython on Reddit: How can I loop through a list in the order of smallest to largest, whilst still having an index for each element be their original index in the unsorted list?
January 9, 2023 -

[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?

🌐
Python Morsels
pythonmorsels.com β€Ί sorting-in-python
Sorting iterables in Python - Python Morsels
July 29, 2026 - >>> numbers = [4, 2, 7, 1, 5] >>> for n in sorted(numbers): ... print(n) ... 1 2 4 5 7 Β· This use of sorted makes it feel a little bit more like a looping helper, like the built-in enumerate, reversed, and zip functions.