/  Technology   /  Python   /  Write a sorting algorithm for a numerical dataset in Python.
writing a sort algorithm- Python - i2tutorials

Write a sorting algorithm for a numerical dataset in Python.

Sorting refers to arranging the given data in a particular format. A sorting algorithm specifies the way to arrange data in a particular format and order. It makes the data more readable and the data searching can be optimized to a very high level.

There are many kinds of sorting algorithms in python. These are:

  •  1. Bubble sort
  •  2. Merge sort
  •  3. Insertion sort
  •  4. Shell sort
  • 5.  Selection sort

A sorting algorithm for a given numerical dataset is given below:

def bubblesort(list):

# Swap the elements to arrange in order

    for iter_num in range(len(list)-1,0,-1):

        for idx in range(iter_num):

            if list[idx]>list[idx+1]:

                temp = list[idx]

                list[idx] = list[idx+1]

                list[idx+1] = temp

list = [19,2,31,45,6,11,121,27]



This is a comparison based algorithm where each pair of adjacent elements is compared and the elements are swapped if they are not in order.

Leave a comment