当前位置: 首页 > news >正文

怎么帮网站做支付接口搜索引擎

怎么帮网站做支付接口,搜索引擎,快速建企业网站,怎么做网上网站本内容是基于王道计算机数据结构的插入排序、冒泡排序、希尔排序、快速排序、简单选择排序整理。 文章目录 插入排序算法性能代码 冒泡排序算法性能代码 希尔排序算法性能代码 快速排序算法性能代码 简单选择排序算法性能代码 插入排序 算法 算法思想:每次将一个…

本内容是基于王道计算机数据结构的插入排序、冒泡排序、希尔排序、快速排序、简单选择排序整理。

文章目录

  • 插入排序
    • 算法
    • 性能
    • 代码
  • 冒泡排序
    • 算法
    • 性能
    • 代码
  • 希尔排序
    • 算法
    • 性能
    • 代码
  • 快速排序
    • 算法
    • 性能
    • 代码
  • 简单选择排序
    • 算法
    • 性能
    • 代码

插入排序

算法

算法思想:每次将一个待排序的记录按其关键字大小插入到前面已排好序的子序列中,直到全部记录插入完成。

性能

  • 空间复杂度:O(1)
  • 时间复杂度:
    • 最好:原本就有序;O(n)
    • 最坏:原本为逆序;O(n2);
    • 平均:O(n2);
  • 稳定性:稳定;

代码

image.png
image.png
image.png

#include <iostream>
using namespace std;void InsertSort(int a[],int n) {int i, j, temp;for(i = 1; i < n; i++) {if (a[i] < a[i - 1]) {temp = a[i];for (j = i - 1; j >= 0 && a[j] > temp; j--) {a[j + 1] = a[j];}a[j + 1] = temp;}}
}void printfarray(int a[], int n) {for (int i = 0; i < n; i++) {cout << a[i] << " ";}cout << endl;
}int main() {int a[8] = {38, 49, 65, 97, 76, 13, 27, 49};int n = 8;cout << "插入排序前的数组为: ";printfarray(a, n);cout << endl;InsertSort(a,n);cout << "插入排序后的数组为: ";printfarray(a, n);cout << endl;
}

image.png

冒泡排序

算法

  • 从后往前(或从前往后)两两比较相邻元素的值,若为逆序(即A[i-1]>A[i]),则交换它们,直到序列比较完。称这样过程为“一趟”冒泡排序。
  • 第一趟排序使关键字值最小的一个元素“冒”到最前面;
  • 每一趟排序都可以使一个元素移动到最终位置,已经确定最终位置的元素在之后的处理中无需再对比;
  • 若某一趟排序没有发生“交换”,说明此时已经整体有序。

性能

  • 空间复杂度:O(1)
  • 时间复杂度:
    • 最好:原本就有序;O(n)
    • 最坏:原本为逆序;O(n2);
    • 平均:O(n2);
  • 稳定性:稳定;

代码

image.png

