本文共 1336 字,大约阅读时间需要 4 分钟。
时间复杂度O(n*n):
冒泡:
class BubbleSort {public: int* bubbleSort(int* A, int n) { for(int i=0;iA[j+1]) swap(A[j], A[j+1]); } } return A; }};
选择:
class SelectionSort {public: int* selectionSort(int* A, int n) { for (int i=0; iA[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时间复杂度O(n*Log(N))=0; --j) { swap(A[k], A[j]); k=j; } } return A; }};
归并:
递归:
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/