• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / Python / How to Sort Numbers in Python Without Sort Function

How to Sort Numbers in Python Without Sort Function

July 25, 2022 Leave a Comment

To sort a list of numbers in Python without a sort function, you can define your own function and loop through the list swapping numbers based on their values.

def sort_without_sort(lst):
    for i in range(0, len(lst)):
        for j in range(i + 1, len(lst)):
            if lst[i] > lst[j]:
                lst[i], lst[j] = lst[j], lst[i]
    return lst

print(sort_without_sort([9,2,5,4,1,0,7,5]))

#Output:
[0, 1, 2, 4, 5, 5, 7, 9]

When working with collections of data in Python, the ability to easily be able to sort and order the data in the way we want is very valuable.

One such case is if you want to sort a list of numbers.

There are built-in functions such as sort() and sorted() that allow you to sort collections of data in Python, but you can also define your own function which will sort a list of numbers.

To sort numbers in Python without the sort() function, the key is to create a function which will loop over each number and compare each number to every other number in the list. Then at each iteration, we compare the two numbers and swap them if they are out of order.

Below is a user-defined function which will sort a list of numbers in Python.

def sort_without_sort(lst):
    for i in range(0, len(lst)):
        for j in range(i + 1, len(lst)):
            if lst[i] > lst[j]:
                lst[i], lst[j] = lst[j], lst[i]
    return lst

print(sort_without_sort([9,2,5,4,1,0,7,5]))

#Output:
[0, 1, 2, 4, 5, 5, 7, 9]

If you want to add the ability for the user to sort the list of numbers ascending or descending, we can add another argument and an if statement in the inner loop.

def sort_without_sort(lst, desc):
    for i in range(0, len(lst)):
        for j in range(i + 1, len(lst)):
            if desc:
                if lst[i] < lst[j]:
                    lst[i], lst[j] = lst[j], lst[i]
            else:
                if lst[i] > lst[j]:
                    lst[i], lst[j] = lst[j], lst[i]                
    return lst

print(sort_without_sort([9,2,5,4,1,0,7,5], False))
print(sort_without_sort([9,2,5,4,1,0,7,5], True))

#Output:
[0, 1, 2, 4, 5, 5, 7, 9]
[9, 7, 5, 5, 4, 2, 1, 0]

Hopefully this article has been useful for you to learn how to sort numbers in Python without the sort() function.

Other Articles You'll Also Like:

  • 1.  Get Day of Week from Datetime in pandas DataFrame
  • 2.  Python Delete Variable – How to Delete Variables with del Keyword
  • 3.  How to Check if String Contains Uppercase Letters in Python
  • 4.  How to Check if a Dictionary is Empty in Python
  • 5.  Python issubset() Function – Check if Set is Subset of Another Set
  • 6.  Using Python to Find Closest Value in List
  • 7.  Python acos – Find Arccosine and Inverse Cosine of Number
  • 8.  Get Quarter from Date in pandas DataFrame
  • 9.  Get First Character of String in Python
  • 10.  Python Replace Space with Underscore Using String replace() Function

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy

x