1. 冒泡排序(Bubble Sort)
这个算法的名字由来是因为越小的元素会经由交换慢慢“浮”到数列的顶端。
Bubble sort, sometimes referred to as sinking sort, is a simple sorting algorithm that repeatedly steps through the list, compares adjacent pairs and swaps) them if they are in the wrong order.
物理模拟的理论源头.
1.1 Pseudocodes Implementation
1 | #basics |
1.2 动图演示
1.3 代码实现
1 | # compare to get the small and swap |
2. 插入排序 Insertion Sort,
关键点是将第一项作为sorted sequence的分析方法.
2.1 Psedocodes
1 | i ← 1 |
2.2 动态演示
Sorted | Make room |
---|---|
2.3 代码实现
1 |
|
3. 选择排序 Selection Sort
Psuedocodes: 从剩下的序列中选择最小值, 逐个浮上来。
3.1 算法描述
There are many different ways to sort the cards. Here’s a simple one, called selection sort, possibly similar to how you sorted the cards above:
- Find the smallest card. Swap it with the first card.
- Find the second-smallest card. Swap it with the second card.
- Find the third-smallest card. Swap it with the third card.
- Repeat finding the next-smallest card, and swapping it into the correct position until the array is sorted.
3.2 动图演示
Effects | Demonstration |
---|---|
3.3 代码实现
1 |
|
3.4 希尔排序(Shell Sort)
希尔排序是希尔(Donald Shell)于1959年提出的一种排序算法。希尔排序也是一种插入排序,它是简单插入排序经过改进之后的一个更高效的版本,也称为缩小增量排序,同时该算法是冲破O(n2)的第一批算法之一。本文会以图解的方式详细介绍希尔排序的基本思想及其代码实现。
等闲来无事再来了解.
4. 归并排序 (Merge Sort)
In computer science, merge sort (also commonly spelled mergesort) is an efficient, general-purpose, comparison-based sorting algorithm. Most implementations produce a stable sort, which means that the order of equal elements is the same in the input and output. Merge sort is a divide and conquer algorithm that was invented by John von Neumannin 1945.[2] A detailed description and analysis of bottom-up mergesort appeared in a report by Goldstine and von Neumann as early as 1948.[3]
4.1 Pseudocodes
1 | #Top-down implementation |
4.2 动图演示
4.3 Implementation
1 | #psuedocodes方案 |
归并排序是一种稳定的排序方法。和选择排序一样,归并排序的性能不受输入数据的影响,但表现比选择排序好的多,因为始终都是O(nlogn)的时间复杂度。代价是需要额外的内存空间。
归并算法的代价就是需要额外的存储空间.
5. 快速排序(Quick Sort)
Quicksort (sometimes called partition-exchange sort) is an efficient sorting algorithm, serving as a systematic method for placing the elements of a random access file or an array in order. Developed by British computer scientist Tony Hoarein 1959[1] and published in 1961,[2] it is still a commonly used algorithm for sorting. When implemented well, it can be about two or three times faster than its main competitors, merge sort and heapsort.[3][contradictory]
5.1 算法描述
Quicksort is a divide and conquer algorithm. Quicksort first divides a large array into two smaller sub-arrays: the low elements and the high elements. Quicksort can then recursively sort the sub-arrays. The steps are:
- Pick an element, called a pivot, from the array.
- Partitioning: reorder the array so that all elements with values less than the pivot come before the pivot, while all elements with values greater than the pivot come after it (equal values can go either way). After this partitioning, the pivot is in its final position. This is called the partition operation.
- Recursively) apply the above steps to the sub-array of elements with smaller values and separately to the sub-array of elements with greater values.
5.2 动图演示
Effects | Demonstration |
---|---|
5.3 Implementation
1 | def quicksort(arr): |