Sorting Algorithms

A sorting algorithm is a method or process used to arrange elements in a particular order, typically in numerical or lexicographical order. Sorting is a fundamental operation in computer science and is used to organize data for more efficient searching, retrieval, and manipulation.

Example: Selection Sort

How it works:

  1. Find the smallest card. Swap it with the first item.

  2. Find the second-smallest card. Swap it with the second item.

  3. Find the third-smallest card. Swap it with the third item.

  4. Repeat finding the next-smallest card, and swapping it into the correct position until the array is sorted.

Python Code for Selection Sort:

def selectionSort(array):
    for i in range(len(array)):

        smallest = None
        location = 0
        
        for j in range(i+1, len(array)):
            if smallest is None:
                smallest = array[j]
                location = j
            elif array[j] < smallest:
                smallest = array[j]
                location = j
        # end of inner for
        
        if smallest < array[i]:
            # Python Swap
            array[i], array[location] = array[location], array[i]
    # end of outer loop
    return array
# end of selectionSort()

def selectionSort2(array):
    # A two list approach
    sorted_list = []
    
    while array:
        smallest = min(array)
        array.remove(smallest)
        sorted_list.append(smallest)
    
    return sorted_list
# end of selectionSort2()

What sorting algorithm does the Python use?

There is a built-in sorting function called sorted() and lists have a method called .sort()

Timsort is a hybridarrow-up-right, stablearrow-up-right sorting algorithmarrow-up-right, derived from merge sortarrow-up-right and insertion sortarrow-up-right, designed to perform well on many kinds of real-world data. It was implemented by Tim Petersarrow-up-right in 2002 for use in the Python programming languagearrow-up-right.

Wikipediaarrow-up-right

Timsort is classified to be faster than our example of a selection sort.

Using sorted()

Using .sort()

Connected Readings

Last updated