博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
冒泡、选择、插入、归并
阅读量:4071 次
发布时间:2019-05-25

本文共 1336 字,大约阅读时间需要 4 分钟。

时间复杂度O(n*n):

冒泡:

class BubbleSort {public:    int* bubbleSort(int* A, int n) {        for(int i=0;i
A[j+1]) swap(A[j], A[j+1]); } } return A; }};

选择:

class SelectionSort {public:    int* selectionSort(int* A, int n) {        for (int i=0; i
A[j]) k=j; } if(k!=i) swap(A[i], A[k]); } return A; }};

插入:

class InsertionSort {public:    int* insertionSort(int* A, int n) {        for (int i=0; i
=0; --j) { swap(A[k], A[j]); k=j; } } return A; }};

时间复杂度O(n*Log(N))

归并:

递归:

class MergeSort {    void merge(int *A,int start1,int end1,int start2,int end2){        int cur = 0,curA=start1,curB=start2,len = end2 - start1 +1;        int *array = new int[end2-start1+1];        while (cur!=len) {            if(A[curA]
=end) return ; int mid = (start+end)/2; mergeSort2(A, start, mid); mergeSort2(A, mid+1, end); merge(A, start, mid, mid+1, end); }public: int* mergeSort(int* A, int n) { mergeSort2(A, 0, n-1); return A; }};
循环:

class MergeSort {public:    int* mergeSort(int* A, int n) {        int *B = new int[n];        int curB = 0;        for (int len = 1; len

转载地址:http://xehji.baihongyu.com/

你可能感兴趣的文章
winform中的状态栏,以及在状态栏目上显示时间
查看>>
给Winform添加登陆的form
查看>>
HttpWebRequest的一些认识
查看>>
HttpWebRequest发送Post数据
查看>>
HttpWebrequest来模拟登陆的全过程
查看>>
c#里面的覆盖
查看>>
DataGridView初试
查看>>
自定义DataGridView的复选框列,点击最后一个会自动多出来一行的解决
查看>>
SplitContainer的一些实际开发经验
查看>>
Log4net输出信息到RichTextBox
查看>>
在北大学习这几天
查看>>
一个关于Http的请求头Expect
查看>>
最近用C#写Winform的一个心得
查看>>
PHP中日期相加减
查看>>
Ext中RowExpander数据刷新
查看>>
Ext中tabpanel对面板的添加
查看>>
Ext中的选择器
查看>>
自己设计的一个PHP的MVC framework
查看>>
ext中联动combo远程加载选中的解决
查看>>
ie下对于window.location.href的跳转时获取不到referer的,php中的路径包含有未定式的
查看>>