Back to articles
Different Sorting Methodologies

Different Sorting Methodologies

via Dev.to PythonDharani

Introduction Sorting is a fundamental concept in programming and data structures. It is used to arrange data in a specific order, such as ascending or descending. Different sorting algorithms are used based on efficiency and use cases. What is Sorting? Sorting is the process of arranging elements in a particular order: Ascending order (small to large) Descending order (large to small) Types of Sorting Algorithms 1. Bubble Sort Repeatedly compares adjacent elements and swaps them if needed Code: python def bubble_sort(arr): n = len(arr) for i in range(n): for j in range(0, n-i-1): if arr[j] > arr[j+1]: arr[j], arr[j+1] = arr[j+1], arr[j] return arr ---- ### 2. Insertion sort def insertion_sort(arr): for i in range(1, len(arr)): key = arr[i] j = i - 1 while j >= 0 and arr[j] > key: arr[j+1] = arr[j] j -= 1 arr[j+1] = key return arr ------ ### 3. Selection sort def selection_sort(arr): for i in range(len(arr)): min_idx = i for j in range(i+1, len(arr)): if arr[j] < arr[min_idx]: min_idx =

Continue reading on Dev.to Python

Opens in a new tab

Read Full Article
6 views

Related Articles