Tag: sort()

  • Algorithms 101: Stable vs Unstable Sorting in Python

    When sorting algorithms are discussed, one important characteristic to consider is whether they are stable or unstable. This distinction can have significant implications, especially when dealing with data that contains equal elements where the relative order matters. 1. What is a Stable Sort? [English] A stable sort is a sorting…

  • Algorithms 101: Is Selection Sort Stable

    选择排序是稳定的吗? Understanding Stability in Sorting Algorithms 理解排序算法中的稳定性 In the context of sorting algorithms, stability refers to whether the algorithm preserves the relative order of records with equal keys (i.e., if two elements are equal, they should appear in the same order in the sorted output as they do in the…

  • Algorithms 101: Insertion Sort

    插入排序 (Insertion Sort) 插入排序是一种简单且高效的排序算法,适用于小规模数据集。其核心思想是在已排序部分中,从右到左找到合适的位置插入新来的元素,使已排序部分依然有序。这个过程重复进行,直到整个数组排序完成。 Insertion Sort is a simple and efficient sorting algorithm, especially for small datasets. The key idea is to insert each new element into its correct position within the already sorted portion by sliding it from right to left until it finds its place. This process is…

  • Algorithms 101: Selection Sort

    选择排序 (Selection Sort) 选择排序是一种简单直观的排序算法。它的基本思想是在未排序部分中选择最小(或最大)的元素,并将其放在已排序部分的末尾。这个过程持续进行,直到整个数组都被排序。下面将详细介绍选择排序的工作原理、代码实现以及时间复杂度。 Selection Sort is a simple and intuitive sorting algorithm. The basic idea is to repeatedly find the minimum (or maximum) element from the unsorted portion and move it to the end of the sorted portion. This process continues until the entire array is sorted. Below, we’ll…

  • Algorithms 101: Bubble Sort

    冒泡排序 (Bubble Sort) 冒泡排序是一种简单的排序算法,它通过反复比较相邻元素并交换它们的位置,将较大的元素“滚”到数组的末尾。这个过程重复进行,直到整个数组排序完成。每一轮比较后,最大的元素会“冒泡”到当前未排序部分的最后一个位置。 Bubble Sort is a simple sorting algorithm that repeatedly compares adjacent elements and swaps them if they are in the wrong order. This causes the larger elements to "bubble" to the end of the array. The process is repeated until the entire array is sorted. After…

  • Python 101: sort() vs sorted() in Python

    In Python sort() vs sorted() When sorting lists in Python, you can use either the sort() method or the sorted() function. Below is a detailed comparison of these two approaches, including code examples, tips, and warnings. Comparison Table Feature sort() Method sorted() Function Modifies Original List Yes No Returns None…