diff --git a/.DS_Store b/.DS_Store index dadf0fa..0d7a98a 100644 Binary files a/.DS_Store and b/.DS_Store differ diff --git a/数据结构/.DS_Store b/数据结构/.DS_Store index 7376a41..1bc079d 100644 Binary files a/数据结构/.DS_Store and b/数据结构/.DS_Store differ diff --git a/数据结构/code/BinaryInsertSort.cpp b/数据结构/code/BinaryInsertSort.cpp new file mode 100644 index 0000000..91de713 --- /dev/null +++ b/数据结构/code/BinaryInsertSort.cpp @@ -0,0 +1,45 @@ +/* + * @Description: 折半插入算法【伪代码】 + * @Version: Beta1.0 + * @Author: 【B站&公众号】Rong姐姐好可爱 + * @Date: 2020-04-15 18:27:59 + * @LastEditors: 【B站&公众号】Rong姐姐好可爱 + * @LastEditTime: 2021-03-27 12:19:13 + */ + +void BinaryInsertSort(ElemType Arr[],int n){ + + int i,j,lowIndex,hightIndex,midIndex; + + for(i=2;j<=n;i++){ + // 将待排序的元素暂存在Arr[0]上 + Arr[0]=Arr[i]; + + lowIndex=1; // 左侧子表 折半查找起始位置 + hightIndex=i-1; // 左侧子表 折半查找结束位置 + + while(lowIndex<=hightIndex){ + + // 左侧有序子表的中间位置角标 + midIndex=(lowIndex+hightIndex)/2; + + if(Arr[midIndex].key>Arr[0].key){ + // 小于中间元素,插入位置在子表左侧 + hightIndex=mid-1 + }else{ + // 大于或者等于中间元素,插入位置在子表右侧 + lowIndex=midIndex+1; + } + } + + // 跳出循环需要(lowIndex>hightIndex),说明待插入位置的角标在hightIndex之后,为 hightIndex+1,此时需要将(hightIndex,i)之间的所有元素后移 + + + for(j=i-1;j>hightIndex;--j){ + Arr[j+1]=Arr[j] + } + + // 后移完成后,将元素Arr[0]赋值到位置(hightIndex+1)上 + Arr[hightIndex+1]=Arr[0] + } +} diff --git a/数据结构/code/BinaryInsertSort.js b/数据结构/code/BinaryInsertSort.js new file mode 100644 index 0000000..7f6a267 --- /dev/null +++ b/数据结构/code/BinaryInsertSort.js @@ -0,0 +1,60 @@ +/* + * @Description: 折半插入排序【JavaScript版本】 + * @Version: Beta1.0 + * @Author: 【B站&公众号】Rong姐姐好可爱 + * @Date: 2021-03-27 12:35:17 + * @LastEditors: 【B站&公众号】Rong姐姐好可爱 + * @LastEditTime: 2021-03-27 12:50:00 + */ + + +/** + * 折半插入排序 + * @param {Array} arr 待排序数组 + * @param {int} len 数组长度 + */ +function binaryInsertSort(arr,len){ + + // 数组长度校验【非必须】 + + len=arr.length===len?len:arr.length + + + for(let i=1;ihighIndex;--j){ + arr[j+1]=arr[j] + } + arr[highIndex+1]=temp; + + } + + // 返回经过折半插入排序处理的有序数组 + return arr; +} + + + +const dealArr=[5,2,7,3,18,8,12,1] +console.log('插入排序前:',dealArr) +const sortResult=binaryInsertSort(dealArr,7) + +console.log('插入排序后:',sortResult) + diff --git a/数据结构/code/BubbleSort.cpp b/数据结构/code/BubbleSort.cpp new file mode 100644 index 0000000..9eafd90 --- /dev/null +++ b/数据结构/code/BubbleSort.cpp @@ -0,0 +1,59 @@ +/* + * @Description: 冒泡排序 + * @Version: Beta1.0 + * @Author: 【B站&公众号】Rong姐姐好可爱 + * @Date: 2021-03-31 08:24:18 + * @LastEditors: 【B站&公众号】Rong姐姐好可爱 + * @LastEditTime: 2021-04-06 07:26:15 + */ + + +void BubbleSwapSort(ElemType A[], int n){ + + for(i=0;ii;j--){ + if(A[j-1].key>A[j].key){ + + // 将两个元素A[j-1]、A[j]进行交换,有多种方法 + swap(A[j-1],A[j]) + // 确认已发生交换 + flag=true + } + } + + // 本趟遍历后没有发生交换,说明表已经有序 + if(flag==false){ + return ; + } + } +} + +/** + * 加减法实现两个元素值互换 + * + */ +void swap(int a, int b){ + // 此时a为两值的和 + a=a+b; + // 此时b的值为a + b=a-b + // 如何实现让a的值为b呢??此时a的值为b + a=a-b; +} + +/** + * 临时变量实现两个元素值的互换 + * + */ +void swap(int a,int b){ + int temp; + temp=a; + a=b; + b=temp +} + diff --git a/数据结构/code/BubbleSort.js b/数据结构/code/BubbleSort.js new file mode 100644 index 0000000..42c6d4f --- /dev/null +++ b/数据结构/code/BubbleSort.js @@ -0,0 +1,61 @@ +/* + * @Description: 冒泡排序【JavaScript版本】 + * @Version: Beta1.0 + * @Author: 【B站&公众号】Rong姐姐好可爱 + * @Date: 2021-04-06 07:26:59 + * @LastEditors: 【B站&公众号】Rong姐姐好可爱 + * @LastEditTime: 2021-04-06 08:01:19 + */ + + + +function BubbleSort (arr, len) { + // 校正数组的长度 + len = arr.length == len ? len : arr.length + + // 冒泡排序,让数组arr有序 + for (let i = 0; i < len - 1; i++) { + + let isSorted = false; + + // len个数组,进行len-1趟,即:一趟冒泡 + for (let j = len - 1; j > i; j--) { + // 注意:这里的for循环倒序是有讲究的,想象一下泡泡不都是网上升的么.... + if (arr[j - 1] > arr[j]) { + // 交换元素,始终让最小的元素往上走(冒泡) + const temp = arr[j - 1]; + arr[j - 1] = arr[j]; + arr[j] = temp + + // 需要冒泡 + isSorted = true + } + } + // 第一趟比较后,如果本身序列是有序的,就直接跳出循环 + if (isSorted === false) { + break; + } + } + + return arr +} + + +/** + * + * 加减法交换元素的值 + * 注意:JavaScript中使用需要考虑到作用域的问题 + * @param {int} a + * @param {int} b + */ +function swap (a, b) { + a = a + b; + b = a - b; + a = a - b; +} + + +const initArr = [1, 5, 8, 3, 2, 9, 16] +console.log(`冒泡排序前:${initArr}`) +const sortedArr = BubbleSort(initArr, 7); +console.log(`冒泡排序后:${sortedArr}`) \ No newline at end of file diff --git a/数据结构/code/QuickSort.cpp b/数据结构/code/QuickSort.cpp new file mode 100644 index 0000000..7eb0232 --- /dev/null +++ b/数据结构/code/QuickSort.cpp @@ -0,0 +1,47 @@ +/* + * @Description: 快速排序【伪代码】 + * @Version: Beta1.0 + * @Author: 【B站&公众号】Rong姐姐好可爱 + * @Date: 2020-03-23 08:23:20 + * @LastEditors: 【B站&公众号】Rong姐姐好可爱 + * @LastEditTime: 2021-04-08 21:51:28 + */ + + +void QuickSort(ElemType A[] , int low , int high){ + + // low > high 表角标越界,low=high 子表只有一个元素,不需要进行快排,已经有序 + if(low=pivot) --high + A[low]=A[high] // 比pivot小的都移到左表 注意--high 从后往前遍历 + + while(lowhigh 跳出循环后即找到能将当前表一分为二的pivotKey值 + A[low]=pivot + // 基准元素pivot对应最终的位置角标 + return low +} \ No newline at end of file diff --git a/数据结构/code/QuickSort.js b/数据结构/code/QuickSort.js new file mode 100644 index 0000000..d3813d1 --- /dev/null +++ b/数据结构/code/QuickSort.js @@ -0,0 +1,70 @@ +/* + * @Description: 快速排序【JavaScript版本】 + * @Version: Beta1.0 + * @Author: 【B站&公众号】Rong姐姐好可爱 + * @Date: 2021-04-08 08:20:35 + * @LastEditors: 【B站&公众号】Rong姐姐好可爱 + * @LastEditTime: 2021-04-08 21:50:12 + */ + + +/** + * 基于分治法思想,将数组进行快速排序 + * @param {Array} arr 待排序的数组 + * @param {int} low 数组低位角标 左指针 + * @param {int} high 数组高位角标 右指针 + * @returns + */ +function QuickSort(arr,low,high){ + + // low=high 说明只有一个元素,理解为有序,不做处理 + // low>high 说明左右指针已经重合,数组已经遍历完,需要跳出 + if(low= pivot ) --high; + arr[low]=arr[high] + + // 从左往右直到比pivot大跳出循环 + while(low=1;k=k/2){ + + // 增量子表进行直接插入排序 + for(i=k+1;i<=n;++i){ + + if(Arr[i].key0&&Arr[0].key=1;n/=2){ + + // // 步长为k,则对应分为k个组,分别对其进行 直接插入排序 + + for(i=1,i<=k;i++){ + + // 第一步: 对应组的元素找出来,组成新的待排序的数列 + // 第二步: 对待排序数列进行 直接插入排序 + + specialStraightInsertSort(ElemType Arr[], int n , int k , int i) + + } + + } + // 返回 + return Arr; +} + \ No newline at end of file diff --git a/数据结构/code/ShellSort.js b/数据结构/code/ShellSort.js new file mode 100644 index 0000000..7a2af34 --- /dev/null +++ b/数据结构/code/ShellSort.js @@ -0,0 +1,130 @@ +/* + * @Description: 希尔排序 + * @Version: Beta1.0 + * @Author: 【B站&公众号】Rong姐姐好可爱 + * @Date: 2020-02-21 08:07:13 + * @LastEditors: 【B站&公众号】Rong姐姐好可爱 + * @LastEditTime: 2021-03-28 11:37:29 + */ + + + +/** + * + * 数组的希尔排序 + * @param {Array} arr 待排序数组 + * @param {int} len 数组长度,可校验 + * @returns 返回已排序的数组,从小到大 + */ +function shellSort (arr, len) { + + // 校对数组长度 + len = arr.length === len ? len : arr.length + + // 注意处理浮点【向上取整】 防止空指针 + for (let increment = Math.floor(len / 2); increment >= 1; increment = Math.floor(increment / 2)) { + // 对每组数据,进行直接排序 + for (let groupIndex = 0; groupIndex < increment; ++groupIndex) { + + specialStraightInsertSort(arr, len, increment, groupIndex) + } + + } + + return arr +} + +/** + * 根据希尔排序的步长对分组进行直接插入排序处理 + * @param {Array} arr 排序数组 + * @param {int} len 数组长度 + * @param {int} increment 增量步长 + * @param {int} groupIndex 分组,第几个分组 + */ +function specialStraightInsertSort (arr, len, increment, groupIndex) { + len = arr.length === len ? len : arr.length + console.log(`数组长度:${len}----->当前步长:${increment}---->分组:${groupIndex}`) + + for(let eleStartIndex=groupIndex+increment;eleStartIndex=0&&arr[j]>temp;j-=increment){ + arr[j+increment]=arr[j] + } + arr[j+increment]=temp + } + + console.log('specialStraightInsertSort处理后:',arr) + return arr +} + + +/** + * 插入排序 + * @param{Array} arr 待排序的数组 + * @param{int} len 数组arr的长度,可以用arr.length()计算得到 + */ +function straightInsertSort (arr, len) { + // 重新确定数组长度 + len = arr.length === len ? len : arr.length; + + // 从第二个元素开始循环,共len-1次 + for (let i = 1; i < len; i++) { + + // 后面的额元素比前面的元素小,需要把前面大于哨兵元素有序序列,移动后面一位 + if (arr[i] < arr[i - 1]) { + let j; + // 哨兵元素 + const temp = arr[i]; + for (j = i - 1; arr[j] > temp; --j) { + // 后移 + arr[j + 1] = arr[j] + } + // 跳出循环逻辑,出现arr[j] > arr[j-1] + + // 哨兵即待排序的 + arr[j + 1] = temp + } + } + + return arr + +} + + +const dealArr = [5, 8, 2, 16, 3, 9, 1] +console.log('插入排序前:', dealArr) +const sortResult = shellSort(dealArr, 7) +console.log('插入排序后:', sortResult) + + +/** + * 简化的希尔排序 + * @param {Array} arr + * @returns 返回已排序号的数组,从小到大 + */ +function shellSortBetter (arr) { + var len = arr.length; + var increment = Math.floor(len / 2); + while (increment != 0) { + for (var i = increment; i < len; i++) { + var temp = arr[i] + for (var j = i - increment; j >= 0 && temp < arr[j]; j -= increment) { + arr[j + increment] = arr[j] + } + arr[j + increment] = temp; + } + increment = Math.floor(increment / 2) + } + return arr; +} + + +console.log('简化shellSortBetter希尔排序前:', dealArr) +const sortResultBetter = shellSortBetter(dealArr) +console.log('简化shellSortBetter希尔排序后:', sortResultBetter) \ No newline at end of file diff --git a/数据结构/code/StraightInsertSort.cpp b/数据结构/code/StraightInsertSort.cpp new file mode 100644 index 0000000..1827725 --- /dev/null +++ b/数据结构/code/StraightInsertSort.cpp @@ -0,0 +1,26 @@ +/* + * @Description: 直接插入排序【伪代码】 + * @Version: Beta1.0 + * @Author: 【B站&公众号】Rong姐姐好可爱 + * @Date: 2021-03-25 08:07:23 + * @LastEditors: 【B站&公众号】Rong姐姐好可爱 + * @LastEditTime: 2021-03-26 07:29:00 + */ +void straightInsertSort(ElemType A[], int n){ + int i,j; + + // 依次将前面的第2到第n个元素插入到前面的有序序列 + for(i=2;i<=n;i++){ + if(A[i].key< A[i-1].key){ + // 哨兵元素 + A[0]=A[i]; + // 循环向后挪动 + for(j=i-1;A[0].keytemp;--j){ + // 后移 + arr[j+1]=arr[j] + } + // 跳出循环逻辑,出现arr[j] > arr[j-1] + + // 哨兵即待排序的 + arr[j+1]=temp + } + } + + return arr + +} + +const dealArr=[5,2,7,3,18,8,12,1] +console.log('插入排序前:',dealArr) +const sortResult=straightInsertSort(dealArr,7) + +console.log('插入排序后:',sortResult) + +