├── docs ├── .vitepress │ ├── nav.ts │ ├── slide │ │ ├── 01_basic.ts │ │ └── 03_sort.ts │ └── config.ts ├── index.md ├── other │ └── 01_intro_others.md ├── basic │ └── 01_intro_html_css.md └── sort │ ├── 02_选择排序.md │ ├── 08_计数排序.md │ ├── 03_插入排序.md │ ├── 01_冒泡排序.md │ ├── 09_桶排序.md │ ├── 06_堆排序.md │ ├── 10_基数排序.md │ ├── 07_希尔排序.md │ ├── 05_快速排序.md │ └── 04_归并排序.md ├── README.md ├── .gitignore ├── package.json ├── .github └── workflows │ └── dep.yml └── LICENSE /docs/.vitepress/nav.ts: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /docs/index.md: -------------------------------------------------------------------------------- 1 | # Hello World 2 | demo -------------------------------------------------------------------------------- /docs/other/01_intro_others.md: -------------------------------------------------------------------------------- 1 | # 介绍其他技术 2 | -------------------------------------------------------------------------------- /docs/basic/01_intro_html_css.md: -------------------------------------------------------------------------------- 1 | # 介绍HTML-CSS 2 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # 分享编程技术, 成为一名合格的coder! 2 | Share various programming technologies to become a qualified coder. 3 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | logs 3 | *.log 4 | npm-debug.log* 5 | yarn-debug.log* 6 | yarn-error.log* 7 | pnpm-debug.log* 8 | lerna-debug.log* 9 | 10 | node_modules 11 | dist 12 | dist-ssr 13 | *.local 14 | 15 | # Editor directories and files 16 | .vscode/* 17 | !.vscode/extensions.json 18 | .idea 19 | .DS_Store 20 | *.suo 21 | *.ntvs* 22 | *.njsproj 23 | *.sln 24 | *.sw? -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "vitepress-project", 3 | "version": "1.0.0", 4 | "private": true, 5 | "type": "module", 6 | "scripts": { 7 | "dev": "vitepress dev docs", 8 | "build": "vitepress build docs", 9 | "serve": "vitepress serve docs" 10 | }, 11 | "devDependencies": { 12 | "@vuepress/plugin-search": "^1.9.8", 13 | "vitepress": "1.0.0-alpha.28", 14 | "vue": "3.2.44" 15 | }, 16 | "pnpm": { 17 | "peerDependencyRules": { 18 | "ignoreMissing": [ 19 | "@algolia/client-search" 20 | ] 21 | } 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /docs/.vitepress/slide/01_basic.ts: -------------------------------------------------------------------------------- 1 | export function slideBasic() { 2 | return [ 3 | { 4 | text: 'HTML基础', 5 | collapsed: false, 6 | items: [ 7 | { text: 'What is VitePress?', link: '/guide/what-is-vitepress' }, 8 | { text: 'Getting Started', link: '/guide/getting-started' }, 9 | { text: 'Configuration', link: '/guide/configuration' }, 10 | { text: 'Routing', link: '/guide/routing' }, 11 | { text: 'Deploying', link: '/guide/deploying' }, 12 | { text: 'Internationalization', link: '/guide/i18n' } 13 | ] 14 | }, 15 | ] 16 | } 17 | -------------------------------------------------------------------------------- /.github/workflows/dep.yml: -------------------------------------------------------------------------------- 1 | name: Deploy 2 | 3 | on: 4 | push: 5 | branches: 6 | - main 7 | 8 | jobs: 9 | deploy: 10 | runs-on: ubuntu-latest 11 | 12 | steps: 13 | - uses: actions/checkout@v2 14 | - name: Install dependencies 15 | run: npm install 16 | - name: Build 17 | run: npm run build 18 | - name: Setup SSH and deploy 19 | uses: easingthemes/ssh-deploy@v2.1.4 20 | with: 21 | args: -rltgoDzvO --delete 22 | env: 23 | SSH_PRIVATE_KEY: ${{ secrets.SSH_PRIVATE_KEY }} 24 | REMOTE_HOST: ${{ vars.REMOTE_HOST }} 25 | REMOTE_USER: ${{ vars.REMOTE_USER }} 26 | SOURCE: 'docs/.vitepress/dist' 27 | TARGET: '/root/tobecoder/' 28 | -------------------------------------------------------------------------------- /docs/.vitepress/slide/03_sort.ts: -------------------------------------------------------------------------------- 1 | export function slideSort() { 2 | return [ 3 | { 4 | text: '简单排序算法', 5 | collapsed: false, 6 | items: [ 7 | { text: '01_冒泡排序', link: '/sort/01_冒泡排序' }, 8 | { text: '02_选择排序', link: '/sort/02_选择排序' }, 9 | { text: '03_插入排序', link: '/sort/03_插入排序' } 10 | ] 11 | }, 12 | { 13 | text: "高级排序算法", 14 | collapsed: false, 15 | items: [ 16 | { text: '04_归并排序', link: '/sort/04_归并排序' }, 17 | { text: '05_快速排序', link: '/sort/05_快速排序' }, 18 | { text: '06_堆排序', link: '/sort/06_堆排序' }, 19 | { text: '07_希尔排序', link: '/sort/07_希尔排序' } 20 | ] 21 | }, 22 | { 23 | text: "其他排序算法", 24 | collapsed: false, 25 | items: [ 26 | { text: '08_计数排序', link: '/sort/08_计数排序' }, 27 | { text: '09_桶排序', link: '/sort/09_桶排序' }, 28 | { text: '10_基数排序', link: '/sort/19_基数排序' }, 29 | ] 30 | } 31 | ] 32 | } -------------------------------------------------------------------------------- /docs/.vitepress/config.ts: -------------------------------------------------------------------------------- 1 | import { defineConfig } from 'vitepress' 2 | import { slideBasic } from './slide/01_basic' 3 | import { slideSort } from './slide/03_sort' 4 | const pkg = require('../../package.json') 5 | 6 | // https://vitepress.vuejs.org/config/app-configs 7 | export default defineConfig({ 8 | title: "HTML+CSS最全最详细学习", 9 | description: "HTML+CSS最全最详细学习", 10 | head: [['meta', { name: 'theme-color', content: '#3c8772' }]], 11 | markdown: { 12 | headers: { 13 | level: [0, 0] 14 | } 15 | }, 16 | themeConfig: { 17 | nav: nav(), 18 | sidebar: { 19 | '/basic/': slideBasic(), 20 | '/sort/': slideSort() 21 | }, 22 | socialLinks: [ 23 | { icon: 'github', link: 'https://github.com/coderwhy' } 24 | ], 25 | algolia: { 26 | appId: 'Q9SP7FM66R', 27 | apiKey: '168419f13544ab4c72553d37c9b7cfc9', 28 | indexName: 'coderwhy' 29 | }, 30 | } 31 | }) 32 | 33 | function nav() { 34 | return [ 35 | { text: '最全最强最详细HTML+CSS', link: '../basic/01_intro_html_css', activeMatch: '/basic/' }, 36 | { text: '十大排序算法', link: '../sort/01_冒泡排序.md', activeMatch: '/sort/' }, 37 | { text: '其他技术(更新ing)', link: '../other/01_intro_others.md', activeMatch: '/other/' }, 38 | { 39 | text: `当前版本${pkg.version}`, 40 | items: [ 41 | { 42 | text: 'coderwhy疯狂更新ing', 43 | link: 'https://github.com/vuejs/vitepress/blob/main/CHANGELOG.md' 44 | } 45 | ] 46 | } 47 | ] 48 | } 49 | -------------------------------------------------------------------------------- /docs/sort/02_选择排序.md: -------------------------------------------------------------------------------- 1 | # TypeScript实现十大排序算法(二) - 选择排序 2 | 3 | ## 一. 选择排序的定义 4 | 5 | 选择排序(Selection Sort)是一种简单的排序算法。 6 | 7 | 它的基本思想是: 8 | 9 | * 首先在未排序的数列中找到最小(大)元素,然后将其存放到数列的起始位置; 10 | * 接着,再从剩余未排序的元素中继续寻找最小(大)元素,然后放到已排序序列的末尾。 11 | * 以此类推,直到所有元素均排序完毕。 12 | 13 | 选择排序的主要优点与数据移动有关。 14 | 15 | * 如果某个元素位于正确的最终位置,则它不会被移动。 16 | 17 | * 选择排序每次交换一对元素,它们当中至少有一个将被移到其最终位置上,因此对n个元素的表进行排序总共进行至多n-1次交换。 18 | * 在所有的完全依靠交换去移动元素的排序方法中,选择排序属于非常好的一种。 19 | 20 | 选择排序的实现方式很简单,并且容易理解,因此它是学习排序算法的很好的入门途径。 21 | 22 | 23 | 24 | ## 二. 选择排序的流程 25 | 26 | 选择排序流程详细步骤: 27 | 28 | 1. 首先将要排序的数组复制到一个新数组中,这样原数组不会被改变。 29 | 2. 初始化最小数字的索引值为0,然后在数组中循环,在当前索引后面的元素中找到最小的数字的索引。 30 | 3. 如果当前索引位置的数字不是最小数字,那么将这两个数字互换。 31 | 4. 继续寻找下一个数字,直到索引到最后一个元素,此时整个数组已经是从小到大排序的了。 32 | 5. 重复上面的步骤,每次排序的范围都会减少一个,直到整个数组排序完毕。 33 | 34 | ![选择排序](https://coderwhy-1257727333.cos.ap-guangzhou.myqcloud.com/uPic/image-20230220154826956.png) 35 | 36 | 37 | 38 | ## 三. 选择排序的图解 39 | 40 | ![选择排序](https://coderwhy-1257727333.cos.ap-guangzhou.myqcloud.com/uPic/image-20230220154848891.png) 41 | 42 | ![选择排序](https://coderwhy-1257727333.cos.ap-guangzhou.myqcloud.com/uPic/Sorting_selection_sort_anim.png) 43 | 44 | 45 | 46 | ## 四. 选择排序的代码 47 | 48 | 以下是 TypeScript 实现的选择排序代码: 49 | 50 | ```ts 51 | function selectionSort(arr: number[]): number[] { 52 | // 循环遍历整个数组 53 | for (let i = 0; i < arr.length; i++) { 54 | // 预设最小数的索引为当前循环的索引 55 | let minIndex = i; 56 | // 在后面的数中寻找更小的数 57 | for (let j = i + 1; j < arr.length; j++) { 58 | if (arr[j] < arr[minIndex]) { 59 | // 如果找到更小的数,记录它的索引 60 | minIndex = j; 61 | } 62 | } 63 | // 如果当前循环的索引不是最小数的索引,交换它们 64 | if (i !== minIndex) { 65 | [arr[i], arr[minIndex]] = [arr[minIndex], arr[i]]; 66 | } 67 | } 68 | // 返回排序后的数组 69 | return arr; 70 | } 71 | 72 | // 测试数据 73 | const testArr = [5, 2, 9, 1, 5, 6]; 74 | // 调用插入排序函数 75 | const sortedArr = selectionSort(testArr); 76 | // 打印结果 77 | console.log(sortedArr); 78 | ``` 79 | 80 | 以下是代码的详细说明: 81 | 82 | 1. 首先循环遍历整个数组。 83 | 2. 在每一次循环中,预设最小数的索引为当前循环的索引。 84 | 3. 在后面的数中寻找更小的数,如果找到更小的数,记录它的索引。 85 | 4. 如果当前循环的索引不是最小数的索引,交换它们。 86 | 5. 重复步骤2-4,直到遍历完整个数组。 87 | 6. 返回排序后的数组。 88 | 89 | 90 | 91 | ## 五. 选择排序的时间复杂度 92 | 93 | 计算选择排序算法的时间复杂度,通常是通过分析算法中每一步的执行次数来确定的。 94 | 95 | 我们分析选择排序中的每一步,再将每一步的时间复杂度加起来,最后得到的就是选择排序的时间复杂度。 96 | 97 | * 在选择排序中,最多的操作是内层循环,其执行了N-1次,并且每次执行内层循环都要花费O(N)的时间。 98 | * 因此,内层循环的时间复杂度是O(N^2)。 99 | 100 | * 外层循环也要执行N-1次,因此,它的时间复杂度也是O(N^2)。 101 | * 所以,整个选择排序算法的时间复杂度是O(N^2)。 102 | 103 | 104 | 105 | 106 | 107 | ## 六. 选择排序的总结 108 | 109 | * 选择排序是一种简单易懂的排序算法。 110 | 111 | * 它的基本思想是遍历整个列表,每次找出最小的元素,并且将它移到列表的最左边,重复这个过程直到整个列表都有序排列。 112 | 113 | * 在平均情况下,选择排序的时间复杂度为 O(n^2),在最坏情况下与最好情况下都为 O(n^2)。 114 | 115 | * 选择排序在数据规模较小时非常适用,在数据规模较大时不够高效。 116 | 117 | 118 | 119 | 120 | 121 | -------------------------------------------------------------------------------- /docs/sort/08_计数排序.md: -------------------------------------------------------------------------------- 1 | # TypeScript实现十大排序算法(八) - 计数排序 2 | 3 | ## 一. 计数排序的定义 4 | 5 | 计数排序是一种非比较型整数排序算法。它的核心思想是对于待排序的数组,统计每个数出现的次数,然后根据次数从前往后依次输出。 6 | 7 | 计数排序的时间复杂度为O(n),这使得它比其他排序算法快得多,特别是当待排序的数据量很大,但是其空间复杂度却较高。 8 | 9 | 优点: 10 | 11 | 1. 时间复杂度低,只需要O(n)的时间复杂度。 12 | 2. 易于实现。 13 | 3. 对于数据范围很小的情况下,它可以达到稳定性。 14 | 15 | 缺点: 16 | 17 | 1. 空间复杂度高,需要额外的存储空间。 18 | 2. 仅适用于整数数据的排序。 19 | 3. 不适用于浮点数和负数的排序。 20 | 21 | 22 | 23 | ## 二. 计数排序的流程 24 | 25 | 计数排序的流程分析: 26 | 27 | 1. 首先确定数列的范围,并创建一个对应的桶列表,每个桶代表一个数列中的值; 28 | 2. 然后遍历原数列,并统计每个数列中值出现的次数,并将次数加入对应的桶中; 29 | 3. 接下来将桶列表转换为前缀和数组,即统计每个桶前面所有桶的数值总和; 30 | 4. 最后遍历原数列,对于每个数,将它插入对应的桶中的对应位置,位置确定的方法是:将该数的数值减去数列的最小值,然后加一,再在前缀和数组中找到该数的前一个数的位置,这个位置即为该数的插入位置。 31 | 32 | 通过这样的步骤,我们就可以得到一个从小到大排序的数列。 33 | 34 | 35 | 36 | 37 | 38 | ## 三. 计数排序的图解 39 | 40 | 整体流程: 41 | 42 | ![计数排序](https://coderwhy-1257727333.cos.ap-guangzhou.myqcloud.com/uPic/CountingSort.png) 43 | 44 | 45 | 46 | ## 四. 计数排序的代码 47 | 48 | 下面是TypeScript实现的计数排序代码,带有详细的注释: 49 | 50 | ```ts 51 | function countingSort(array: number[]): number[] { 52 | const n = array.length; 53 | 54 | // 计算数列最大值和最小值 55 | let max = array[0]; 56 | let min = array[0]; 57 | for (let i = 1; i < n; i++) { 58 | if (array[i] > max) { 59 | max = array[i]; 60 | } 61 | if (array[i] < min) { 62 | min = array[i]; 63 | } 64 | } 65 | 66 | // 统计数列中每个值出现的次数 67 | const count = new Array(max - min + 1).fill(0); 68 | for (let i = 0; i < n; i++) { 69 | count[array[i] - min]++; 70 | } 71 | 72 | // 累加数组 73 | for (let i = 1; i < count.length; i++) { 74 | count[i] += count[i - 1]; 75 | } 76 | 77 | // 从后向前遍历原始数组,按照统计数组中的位置放置元素 78 | const res = new Array(n); 79 | for (let i = n - 1; i >= 0; i--) { 80 | res[--count[array[i] - min]] = array[i]; 81 | } 82 | 83 | return res; 84 | } 85 | ``` 86 | 87 | 在上面的代码中,我们首先定义了一个名为countingSort的函数 88 | 89 | * 该函数接受两个参数:待排序数组arr和数组中元素的最大值maxValue。 90 | 91 | * 接下来,我们创建了一个大小为maxValue + 1的数组count,该数组用于统计数字出现的次数。 92 | 93 | * 接着,我们使用for循环对数组arr中的每个元素进行处理,并统计每个数字出现的次数。 94 | 95 | * 最后,我们遍历数组count,并使用for循环把它的元素按照统计的次数逐个输出到结果数组中,最终得到有序数组。 96 | 97 | 98 | 99 | 100 | 101 | ## 五. 计数排序的时间复杂度 102 | 103 | 计数排序的时间复杂度分析如下: 104 | 105 | * 预处理:建立一个计数数组,把输入数组中每个数字出现的次数存入计数数组。 106 | - 时间复杂度:O(k),其中k是数字的取值范围。 107 | 108 | * 累加:遍历计数数组,把每个数字出现的次数加上它前面数字出现的次数。 109 | - 时间复杂度:O(k) 110 | 111 | * 输出:遍历输入数组,把每个数字存入输出数组对应的位置。 112 | - 时间复杂度:O(n),其中n是输入数组的长度。 113 | 114 | 因此,计数排序的总时间复杂度为:O(k + n)。 115 | 116 | 计数排序对于长度较短,数字取值范围较小的数组是非常高效的。 117 | 118 | 119 | 120 | ## 六. 计数排序的总结 121 | 122 | 计数排序是一种特殊的排序算法: 123 | 124 | * 其不同于其他算法的地方在于它不依赖于比较,而是通过计算每个元素的出现次数,来确定每个元素的排名。 125 | 126 | 计数排序的复杂度为 O(n+k),其中 k 是数组中出现的数的范围。 127 | 128 | * 因此,计数排序非常适合数据范围较小的数组,并且可以很快地对数组进行排序。 129 | * 但是,由于需要额外的存储空间来存储每个数的出现次数,因此不适合大数据量的数组。 130 | 131 | 总的来说,计数排序是一种非常有效的排序算法,在适当的情况下可以使用。 132 | 133 | 134 | 135 | -------------------------------------------------------------------------------- /docs/sort/03_插入排序.md: -------------------------------------------------------------------------------- 1 | # TypeScript实现十大排序算法(三) - 插入排序 2 | 3 | ## 一. 插入排序的定义 4 | 5 | 插入排序就像是你打扑克牌,你从牌堆顶取一张牌,找到合适的位置插入到已有牌的顺序中,并不断重复这一步骤直到所有的牌都被插入到合适的位置,最终使得整副牌有序。 6 | 7 | 与打牌类似,插入排序(Insertion sort)的实现方法是: 8 | 9 | * 首先假设第一个数据是已经排好序的,接着取出下一个数据,在已经排好序的数据中从后往前扫描,找到比它小的数的位置,将该位置之后的数整体后移一个单位,然后再将该数插入到该位置。 10 | * 不断重复上述操作,直到所有的数据都插入到已经排好序的数据中,排序完成。 11 | 12 | ![打牌中的插入排序](https://coderwhy-1257727333.cos.ap-guangzhou.myqcloud.com/uPic/image-20230220155139386.png) 13 | 14 | 插入排序的优势在于它的性能表现在已经有序的序列上比冒泡排序、选择排序两种算法要好。 15 | 16 | * 它的时间复杂度为O(n),因此,如果序列已经被排好,插入排序将会比冒泡排序和选择排序快得多。 17 | 18 | * 另外,插入排序空间复杂度为O(1),因此,对于内存限制较小的情况,插入排序也是一个更优的选择。 19 | 20 | 21 | 22 | ## 二. 插入排序的流程 23 | 24 | 插入排序的流程如下: 25 | 26 | 1. 首先,假设数组的第一个元素已经排好序了,因为它只有一个元素,所以可以认为是有序的。 27 | 2. 然后,从第二个元素开始,不断与前面的有序数组元素进行比较。 28 | 3. 如果当前元素小于前面的有序数组元素,则把当前元素插入到前面的合适位置。 29 | 4. 否则,继续与前面的有序数组元素进行比较。 30 | 5. 以此类推,直到整个数组都有序。 31 | 6. 循环步骤2~5,直到最后一个元素。 32 | 7. 完成排序。 33 | 34 | ![插入排序](https://coderwhy-1257727333.cos.ap-guangzhou.myqcloud.com/uPic/image-20230220155157000.png) 35 | 36 | 37 | 38 | ## 三. 插入排序的图解 39 | 40 | ![插入排序](https://coderwhy-1257727333.cos.ap-guangzhou.myqcloud.com/uPic/image-20230220155216749.png) 41 | 42 | ![插入排序](https://coderwhy-1257727333.cos.ap-guangzhou.myqcloud.com/uPic/Sorting_insertion_sort_anim.png) 43 | 44 | 45 | 46 | ## 四. 插入排序的代码 47 | 48 | 以下是 TypeScript 实现的插入排序代码,带有详细的注释: 49 | 50 | ```ts 51 | function insertionSort(arr: number[]): number[] { 52 | // 对于数组的每一个元素,从它开始到0位置,比较该元素和前一个元素的大小 53 | for (let i = 1; i < arr.length; i++) { 54 | let current = arr[i]; 55 | let j = i - 1; 56 | // 如果该元素小于前一个元素,那么前一个元素向后移动,并继续向前比较 57 | while (j >= 0 && arr[j] > current) { 58 | arr[j + 1] = arr[j]; 59 | j--; 60 | } 61 | // 如果该元素大于前一个元素,那么它将放到合适的位置 62 | arr[j + 1] = current; 63 | } 64 | // 返回排序后的数组 65 | return arr; 66 | } 67 | 68 | // 测试数据 69 | const testArr = [5, 2, 9, 1, 5, 6]; 70 | // 调用插入排序函数 71 | const sortedArr = insertionSort(testArr); 72 | // 打印结果 73 | console.log(sortedArr); 74 | ``` 75 | 76 | 代码执行的过程: 77 | 78 | 1. 首先我们定义了一个 `insertSort` 函数,并传入一个数字数组作为参数。 79 | 2. 接着我们定义一个变量 `current`,它将存储当前需要比较的数字。 80 | 3. 然后我们使用一个循环,将数组的第二项到最后一项依次与前面的数字进行比较。 81 | 4. 在内层循环中,我们首先将 `j` 定义为 `i-1`,然后每次执行循环时,如果 `j` 大于等于 0 并且 `arr[j]` 大于 `current`,我们就交换 `arr[j]` 和 `arr[j + 1]` 的值。 82 | 5. 在循环结束后,我们将 `current` 插入到正确的位置,并继续比较下一个数字。 83 | 6. 当所有数字都被比较过后,我们就可以返回最终排序好的数组。 84 | 85 | 86 | 87 | ## 五. 插入排序的时间复杂度 88 | 89 | 插入排序的时间复杂度在最好的情况下为O(n),在最坏的情况下为O(n^2),平均时间复杂度为O(n^2)。 90 | 91 | 当数据已经有序时,插入排序只需要做n-1次比较和0次移动,运行时间为O(n); 92 | 93 | 当数据完全逆序时,插入排序需要做n-1趟比较和3/2*(n-1)^2/2次移动,运行时间为O(n^2)。 94 | 95 | 由于插入排序的最好时间复杂度与最坏时间复杂度都接近O(n^2),所以插入排序适用于数据规模不大的场合,如果数据规模很大,通常使用其他算法。 96 | 97 | 98 | 99 | 100 | 101 | ## 六. 插入排序的总结 102 | 103 | * 插入排序是一种简单而直观的排序算法,它可以快速地对部分有序的数组进行排序。 104 | 105 | * 插入排序通过比较相邻的元素并在需要时将其交换,来实现从小到大的排列。 106 | 107 | * 插入排序的时间复杂度在最好情况下是线性O(n),最坏情况下是O(n^2)。 108 | 109 | 110 | 总而言之,如果数组部分有序,插入排序可以比冒泡排序和选择排序更快。 111 | 112 | * 但是如果数组完全逆序,则插入排序的时间复杂度比较高,不如快速排序或归并排序。 113 | * 因此,在选择排序算法时,应该根据需要选择合适的算法。 114 | 115 | 116 | 117 | 118 | 119 | -------------------------------------------------------------------------------- /docs/sort/01_冒泡排序.md: -------------------------------------------------------------------------------- 1 | # TypeScript实现十大排序算法(一) - 冒泡排序 2 | 3 | ## 一. 冒泡排序的定义 4 | 5 | 冒泡排序是一种简单的排序方法。 6 | 7 | * 基本思路是通过两两比较相邻的元素并交换它们的位置,从而使整个序列按照顺序排列。 8 | * 该算法一趟排序后,最大值总是会移到数组最后面,那么接下来就不用再考虑这个最大值。 9 | * 一直重复这样的操作,最终就可以得到排序完成的数组。 10 | 11 | 这种算法是稳定的,即相等元素的相对位置不会发生变化。 12 | 13 | * 而且在最坏情况下,时间复杂度为O(n^2),在最好情况下,时间复杂度为O(n)。 14 | 15 | 因此,冒泡排序适用于数据规模小的场景。 16 | 17 | ![](https://coderwhy-1257727333.cos.ap-guangzhou.myqcloud.com/uPic/image-20230219183009587.png) 18 | 19 | 20 | 21 | ## 二. 冒泡排序的流程 22 | 23 | 冒泡排序的流程如下: 24 | 25 | 1. 从第一个元素开始,逐一比较相邻元素的大小。 26 | 2. 如果前一个元素比后一个元素大,则交换位置。 27 | 3. 在第一轮比较结束后,最大的元素被移动到了最后一个位置。 28 | 4. 在下一轮比较中,不再考虑最后一个位置的元素,重复上述操作。 29 | 5. 每轮比较结束后,需要排序的元素数量减一,直到没有需要排序的元素。 30 | 6. 排序结束。 31 | 7. 这个流程会一直循环,直到所有元素都有序排列为止。 32 | 33 | 34 | 35 | ## 三. 冒泡排序的图解 36 | 37 | ![image-20230219183028618](https://coderwhy-1257727333.cos.ap-guangzhou.myqcloud.com/uPic/image-20230219183028618.png) 38 | 39 | ![image-20230219183037940](https://coderwhy-1257727333.cos.ap-guangzhou.myqcloud.com/uPic/image-20230219183037940.png) 40 | 41 | ![冒泡排序](https://coderwhy-1257727333.cos.ap-guangzhou.myqcloud.com/uPic/Sorting_bubblesort_anim.png) 42 | 43 | 44 | 45 | ## 四. 冒泡排序的代码 46 | 47 | ```ts 48 | // 定义函数,用于实现冒泡排序算法 49 | function bubbleSort(arr: number[]): number[] { 50 | // 外层循环,控制需要比较的轮数 51 | for (let i = 0; i < arr.length - 1; i++) { 52 | // 内层循环,控制每轮需要比较的次数 53 | for (let j = 0; j < arr.length - 1 - i; j++) { 54 | // 如果前一个元素比后一个元素大,则交换它们的位置 55 | if (arr[j] > arr[j + 1]) { 56 | [arr[j], arr[j + 1]] = [arr[j + 1], arr[j]]; 57 | } 58 | } 59 | } 60 | // 返回排序后的数组 61 | return arr; 62 | } 63 | 64 | // 测试代码 65 | const arr = [3, 44, 38, 5, 47, 15, 36, 26, 27, 2, 46, 4, 19, 50, 48]; 66 | console.log(bubbleSort(arr)); 67 | // 输出:[2, 3, 4, 5, 15, 19, 26, 27, 36, 38, 44, 46, 47, 48, 50] 68 | ``` 69 | 70 | 说明: 71 | 72 | 1. 冒泡排序是一种暴力枚举算法,通过多次循环比较相邻的元素,把最大的元素逐渐冒泡到数组末端。 73 | 2. 外层循环:控制排序的趟数,每一轮排序会把最大的元素放到最后,因此每次循环需要比较的元素个数也会逐渐减少。 74 | 3. 内层循环:比较相邻元素,如果左边元素比右边元素大,则交换位置。 75 | 4. 冒泡排序是一种时间复杂度较高的算法,一般不用于大数据量的排序,但它很容易理解,是一种初学者学习排序算法的好 76 | 77 | 78 | 79 | ## 五. 冒泡排序的时间复杂度 80 | 81 | 在冒泡排序中,每次比较两个相邻的元素,并交换他们的位置,如果左边的元素比右边的元素大,则交换它们的位置。这样的比较和交换的过程可以用一个循环实现。 82 | 83 | * 在最好的情况下,数组已经是有序的,那么比较和交换的次数是最少的。 84 | * 在这种情况下,比较次数是n-1次,交换次数是0次,其中n是数组的长度。 85 | 86 | * 在最坏的情况下,数组是逆序的,那么比较和交换的次数是最多的。 87 | * 在这种情况下,比较次数是n-1次,交换次数是n(n-1)/2次,其中n是数组的长度。 88 | 89 | * 在平均情况下,比较和交换的次数取决于数组的排列方式。 90 | * 一般来说,平均情况下比较次数是n-1次,交换次数是n(n-1)/4次,其中n是数组的长度。 91 | 92 | 冒泡排序的时间复杂度分析: 93 | 94 | - 最好情况:当序列已经有序,每次比较和交换操作都不会进行,只需要进行n-1次比较,时间复杂度为O(n)。 95 | - 最坏情况:当序列完全逆序,需要进行n-1轮比较和n-1次交换操作,时间复杂度为O(n^2)。 96 | - 平均情况:需要进行的比较和交换操作的次数在所有情况中的平均值,时间复杂度也是O(n^2)。 97 | 98 | 由此可见,冒泡排序的时间复杂度主要取决于数据的初始顺序,最坏情况下时间复杂度是O(n^2),不适用于大规模数据的排序。 99 | 100 | 101 | 102 | 103 | 104 | ## 六. 冒泡排序的总结 105 | 106 | * 冒泡排序适用于数据规模较小的情况,因为它的时间复杂度为O(n^2),对于大数据量的排序会变得很慢。 107 | 108 | * 同时,它的实现简单,代码实现也容易理解,适用于学习排序算法的初学者。 109 | 110 | * 但是,在实际的应用中,冒泡排序并不常用,因为它的效率较低。 111 | 112 | * 此外,冒泡排序比较和交换的次数较多,占用更多的存储空间和时间,不适用于处理大数据量的情况。 113 | * 因此,在实际应用中,冒泡排序通常被更高效的排序算法代替,如快速排序、归并排序等。 114 | 115 | 116 | 117 | 118 | 119 | -------------------------------------------------------------------------------- /docs/sort/09_桶排序.md: -------------------------------------------------------------------------------- 1 | # TypeScript实现十大排序算法(九) - 桶排序 2 | 3 | ## 一. 桶排序的定义 4 | 5 | 桶排序是一种非比较的排序算法,通过分配数据到不同的桶中,最后对每个桶内的数据进行单独的排序或计数,最后将所有桶内的数据按顺序连接,即实现了排序的过程。 6 | 7 | 桶排序的基本思想是将数组分到有限数量的桶子里。 8 | 9 | * 每个桶子再个别排序(有可能再使用别的排序算法或是以递归方式继续使用桶排序进行排序)。 10 | * 它是鸽巢排序的一种归纳结果。 11 | 12 | 桶排序是一种特殊的计数排序,它是针对于元素的数量而不是元素的值。 13 | 14 | 桶排序的优点: 15 | 16 | * 速度快,时间复杂度为 O(n)。 17 | 18 | 桶排序的缺点: 19 | 20 | * 需要额外的内存空间。 21 | * 当待排序的数据的取值范围比较大的时候,桶的数量也会比较大,内存开销会比较大。 22 | * 对于浮点数排序比较麻烦。 23 | 24 | 25 | 26 | ## 二. 桶排序的流程 27 | 28 | 桶排序的流程如下: 29 | 30 | 1. 首先,需要确定桶的个数,并且确定每一个桶的数值范围,这两个信息是排序过程的关键。 31 | 2. 接下来,遍历数组中的每一个元素,并将其分配到相应的桶中。 32 | 3. 对每一个桶内的数据进行排序,可以使用其他的排序算法或者再次使用桶排序(下面代码中我使用的是插入排序)。 33 | 4. 最后,将每个桶中的数据依次取出,组成有序的数列。 34 | 35 | 36 | 37 | 38 | 39 | ## 三. 桶排序的图解 40 | 41 | 整体流程: 42 | 43 | ![桶排序](https://coderwhy-1257727333.cos.ap-guangzhou.myqcloud.com/uPic/ProxMapSortDemo.png) 44 | 45 | 46 | 47 | ## 四. 桶排序的代码 48 | 49 | 下面是TypeScript实现的桶排序代码,带有详细的注释: 50 | 51 | ```ts 52 | function bucketSort(arr: number[], bucketSize = 5) { 53 | if (arr.length === 0) { 54 | return arr; 55 | } 56 | 57 | // 找到数组中的最大值和最小值 58 | let i; 59 | let minValue = arr[0]; 60 | let maxValue = arr[0]; 61 | for (i = 1; i < arr.length; i++) { 62 | if (arr[i] < minValue) { 63 | minValue = arr[i]; 64 | } else if (arr[i] > maxValue) { 65 | maxValue = arr[i]; 66 | } 67 | } 68 | 69 | // 计算桶的数量 70 | let bucketCount = Math.floor((maxValue - minValue) / bucketSize) + 1; 71 | let buckets = new Array(bucketCount); 72 | for (i = 0; i < buckets.length; i++) { 73 | buckets[i] = []; 74 | } 75 | 76 | // 将数组中的元素分配到各个桶中 77 | for (i = 0; i < arr.length; i++) { 78 | buckets[Math.floor((arr[i] - minValue) / bucketSize)].push(arr[i]); 79 | } 80 | 81 | // 对每个桶中的元素进行排序 82 | arr.length = 0; 83 | for (i = 0; i < buckets.length; i++) { 84 | if (buckets[i].length > 0) { 85 | insertionSort(buckets[i]); 86 | for (let j = 0; j < buckets[i].length; j++) { 87 | arr.push(buckets[i][j]); 88 | } 89 | } 90 | } 91 | 92 | return arr; 93 | } 94 | 95 | // 插入排序 96 | function insertionSort(arr: number[]) { 97 | let i; 98 | let j; 99 | let temp; 100 | for (i = 1; i < arr.length; i++) { 101 | temp = arr[i]; 102 | j = i - 1; 103 | while (j >= 0 && arr[j] > temp) { 104 | arr[j + 1] = arr[j]; 105 | j--; 106 | } 107 | arr[j + 1] = temp; 108 | } 109 | return arr; 110 | } 111 | ``` 112 | 113 | 桶排序的代码整体实现如下: 114 | 115 | 1. 定义一个函数,接收一个数组作为参数,进行桶排序。 116 | 2. 定义一个辅助数组,并且初始化所有的元素为0。 117 | 3. 遍历原数组,对于每个元素,将辅助数组对应下标的元素加一。 118 | 4. 遍历辅助数组,并且将原数组的元素依次存储在辅助数组对应的位置上。 119 | 5. 将原数组的元素从辅助数组的末尾复制到原数组的开头。 120 | 121 | 122 | 123 | 124 | 125 | ## 五. 桶排序的时间复杂度 126 | 127 | 桶排序的复杂度分析主要是分析其两个主要部分的复杂度,即: 128 | 129 | * 桶的初始化: 在桶的初始化过程中,每个桶的创建都是常数时间的 130 | * 因此,初始化的时间复杂度是 O(n),其中 n 为要排序的数的个数。 131 | * 将数据放入桶: 对于每个数,它只需要被放入一次 132 | * 因此该部分的时间复杂度是 O(n)。 133 | 134 | 因此,桶排序的总时间复杂度是 O(n),这是一种非常高效的排序算法。 135 | 136 | 137 | 138 | ## 六. 桶排序的总结 139 | 140 | 桶排序是一种非比较型排序算法,它的工作原理是将数组分到有限数量的桶子里。 141 | 142 | * 每个桶子再个按照顺序排序,最后依次把不同桶子中的数据拼接起来。 143 | 144 | * 桶排序的优势在于它对于一定范围内的数据,具有非常高的排序效率,时间复杂度为O(n)。 145 | 146 | * 但是对于数据范围很大的情况,会造成空间浪费。 147 | 148 | 总的来说,桶排序是一种非常有效的排序方法,但它并不适用于所有场景,需要根据数据特点进行适当的选择。 149 | 150 | 151 | 152 | -------------------------------------------------------------------------------- /docs/sort/06_堆排序.md: -------------------------------------------------------------------------------- 1 | # TypeScript实现十大排序算法(六) - 堆排序 2 | 3 | ## 一. 堆排序的定义 4 | 5 | 堆排序(Heap Sort)是一种选择排序,它的特点是:对需要排序的数据建立一个堆,然后每次取出堆顶元素,直到堆为空。 6 | 7 | * 每次取出堆顶元素后,剩下的元素就是一个新的待排序的序列,因此,每次取出的元素都是序列中最大(最小)的元素。 8 | 9 | 在堆排序中,可以使用大根堆或小根堆。 10 | 11 | * 大根堆排序时,每次取出的都是最大的元素。 12 | * 小根堆排序时,每次取出的都是最小的元素。 13 | 14 | 堆排序的时间复杂度为 O(nlogn)。 15 | 16 | 17 | 18 | 学习堆排序之前最好先理解堆结构,这样更有利于对堆排序的理解。 19 | 20 | 21 | 22 | ## 二. 堆排序的流程 23 | 24 | **堆排序可以分成两大步骤:构建最大堆和排序** 25 | 26 | **构建最大堆:** 27 | 28 | ①遍历待排序序列,从最后一个非叶子节点开始,依次对每个节点进行调整。 29 | 30 | ②假设当前节点的下标为 i,左子节点的下标为 2i+1,右子节点的下标为 2i+2,父节点的下标为 (i-1)/2。 31 | 32 | ③对于每个节点 i,比较它和左右子节点的值,找出其中最大的值,并将其与节点 i 进行交换。 33 | 34 | ④重复进行这个过程,直到节点 i 满足最大堆的性质。 35 | 36 | ⑤依次对每个非叶子节点进行上述操作,直到根节点,这样我们就得到了一个最大堆。 37 | 38 | **排序:** 39 | 40 | ①将堆的根节点(也就是最大值)与堆的最后一个元素交换,这样最大值就被放在了正确的位置上。 41 | 42 | ②将堆的大小减小一,并将剩余的元素重新构建成一个最大堆。 43 | 44 | ③重复进行步骤 ① 和步骤 ②,直到堆的大小为 1,这样我们就得到了一个有序的序列。 45 | 46 | 47 | 48 | ## 三. 堆排序的图解 49 | 50 | ![堆排序](https://coderwhy-1257727333.cos.ap-guangzhou.myqcloud.com/uPic/image-20230220160342077.png) 51 | 52 | 整体流程: 53 | 54 | ![](https://coderwhy-1257727333.cos.ap-guangzhou.myqcloud.com/uPic/Sorting_heapsort_anim.png) 55 | 56 | 案例步骤: 57 | 58 | ![堆排序](https://coderwhy-1257727333.cos.ap-guangzhou.myqcloud.com/uPic/Heapsort-example.png) 59 | 60 | 61 | 62 | ## 四. 堆排序的代码 63 | 64 | 下面是TypeScript实现的堆排序代码,带有详细的注释: 65 | 66 | ```ts 67 | // 堆排序的函数 68 | function heapSort(arr: number[]) { 69 | // 先建立大根堆(从最后一个非叶子节点向上调整) 70 | for (let i = Math.floor(arr.length / 2) - 1; i >= 0; i--) { 71 | adjustHeap(arr, i, arr.length); 72 | } 73 | // 每次把堆顶的数与最后一个数交换,并重新调整堆 74 | for (let i = arr.length - 1; i > 0; i--) { 75 | [arr[0], arr[i]] = [arr[i], arr[0]]; 76 | adjustHeap(arr, 0, i); 77 | } 78 | return arr; 79 | } 80 | 81 | // 堆调整函数 82 | function adjustHeap(arr: number[], i: number, len: number) { 83 | let temp = arr[i]; 84 | // 将当前节点与其左右子节点比较,找出最大的那个 85 | for (let j = 2 * i + 1; j < len; j = 2 * j + 1) { 86 | if (j + 1 < len && arr[j + 1] > arr[j]) { 87 | j++; 88 | } 89 | // 如果子节点比父节点大,就交换 90 | if (arr[j] > temp) { 91 | arr[i] = arr[j]; 92 | i = j; 93 | } else { 94 | break; 95 | } 96 | } 97 | arr[i] = temp; 98 | } 99 | 100 | 101 | 102 | // 测试数据 103 | const testArr = [5, 2, 9, 1, 5, 6]; 104 | // 调用插入排序函数 105 | const sortedArr = heapSort(testArr); 106 | // 打印结果 107 | console.log(sortedArr); 108 | ``` 109 | 110 | 整个代码实现了快速排序的算法流程: 111 | 112 | - `heapSort` 函数是堆排序的主体函数,使用大根堆实现从小到大的排序 113 | - `adjustHeap` 函数是堆调整函数,用来调整大根堆,以保证堆顶的数是整个堆中最大的数 114 | 115 | 116 | 117 | 118 | 119 | ## 五. 堆排序的时间复杂度 120 | 121 | 堆排序的时间复杂度为O(nlogn),详细的计算过程如下: 122 | 123 | 堆排序的主要过程是建堆和排序。 124 | 125 | * 建堆的时间复杂度为O(n),因为需要对每一个节点都进行比较,以确保堆性质得到满足。 126 | 127 | * 排序的时间复杂度为O(nlogn),因为每一次排序都需要交换根节点和最后一个节点,并且重新堆化剩下的元素。 128 | * 这个过程的时间复杂度与树的高度相关,由于这是一个完全二叉树,所以高度为logn。 129 | * 因此,排序的总时间复杂度为O(nlogn)。 130 | 131 | 总的来说,堆排序的时间复杂度为O(nlogn),是一种高效的排序算法。 132 | 133 | 134 | 135 | ## 六. 堆排序的总结 136 | 137 | 堆排序是一种选择排序的改进版,利用了堆这种数据结构的性质。 138 | 139 | * 它的时间复杂度为O(nlogn),空间复杂度为O(1)。 140 | 141 | * 它的优点在于其不需要额外的数组,只需要在原数组上操作,因此空间复杂度比较低。 142 | 143 | * 同时,它还比较快,比较适合大规模数据的排序。不过,它的缺点是实现较为复杂,需要对堆数据结构有较深的了解。 144 | 145 | * 同时,在实际应用中,由于需要频繁的交换元素,因此在排序速度上可能比较慢。 146 | 147 | * 因此,需要根据实际情况选择排序方式。 148 | 149 | 150 | 151 | 152 | 153 | -------------------------------------------------------------------------------- /docs/sort/10_基数排序.md: -------------------------------------------------------------------------------- 1 | # TypeScript实现十大排序算法(十) - 基数排序 2 | 3 | ## 一. 基数排序的定义 4 | 5 | 基数排序是一种非比较型整数排序算法,其原理是将整数按位数分组,对每一位进行桶排序。 6 | 7 | * 基数排序从低位向高位排序,将整数分为个位、十位、百位等不同的数位。 8 | * 再将每一位数位上的数字分别放入桶中进行排序,最后将排好序的数字从桶中取出。 9 | 10 | 基数排序是桶排序的扩展,用于整数排序,与桶排序相比,其多了一重循环,用于不同数位的比较。 11 | 12 | 基数排序适用于整数排序,特别是位数大小不一的整数排序,比如电话号码、银行卡号等。 13 | 14 | 基数排序的优点: 15 | 16 | 1. 稳定性:基数排序是稳定的排序算法,排序后,相同的数字的相对位置不变。 17 | 2. 时间复杂度低:基数排序的时间复杂度为O(n * k),k为数字的位数。 18 | 3. 适用范围广:基数排序适用于数据范围很大的场景,如数字的位数不多。 19 | 20 | 基数排序的缺点: 21 | 22 | 1. 需要额外的存储空间:基数排序需要使用一些额外的存储空间存储桶,占用额外的空间。 23 | 2. 数据结构不同导致不能比较:基数排序只适用于整数数字,不能对其他数据结构进行排序。 24 | 25 | 26 | 27 | ## 二. 基数排序的流程 28 | 29 | 基数排序的流程分析: 30 | 31 | 1. 根据待排序的数的位数,确定需要进行的趟数,每一趟按照对应的位数进行排序。 32 | 2. 创建10个桶,分别代表0~9的数字,存储对应位数上数字为该数字的数。 33 | 3. 遍历待排序数组,将每一个数字按照其对应位数上的数字放入对应的桶中。 34 | 4. 按照桶的顺序依次遍历每一个桶,将桶中的数字存入辅助数组中。 35 | 5. 将辅助数组的数字复制回原数组中。 36 | 6. 如果还有多余的趟数,则从第2步开始重复上述操作,直到所有的趟数都已经结束。 37 | 7. 排序完成。 38 | 39 | 40 | 41 | 42 | 43 | ## 三. 基数排序的图解 44 | 45 | 整体流程: 46 | 47 | ![基数排序](https://coderwhy-1257727333.cos.ap-guangzhou.myqcloud.com/uPic/%E5%9F%BA%E6%95%B0%E6%8E%92%E5%BA%8F.png) 48 | 49 | 50 | 51 | ## 四. 基数排序的代码 52 | 53 | 下面是TypeScript实现的基数排序代码,带有详细的注释: 54 | 55 | ```ts 56 | function radixSort(arr: number[]) { 57 | const maxDigit = getMaxDigit(arr); // 获取最大位数 58 | 59 | for (let i = 0; i < maxDigit; i++) { 60 | let buckets: number[][] = []; // 创建桶 61 | 62 | for (let j = 0; j < arr.length; j++) { 63 | let digit = getDigit(arr[j], i); // 获取数字的第i位数字 64 | 65 | if (!buckets[digit]) { 66 | buckets[digit] = []; 67 | } 68 | 69 | buckets[digit].push(arr[j]); // 将数字放入相应的桶中 70 | } 71 | 72 | arr = [].concat(...buckets); // 将桶中的数字取出来,重新放入arr数组中 73 | } 74 | 75 | return arr; 76 | } 77 | 78 | // 获取最大位数 79 | function getMaxDigit(arr: number[]) { 80 | let max = 0; 81 | 82 | for (let i = 0; i < arr.length; i++) { 83 | let digit = getDigitCount(arr[i]); 84 | max = Math.max(max, digit); 85 | } 86 | 87 | return max; 88 | } 89 | 90 | // 获取数字的位数 91 | function getDigitCount(num: number) { 92 | if (num === 0) return 1; 93 | 94 | return Math.floor(Math.log10(Math.abs(num))) + 1; 95 | } 96 | 97 | // 获取数字的第i位数字 98 | function getDigit(num: number, i: number) { 99 | return Math.floor(Math.abs(num) / Math.pow(10, i)) % 10; 100 | } 101 | 102 | ``` 103 | 104 | 整体流程分为两步: 105 | 106 | 1. 从低位到高位依次排序,每一位排序时都是使用计数排序的思想。 107 | 2. 从最低位的排序结果开始,依次累加上一位的排序结果,得到最终的排序结果。 108 | 109 | 具体实现过程中,使用了一个二维数组作为桶,桶的第一维是数字的每一位,第二维是存储这一位数字相同的数字的数组。 110 | 111 | 使用了两个循环,外层循环进行排序的次数,内层循环进行元素的遍历。 112 | 113 | 最终,排序完成后,把结果存入原数组,完成排序。 114 | 115 | 116 | 117 | 118 | 119 | ## 五. 基数排序的时间复杂度 120 | 121 | 基数排序的时间复杂度分析: 122 | 123 | 1. 对于单次排序,基数排序的时间复杂度为 O(n),因为每个数都只需要进行一次排序。 124 | 2. 对于整个排序过程,基数排序的时间复杂度为 O(d(n + k)),其中 d 为位数,n 为数组长度,k 为桶的数量。 125 | * 在最坏情况下,当所有数的位数都相同时,d 与 n 的值相同,所以时间复杂度可以看做 O(n^2)。 126 | 127 | 基数排序的空间复杂度分析: 128 | 129 | 1. 基数排序的空间复杂度为 O(n + k),其中 n 为数组长度,k 为桶的数量。 130 | 2. 需要额外的数组存储排序的中间结果,空间复杂度为 O(n)。 131 | 132 | 133 | 134 | ## 六. 基数排序的总结 135 | 136 | 基数排序是一种非比较型整数排序算法,适用于大量数,很长的数列。它是桶排序的一种改进版本。它的基本思想是:将整数按位数切割成不同的数字,然后按每个位数分别比较。具体来说,就是将数列分别按个位,十位,百位... 的大小顺序排序,最后组合在一起。 137 | 138 | 优点: 139 | 140 | 1. 时间复杂度稳定,为O(n * k),其中n为数列的长度,k为数列中数的最大位数。 141 | 2. 空间复杂度小,只需要额外的常数空间。 142 | 3. 是稳定的排序算法,也就是说,相同的数字排序后仍然相同。 143 | 144 | 缺点: 145 | 146 | 1. 不适用于浮点数和负数。 147 | 2. 对于数据范围较大的数列,k的大小也很大,因此,时间复杂度可能较高。 148 | 149 | 总体来说,基数排序是一种非常有效的整数排序算法,特别是对于大量数,很长的数列。 150 | 151 | 152 | 153 | -------------------------------------------------------------------------------- /docs/sort/07_希尔排序.md: -------------------------------------------------------------------------------- 1 | # TypeScript实现十大排序算法(七) - 希尔排序 2 | 3 | ## 一. 希尔排序的定义 4 | 5 | 希尔排序是一种创新的排序算法,它的名字来源于它的发明者Donald Shell,1959年,希尔排序算法诞生了。 6 | 7 | * 在简单排序算法诞生后的很长一段时间内,人们不断尝试发明各种各样的排序算法,但是当时的排序算法的时间复杂度都是O(N²),看起来很难超越。 8 | 9 | * 当时计算机学术界充满了"排序算法不可能突破O(N²)"的声音,这与人类100米短跑不可能突破10秒大关的想法一样。 10 | 11 | * 但是,终于有一天,一位科学家发布了一种超越O(N²)的新排序算法,并以Shell的名字命名,以纪念这个历史里程碑。 12 | 13 | * 随后,还出现了许多可以超越O(N²)的排序算法,希尔排序作为其中之一,也是一种重要的排序算法。 14 | 15 | 希尔排序(Shell sort)是插入排序的一种改进版本,通过使用更大的步长来减少待排序列表中的元素,并最终通过使用步长为 1 的插入排序将数据有序化。 16 | 17 | 希尔排序的时间复杂度较为复杂,但是一般被认为是O(n^(3/2))或者O(n^(4/3))。 18 | 19 | 与其他排序算法相比,希尔排序具有较快的初始速度,但其最坏时间复杂度与插入排序相同。 20 | 21 | 22 | 23 | ## 二. 希尔排序的流程 24 | 25 | 回顾插入排序: 26 | 27 | - 由于希尔排序基于插入排序, 所以有必须回顾一下前面的插入排序. 28 | - 我们设想一下, 在插入排序执行到一半的时候, 标记符左边这部分数据项都是排好序的, 而标识符右边的数据项是没有排序的. 29 | - 这个时候, 取出指向的那个数据项, 把它存储在一个临时变量中, 接着, 从刚刚移除的位置左边第一个单元开始, 每次把有序的数据项向右移动一个单元, 直到存储在临时变量中的数据项可以成功插入. 30 | 31 | 插入排序的问题: 32 | 33 | - 假设一个很小的数据项在很靠近右端的位置上, 这里本来应该是较大的数据项的位置. 34 | - 把这个小数据项移动到左边的正确位置, 所有的中间数据项都必须向右移动一位. 35 | - 如果每个步骤对数据项都进行N次复制, 平均下来是移动N/2, N个元素就是 N*N/2 = N²/2. 36 | - 所以我们通常认为插入排序的效率是O(N²) 37 | - 如果有某种方式, 不需要一个个移动所有中间的数据项, 就能把较小的数据项移动到左边, 那么这个算法的执行效率就会有很大的改进. 38 | 39 | 希尔排序的做法: 40 | 41 | - 比如下面的数字, 81, 94, 11, 96, 12, 35, 17, 95, 28, 58, 41, 75, 15. 42 | - 我们先让间隔为5, 进行排序. (35, 81), (94, 17), (11, 95), (96, 28), (12, 58), (35, 41), (17, 75), (95, 15) 43 | - 排序后的新序列, 一定可以让数字离自己的正确位置更近一步. 44 | - 我们再让间隔位3, 进行排序. (35, 28, 75, 58, 95), (17, 12, 15, 81), (11, 41, 96, 94) 45 | - 排序后的新序列, 一定可以让数字离自己的正确位置又近了一步. 46 | - 最后, 我们让间隔为1, 也就是正确的插入排序. 这个时候数字都离自己的位置更近, 那么需要复制的次数一定会减少很多. 47 | 48 | image-20230205112442838 49 | 50 | 51 | 52 | 选择合适的增量: 53 | 54 | - 在希尔排序的原稿中, 他建议的初始间距是N / 2, 简单的把每趟排序分成两半. 55 | - 也就是说, 对于N = 100的数组, 增量间隔序列为: 50, 25, 12, 6, 3, 1. 56 | - 这个方法的好处是不需要在开始排序前为找合适的增量而进行任何的计算. 57 | - 我们先按照这个增量来实现我们的代码. 58 | 59 | 60 | 61 | 62 | 63 | ## 三. 希尔排序的图解 64 | 65 | 整体流程: 66 | 67 | ![File:Sorting shellsort anim2.gif](https://coderwhy-1257727333.cos.ap-guangzhou.myqcloud.com/uPic/Sorting_shellsort_anim2.png) 68 | 69 | 案例步骤: 70 | 71 | ![堆排序](https://coderwhy-1257727333.cos.ap-guangzhou.myqcloud.com/uPic/Heapsort-example.png) 72 | 73 | 74 | 75 | ## 四. 希尔排序的代码 76 | 77 | 下面是TypeScript实现的希尔排序代码,带有详细的注释: 78 | 79 | ```ts 80 | // TypeScript代码实现希尔排序 81 | function shellSort(arr: number[]) { 82 | // 定义增量, 每次分组, 增量为数组长度的一半 83 | let gap = Math.floor(arr.length / 2); 84 | while (gap > 0) { 85 | // 按组进行排序 86 | for (let i = gap; i < arr.length; i++) { 87 | // 获取当前元素 88 | let current = arr[i]; 89 | let j = i; 90 | // 将相邻元素比较, 满足条件就后移 91 | while (j >= gap && arr[j - gap] > current) { 92 | arr[j] = arr[j - gap]; 93 | j -= gap; 94 | } 95 | // 将当前元素插入合适的位置 96 | arr[j] = current; 97 | } 98 | // 每次递减增量, 直到为1 99 | gap = Math.floor(gap / 2); 100 | } 101 | return arr; 102 | } 103 | ``` 104 | 105 | 整个代码实现了希尔排序的算法流程: 106 | 107 | - 整段代码为一个函数,函数名为"shellSort",参数为待排序的数组"arr"。 108 | - 从最外层循环开始,设置gap的值为数组长度的一半,每次循环缩小gap的值,直到gap的值为1。 109 | - 在gap的控制下,进行插入排序,在gap的间隔内进行循环,在循环内部,比较相邻两个数的大小,若左边数大于右边数,则交换位置。 110 | - 整段代码最后一行,返回排好序的数组。 111 | 112 | 113 | 114 | 115 | 116 | ## 五. 希尔排序的时间复杂度 117 | 118 | 希尔排序的效率 119 | 120 | - 希尔排序的效率很增量是有关系的. 121 | - 但是, 它的效率证明非常困难, 甚至某些增量的效率到目前依然没有被证明出来. 122 | - 但是经过统计, 希尔排序使用原始增量, 最坏的情况下时间复杂度为O(N²), 通常情况下都要好于O(N²) 123 | 124 | Hibbard 增量序列 125 | 126 | - 增量的算法为2^k - 1. 也就是为1 3 5 7...等等. 127 | - 这种增量的最坏复杂度为O(N^3/2), 猜想的平均复杂度为O(N^5/4), 目前尚未被证明. 128 | 129 | Sedgewick增量序列 130 | 131 | - {1, 5, 19, 41, 109, … }, 该序列中的项或者是9*4^i - 9\*2^i + 1或者是4^i - 3*2^i + 1 132 | - 这种增量的最坏复杂度为O(N^4/3), 平均复杂度为O(N^7/6), 但是均未被证明. 133 | 134 | 总之, 我们使用希尔排序大多数情况下效率都高于简单排序, 甚至在合适的增量和N的情况下, 还好好于快速排序. 135 | 136 | 137 | 138 | ## 六. 希尔排序的总结 139 | 140 | 希尔排序是一种改进版的插入排序,从历史的角度来看,它是一种非常非常重要的排序算法,因为它解除了人们对原有排序的固有认知。 141 | 142 | 但是现在已经有很多更加优秀的排序算法:归并排序、快速排序等,所以从实际的应用角度来说,希尔排序已经使用的非常非常少了。 143 | 144 | 因为,我们只需要了解其核心思想即可。 145 | 146 | 147 | 148 | -------------------------------------------------------------------------------- /docs/sort/05_快速排序.md: -------------------------------------------------------------------------------- 1 | # TypeScript实现十大排序算法(五) - 快速排序 2 | 3 | ## 一. 快速排序的定义 4 | 5 | **快速排序**(Quicksort),又称**分区交换排序**(partition-exchange sort),简称**快排** 6 | 7 | * 由Tony Hoare在1959年发明。 8 | 9 | * 快速排序使用了分治的思想,将数组划分为两个子数组,每个子数组再分别进行排序,最终实现整个数组的排序。 10 | 11 | * 快速排序的特点是时间复杂度较好,平均情况下为O(nlogn)。 12 | 13 | * 另外,快速排序是一种原地排序算法,不需要额外的存储空间。 14 | 15 | 快速排序是一种非常流行的排序算法,因为它的时间复杂度和实现方式非常优秀。 16 | 17 | 快速排序广泛应用于各种场景,如数据库、搜索引擎等,其高效的排序速度和低空间复杂度使得它成为了一种非常重要的排序算法。 18 | 19 | ![快速排序的作者](https://coderwhy-1257727333.cos.ap-guangzhou.myqcloud.com/uPic/image-20230220155955664.png) 20 | 21 | 22 | 23 | ## 二. 快速排序的流程 24 | 25 | **快速排序的思路可以分解成以下几个步骤:** 26 | 27 | * ①首先,我们需要选择一个基准元素,通常选择第一个或最后一个元素作为基准元素。 28 | 29 | * ②然后,我们定义两个指针 i 和 j,分别指向数组的左右两端。 30 | 31 | * ③接下来,我们从右侧开始,向左移动 j 指针,直到找到一个小于或等于基准元素的值。 32 | 33 | * ④然后,我们从左侧开始,向右移动 i 指针,直到找到一个大于或等于基准元素的值。 34 | 35 | * ⑤如果 i 指针小于或等于 j 指针,交换 i 和 j 指针所指向的元素。 36 | 37 | * ⑥重复步骤 3-5,直到 i 指针大于 j 指针,这时,我们将基准元素与 j 指针所指向的元素交换位置,将基准元素放到中间位置。 38 | 39 | * ⑦接着,我们将数组分为两部分,左侧部分包含小于或等于基准元素的元素,右侧部分包含大于基准元素的元素。 40 | 41 | * ⑧然后,对左右两部分分别进行递归调用快速排序,直到左右两部分只剩下一个元素。 42 | 43 | * ⑨最终,整个数组就变得有序了。 44 | 45 | 46 | 47 | ## 三. 快速排序的图解 48 | 49 | ![快速排序](https://coderwhy-1257727333.cos.ap-guangzhou.myqcloud.com/uPic/image-20230220160155318.png) 50 | 51 | 整体流程: 52 | 53 | ![快速排序](https://coderwhy-1257727333.cos.ap-guangzhou.myqcloud.com/uPic/Sorting_quicksort_anim.png) 54 | 55 | 案例步骤: 56 | 57 | ![快速排序](https://coderwhy-1257727333.cos.ap-guangzhou.myqcloud.com/uPic/Quicksort-example.png) 58 | 59 | 60 | 61 | 62 | 63 | ## 四. 快速排序的代码 64 | 65 | 下面是TypeScript实现的快速排序代码,带有详细的注释: 66 | 67 | ```ts 68 | // 定义快速排序函数,参数为待排序的数组 69 | function quickSort(array: number[]): number[] { 70 | // 定义辅助函数,用于排序 71 | function sort(left: number, right: number): void { 72 | // 如果左边的索引比右边的索引大,说明区间内已经没有数据,退出函数 73 | if (left >= right) { 74 | return; 75 | } 76 | // 取出基准数 77 | let pivot = array[left]; 78 | // 定义两个指针 79 | let i = left; 80 | let j = right; 81 | // 开始排序 82 | while (i < j) { 83 | // 从右边开始搜索,直到找到比基准数小的数 84 | while (i < j && array[j] >= pivot) { 85 | j--; 86 | } 87 | // 如果找到了,则将该数存放在左边 88 | if (i < j) { 89 | array[i] = array[j]; 90 | i++; 91 | } 92 | // 从左边开始搜索,直到找到比基准数大的数 93 | while (i < j && array[i] <= pivot) { 94 | i++; 95 | } 96 | // 如果找到了,则将该数存放在右边 97 | if (i < j) { 98 | array[j] = array[i]; 99 | j--; 100 | } 101 | } 102 | // 将基准数存放在最终的位置上 103 | array[i] = pivot; 104 | // 递归处理基准数左边的数据 105 | sort(left, i - 1); 106 | // 递归处理基准数右边的数据 107 | sort(i + 1, right); 108 | } 109 | // 调用辅助函数,开始排序 110 | sort(0, array.length - 1); 111 | // 返回排序后的数组 112 | return array; 113 | } 114 | 115 | 116 | // 测试数据 117 | const testArr = [5, 2, 9, 1, 5, 6]; 118 | // 调用插入排序函数 119 | const sortedArr = quickSort(testArr); 120 | // 打印结果 121 | console.log(sortedArr); 122 | ``` 123 | 124 | 整个代码实现了快速排序的算法流程: 125 | 126 | 1. 在数组中选择一个元素作为基准元素(通常是数组的第一个元素) 127 | 2. 将小于等于基准元素的元素移到数组的左边,大于基准元素的元素移到数组的右边 128 | 3. 递归地对左半部分数组和右半部分数组进行快速排序 129 | 130 | 131 | 132 | ## 五. 快速排序的时间复杂度 133 | 134 | 快速排序的复杂度分析需要从时间复杂度和空间复杂度两个方面来考虑。 135 | 136 | 时间复杂度: 快速排序是一种分治思想的排序算法,它的时间复杂度取决于基准数的选取。 137 | 138 | * 最坏情况下,每次选取的基准数为序列中的最大或最小数,导致划分的两部分长度不平均,递归的次数将会达到O(n),因此时间复杂度为O(n^2)。 139 | 140 | * 最优情况下,每次选取的基准数将整个序列划分成两个长度大致相等的部分,递归的次数将会达到log2n,因此时间复杂度为O(nlogn)。 141 | 142 | * 平均情况下,时间复杂度为O(nlogn)。 143 | 144 | 空间复杂度: 快速排序是一种原地排序算法,它不需要额外的存储空间。 145 | 146 | * 在递归过程中,空间复杂度仅为递归调用的栈空间,因此空间复杂度为O(logn)。 147 | 148 | 总结: 快速排序的时间复杂度为O(nlogn),空间复杂度为O(logn),是一种高效的排序算法。 149 | 150 | 151 | 152 | ## 六. 快速排序的总结 153 | 154 | 快速排序是一种分治算法,它的思想是通过选定一个基准值,将数组分成两个部分,左边部分的元素都小于等于基准值,右边部分的元素都大于基准值,然后再递归地对左右两个部分进行快速排序。 155 | 156 | 快速排序的时间复杂度为 O(nlogn) 157 | 158 | * 在最坏情况下,时间复杂度可能退化为O(n^2)。 159 | * 但是它的平均时间复杂度是O(nlogn)。 160 | 161 | 快速排序的空间复杂度为 O(logn) 162 | 163 | * 因为它需要递归调用,但是它的空间复杂度与数据的大小并不相关,所以它不会导致内存使用方面的困难。 164 | 165 | 总的来说,快速排序是一种高效的排序算法,适用于大多数的排序场景。 166 | 167 | 168 | 169 | 170 | 171 | -------------------------------------------------------------------------------- /docs/sort/04_归并排序.md: -------------------------------------------------------------------------------- 1 | # TypeScript实现十大排序算法(四) - 归并排序 2 | 3 | ## 一. 归并排序的定义 4 | 5 | 归并排序(merge sort)是一种常见的排序算法: 6 | 7 | * 它的基本思想是将待排序数组分成若干个子数组。 8 | 9 | * 然后将相邻的子数组归并成一个有序数组。 10 | 11 | * 最后再将这些有序数组归并(merge)成一个整体有序的数组。 12 | 13 | 这个算法最早出现在1945年,由约翰·冯·诺伊曼(John von Neumann)(又一个天才,现代计算机之父,冯·诺依曼结构、普林斯顿结构)首次提出。 14 | 15 | * 当时他在为美国政府工作,研究原子弹的问题。 16 | 17 | * 由于当时计算机,他在研究中提出了一种高效计算的方法,这个方法就是归并排序。 18 | 19 | ![冯·诺依曼和奥本海默在第一台计算机前合影](https://coderwhy-1257727333.cos.ap-guangzhou.myqcloud.com/uPic/image-20230220155453454.png) 20 | 21 | 归并排序的基本思路是先将待排序数组递归地拆分成两个子数组,然后对每个子数组进行排序,最后将两个有序子数组合并成一个有序数组。 22 | 23 | * 在实现中,我们可以使用“分治法”来完成这个过程,即将大问题分解成小问题来解决。 24 | 25 | 归并排序的算法复杂度为 O(nlogn),是一种比较高效的排序算法,因此在实际应用中被广泛使用。 26 | 27 | 虽然归并排序看起来比较复杂,但是只要理解了基本思路,实现起来并不困难,而且它还是一个非常有趣的算法。 28 | 29 | 30 | 31 | 32 | 33 | ## 二. 归并排序的流程 34 | 35 | 归并排序是一种基于分治思想的排序算法,其基本思路可以分为三个步骤: 36 | 37 | 步骤一:分解(Divide):归并排序使用递归算法来实现分解过程,具体实现中可以分为以下几个步骤: 38 | 39 | * 如果待排序数组长度为1,认为这个数组已经有序,直接返回; 40 | 41 | * 将待排序数组分成两个长度相等的子数组,分别对这两个子数组进行递归排序; 42 | 43 | * 将两个排好序的子数组合并成一个有序数组,返回这个有序数组。 44 | 45 | 步骤二:合并(Merge):合并过程中,需要比较每个子数组的元素并将它们有序地合并成一个新的数组: 46 | 47 | * 可以使用两个指针 i 和 j 分别指向两个子数组的开头,比较它们的元素大小,并将小的元素插入到新的有序数组中。 48 | 49 | * 如果其中一个子数组已经遍历完,就将另一个子数组的剩余部分直接插入到新的有序数组中。 50 | 51 | * 最后返回这个有序数组。 52 | 53 | 步骤三:归并排序的递归终止条件: 54 | 55 | * 归并排序使用递归算法来实现分解过程,当子数组的长度为1时,认为这个子数组已经有序,递归结束。 56 | 57 | **总体来看,归并排序的基本思路是分治法,分成子问题分别解决,然后将子问题的解合并成整体的解。** 58 | 59 | 60 | 61 | 62 | 63 | ## 三. 归并排序的图解 64 | 65 | ![归并排序](https://coderwhy-1257727333.cos.ap-guangzhou.myqcloud.com/uPic/image-20230220155729680.png) 66 | 67 | ![归并排序](https://coderwhy-1257727333.cos.ap-guangzhou.myqcloud.com/uPic/image-20230220155744839.png) 68 | 69 | ![归并排序](https://coderwhy-1257727333.cos.ap-guangzhou.myqcloud.com/uPic/Merge-sort-example-300px.png) 70 | 71 | 72 | 73 | 74 | 75 | ## 四. 归并排序的代码 76 | 77 | 下面是TypeScript实现的归并排序代码,带有详细的注释: 78 | 79 | ```ts 80 | // 定义函数mergeSort,参数是待排序数组arr 81 | function mergeSort(arr: number[]): number[] { 82 | // 计算数组长度 83 | const n = arr.length; 84 | // 如果数组长度小于等于1,则直接返回该数组 85 | if (n <= 1) { 86 | return arr; 87 | } 88 | // 计算中间位置 89 | const middle = Math.floor(n / 2); 90 | // 对左边的数组进行归并排序 91 | const left = mergeSort(arr.slice(0, middle)); 92 | // 对右边的数组进行归并排序 93 | const right = mergeSort(arr.slice(middle)); 94 | // 合并两个排好序的数组 95 | return merge(left, right); 96 | } 97 | 98 | // 定义函数merge,参数是两个排好序的数组left和right 99 | function merge(left: number[], right: number[]): number[] { 100 | // 定义指针变量,分别指向两个数组的开头 101 | let i = 0, j = 0; 102 | // 定义一个空数组,用来存放合并后的数组 103 | const result = []; 104 | // 比较两个数组的第一个元素,将较小的放入result数组 105 | while (i < left.length && j < right.length) { 106 | if (left[i] < right[j]) { 107 | result.push(left[i++]); 108 | } else { 109 | result.push(right[j++]); 110 | } 111 | } 112 | // 将没有比较完的剩余元素放入result数组 113 | while (i < left.length) { 114 | result.push(left[i++]); 115 | } 116 | while (j < right.length) { 117 | result.push(right[j++]); 118 | } 119 | // 返回合并后的数组 120 | return result; 121 | } 122 | 123 | 124 | // 测试数据 125 | const testArr = [5, 2, 9, 1, 5, 6]; 126 | // 调用插入排序函数 127 | const sortedArr = mergeSort(testArr); 128 | // 打印结果 129 | console.log(sortedArr); 130 | ``` 131 | 132 | 代码执行的过程: 133 | 134 | 1. `mergeSort` 函数实现归并排序的递归调用,在该函数内,如果数组的长度小于等于1,直接返回该数组。 135 | 2. 如果数组的长度大于1,那么执行以下代码: 136 | - 先计算数组的中点,并将数组分为左右两半。 137 | - 递归调用左边和右边的数组,最终得到两个有序的数组。 138 | 3. `merge` 函数实现将两个有序的数组合并为一个有序的数组。 139 | 140 | 141 | 142 | ## 五. 归并排序的时间复杂度 143 | 144 | **复杂度的分析过程:** 145 | 146 | * 假设数组长度为 n,需要进行 logn 次归并操作; 147 | 148 | * 每次归并操作需要 O(n) 的时间复杂度; 149 | 150 | * 因此,归并排序的时间复杂度为 O(nlogn)。 151 | 152 | **最好情况:** **O(log n)** 153 | 154 | * 最好情况下,待排序数组已经是有序的了,那么每个子数组都只需要合并一次,即只需要进行一次归并操作。 155 | 156 | * 因此,此时的时间复杂度是 O(log n)。 157 | 158 | **最坏情况:** **O(nlogn)** 159 | 160 | * 最坏情况下,待排序数组是逆序的,那么每个子数组都需要进行多次合并。 161 | 162 | * 因此,此时的时间复杂度为 O(nlogn)。 163 | 164 | **平均情况:** **O(nlogn)** 165 | 166 | * 在平均情况下,我们假设待排序数组中任意两个元素都是等概率出现的。 167 | 168 | * 此时,可以证明归并排序的时间复杂度为 O(nlogn)。 169 | 170 | 171 | 172 | ## 六. 归并排序的总结 173 | 174 | 归并排序是一种分治策略的排序算法,是利用分治的思想将一个大问题分成小问题,并在适当的地方合并它们以解决该问题的方法。 175 | 176 | 它是一种稳定的排序算法,时间复杂度为O(nlogn)。 177 | 178 | 归并排序使用了额外的空间,因此更适合处理大数据。 179 | 180 | * 归并排序的基本流程是通过递归将数组分成两半,分别进行递归排序,最终再进行合并。 181 | * 具体来说,将数组的中间元素作为分界点,分别对左右两边的数组进行排序,并在排序完成后进行合并。 182 | 183 | 归并排序的代码实现较为简单,但要注意关于递归函数和合并函数的实现。 184 | 185 | 归并排序是一种不需要过多研究的算法,适合于所有的排序场景。 186 | 187 | 188 | 189 | 190 | 191 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | --------------------------------------------------------------------------------