#include <iostream>
using namespace std;void swap(int &a, int &b){int temp = a;a = b;b = temp;
}void BubbleSort(int a[],int n) {for(int i = 0; i < n-1; i++) {bool flag = false; // 表示本趟冒泡是否发生交换的标志for (int j = n - 1; j > i; j--) {if (a[j-1] > a[j]) {swap(a[j-1],a[j]);flag = true;}}if (flag == false) {return;}}
}void printfarray(int a[], int n) {for (int i = 0; i < n; i++) {cout << a[i] << " ";}cout << endl;
}int main() {int a[8] = {38, 49, 65, 97, 76, 13, 27, 49};int n = 8;cout << "冒泡排序前的数组为: ";printfarray(a, n);cout << endl;BubbleSort(a,n);cout << "冒泡排序后的数组为: ";printfarray(a, n);cout << endl;
}

image.png

希尔排序

算法

  • 先将待排序表分割成若干形如L[i, i+d, i+2d, … ,i + kd]的“特殊”子表,对各个子表分别进行直接插入排序。缩小增量d,重复上述过程,直到d=1为止。
  • 先追求表中元素部分有序,再逐渐逼近全局有序。

性能

  • 空间复杂度:O(1)
  • 时间复杂度:未知,但优于直接插入排序
  • 稳定性:不稳定;

代码

image.png
image.png
image.png
image.png

#include <iostream>
using namespace std;void ShellSort(int a[],int n) {int i, j, d, temp;for (d = n / 2; d >= 1; d = d / 2) {for(i = d; i < n; i++) {if(a[i] < a[i-d]) {temp = a[i];for(j = i - d; j >= 0 && a[j] > temp; j-=d) {a[j + d] = a[j];}a[j + d] = temp;}}}
}void printfarray(int a[], int n) {for (int i = 0; i < n; i++) {cout << a[i] << " ";}cout << endl;
}int main() {int a[11] = {38, 6, 9, 3, 49, 65, 97, 76, 13, 27, 49};int n = 11;cout << "希尔排序前的数组为: ";printfarray(a, n);cout << endl;ShellSort(a,n);cout << "希尔排序后的数组为: ";printfarray(a, n);cout << endl;
}

image.png

快速排序

算法

算法思想:在待排序表L[1 … n]中任取一个元素pivot作为枢轴(或基准,通常取首元素),通过一趟排序将待排序表划分为独立的两部分L[1 … k-1]和L[k+1 … n],使得L[1 … k-1]中的所有元素小于pivot,L[k+1 … n]中的所有元素大于等于pivot,则pivot放在了其最终位置L(k) 上,这个过程称为一次“划分”。然后分别递归地对两个子表重复上述过程,直至每部分内只有一个元素或空为止,即所有元素放在了其最终位置上。
算法表现主要取决于递归深度,若每次“划分”越均匀,则递归深度越低。“划分”越不均匀,递归深度越深。

性能

  • 空间复杂度:
    • 最好:O(n)
    • 最坏:O(log(n))
  • 时间复杂度:
    • 最好:每次划分很均匀;O(n2)
    • 最坏:原本为正序或逆序;O(n log(n));
    • 平均:O(n log(n));
  • 稳定性:不稳定;

代码

image.png
image.png

#include <iostream>
using namespace std;int Partition(int a[],int low, int high) {int pivot = a[low];while (low < high) {while(low < high && a[high] >= pivot) high--;a[low] = a[high];while(low < high && a[low] <= pivot) low++;a[high] = a[low];}a[low] = pivot;return low;
}void QuickSort(int a[],int low, int high) {if (low < high) {int pivotpos = Partition(a,low,high); // 划分QuickSort(a, low, pivotpos-1); // 划分左子表QuickSort(a, pivotpos + 1, high); // 划分右子表}
}void printfarray(int a[], int n) {for (int i = 0; i < n; i++) {cout << a[i] << " ";}cout << endl;
}int main() {int a[11] = {38, 6, 9, 3, 49, 65, 97, 76, 13, 27, 49};int n = 11;cout << "快速排序前的数组为: ";printfarray(a, n);cout << endl;QuickSort(a,0,n-1);cout << "快速排序后的数组为: ";printfarray(a, n);cout << endl;
}

image.png

简单选择排序

算法

  • 每一趟在待排序元素中选取关键字最小的元素加入有序子序列
  • 必须进行总共 n - 1 趟处理;

性能

  • 空间复杂度:O(1)
  • 时间复杂度:O(n2)
  • 稳定性:不稳定;

代码

image.png

#include <iostream>
using namespace std;void swap(int &a, int &b){int temp = a;a = b;b = temp;
}void SelectSort(int a[],int n) {for (int i = 0; i < n-1; i++) {int min = i;for(int j = i + 1; j < n; j++) {if (a[j] < a[min])min = j;}if (min != i) {swap(a[i],a[min]);}}
}void printfarray(int a[], int n) {for (int i = 0; i < n; i++) {cout << a[i] << " ";}cout << endl;
}int main() {int a[11] = {38, 6, 9, 3, 49, 65, 97, 76, 13, 27, 49};int n = 11;cout << "选择排序前的数组为: ";printfarray(a, n);cout << endl;SelectSort(a,n);cout << "选择排序后的数组为: ";printfarray(a, n);cout << endl;
}

image.png

http://www.ds6.com.cn/news/102371.html

相关文章:

  • 顶尖手机网站建设拉人注册给佣金的app
  • 小公司自己怎样做网站免费个人网站建站申请
  • 德清建设银行网站网络营销岗位描述的内容
  • 看英语做游戏的网站seo日常优化内容是什么
  • 如何网站做专题潍坊seo排名
  • 网站首页漂浮广告代码广州百度推广开户
  • 网站式的公司记录怎么做成都网站seo设计
  • 如何做网站好看可以推广的软件
  • 武汉光谷做网站的公司站内搜索工具
  • 网站开发教育培训中国万网官网登录
  • wordpress 回复后可见河南靠谱seo电话
  • 以后做网站发展前途佛山网站建设方案服务
  • 盘锦做网站电话四川百度推广和seo优化
  • 世界十大著名室内设计师西安企业网站seo
  • 为代理赌博做网站网络软文营销的案例
  • app 开发seo好学吗
  • 网站自己维护如何实施网站推广
  • 怎么增加网站的权重谷歌浏览器官网下载
  • 如何利用网站策划做好网站建设电商培训机构靠谱吗
  • 哈尔滨网站建设方案维护热点事件
  • 杭州杭州网站建设今日国际重大新闻事件
  • wordpress 进销存主题微软优化大师
  • 淮北网站开发公司网络营销策划的基本原则是什么
  • 中国外发加工网靠谱吗宁波关键词优化排名工具
  • 浙江建设厅网站 打不开上海网站建设费用
  • 国内互联网建站公司排名抖音seo优化公司
  • 东莞学做网站seo引擎搜索网址
  • 做网站需要什么许可证小红书seo排名帝搜软件
  • 南昌建设医院官方网站百度网页收录
  • 娄底网站建设公司百度收录提交入口网址是什么