
Sorting Methods in Arrays
lets first understand what sorting means sorting means arranging elements in a particular order (mostly ascending or descending) example [5, 2, 3, 1] → [1, 2, 3, 5] there are many ways to sort an array, and each method works differently why do we need sorting sorting helps in: searching faster organizing data solving many problems like binary search, merging, etc types of sorting methods in this blog, I will go through some common sorting methods Bubble Sort Selection Sort Insertion Sort Merge Sort Quick Sort 1. Bubble Sort the bubble sort compares adjacent elements and swaps them if they are not in order, largest element “bubbles” to the end example [5, 2, 3] 5 > 2 → swap → [2,5,3] 5 > 3 → swap → [2,3,5] code def bubblesort ( 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 complexity Time Complexity: O(n²) Space Complexity: O(1) 2. Selection Sort selection sort f
Continue reading on Dev.to Python
Opens in a new tab



