#include <iostream> using namespace std; void paopao_sort(int arr[], int nSize) { for(int i=0;i<nSize;i++) { for(int j=0;j<nSize-1-i;j++) { if(arr[j] > arr[j+1]) { int nTmp = arr[j]; arr[j] = arr[j+1]; arr[j+1] = nTmp; } } } } void select_sort(int arr[], int nSize) { for(int i=0;i<nSize;i++) { for(int j=i;j<nSize;j++) { if(arr[i] > arr[j]) { int nTmp = arr[i]; arr[i] = arr[j]; arr[j] = nTmp; } } } } void insert_sort(int arr[], int nSize) { for(int i=0;i< nSize;i++) { int nTmp = arr[i]; int j = i; for(;j>0 && arr[j-1] > nTmp;j--) { arr[j] = arr[j-1]; } arr[j] = nTmp; } } void quick_sort(int arr[], int low,int high) { if(low >= high) return; int first = low,last = high; int key = arr[first]; while(first < last){ while(first < last && arr[last] >= key){ --last; } arr[first] = arr[last]; while(first < last && arr[first] <= key){ ++first; } arr[last] = arr[first]; } arr[first] = key; quick_sort(arr, low, first -1); quick_sort(arr, first+1, high); } void merge(int sour[], int tmp[], int st, int md, int ed) { if(st >= ed) return; int i=st,j=md+1,k=st; while(i!=md+1 && j!=ed+1){ if(sour[i] > sour[j]){ tmp[k++] = sour[j++]; }else{ tmp[k++] = sour[i++]; } } while(i!=md+1){ tmp[k++] = sour[i++]; } while(j!=ed+1){ tmp[k++] = sour[j++]; } for(i=st;i<=ed;i++){ sour[i] = tmp[i]; } } void merge_sort(int sour[],int tmp[],int st, int ed){ if(st >= ed) return; int md = (st + ed)/2; merge_sort(sour,tmp,st,md); merge_sort(sour,tmp,md+1,ed); merge(sour,tmp,st,md,ed); } void heapAdjust(int arr[],int i,int nLen){ int nChild, nTmp; for(;i*2+1 < nLen;i = nChild){ nChild = i*2+1; if(nChild < nLen - 1 && arr[nChild] < arr[nChild +1]){ nChild ++; } if(arr[nChild] > arr[i]){ nTmp = arr[nChild]; arr[nChild] = arr[i]; arr[i] = nTmp; }else{ break; } } } void heap_sort(int arr[], int nLen) { for(int i=nLen/2-1;i>=0;i--){ heapAdjust(arr,i,nLen); } int nTmp = 0; for(int i=nLen-1;i>0;i--) { nTmp = arr[i]; arr[i] = arr[0]; arr[0] = nTmp; heapAdjust(arr,0,i); } } void heapAdjustS(int arr[], int i, int nLen) { int nChild, nTmp; for(;i*2+1 < nLen;i = nChild){ nChild = i*2+1; if(nChild < nLen -1 && arr[nChild] > arr[nChild+1] ){ nChild ++; } if(arr[i] > arr[nChild]){ nTmp = arr[nChild]; arr[nChild] = arr[i]; arr[i] = nTmp; }else{ break; } } } void heaps_sort(int arr[], int nLen) { for(int i=nLen/2-1;i>=0;i--){ heapAdjustS(arr,i,nLen); } int temp; for(int i=nLen-1;i>0;i--) { temp=arr[i]; arr[i]=arr[0]; arr[0]=temp; heapAdjustS(arr,0,i); } } struct tree{ int node; tree* left; tree* right; }; tree* treeAdjust(tree*& root, int key) { if(root == NULL){ root = new tree; root->node = key; root->left = root->right = NULL; return root; } if(root->node == key){ return root; }else if(root->node > key){ return treeAdjust(root->left, key); }else{ return treeAdjust(root->right, key); } } void treeDel(tree* root, int arr[], int& index) { if(root == NULL) return; if(root->left != NULL){ treeDel(root->left, arr, index); } arr[index++] = root->node; if(root->right != NULL){ treeDel(root->right, arr, index); } delete root; } void tree_sort(int arr[], int nLen) { tree* root = NULL; for(int i=0;i<nLen;i++) { treeAdjust(root,arr[i]); } int nIndex = 0; treeDel(root, arr, nIndex); } void arrCopy(int base[], int to[], int nLen) { for(int i=0;i<nLen;i++) to[i] = base[i]; } void arrShow(int arr[], int nLen) { for(int i=0;i<nLen;i++) cout<<arr[i]<<","; cout<<endl; } int main() { int base[] = {3,6,4,1,2,5,8,7,10,9}; int nLen = sizeof(base)/sizeof(int); int* ptr = new int[nLen]; cout<<"原始數組:"<<endl; arrShow(base, nLen); cout<<"冒泡:"<<endl; arrCopy(base, ptr, nLen); paopao_sort(ptr, nLen); arrShow(ptr, nLen); cout<<"選擇:"<<endl; arrCopy(base, ptr, nLen); select_sort(ptr, nLen); arrShow(ptr, nLen); cout<<"插入:"<<endl; arrCopy(base, ptr, nLen); insert_sort(ptr, nLen); arrShow(ptr, nLen); cout<<"快速:"<<endl; arrCopy(base, ptr, nLen); quick_sort(ptr, 0, nLen-1); arrShow(ptr, nLen); cout<<"歸併:"<<endl; arrCopy(base, ptr, nLen); int* pTmp = new int[nLen]; merge_sort(ptr, pTmp, 0, nLen-1); arrShow(ptr, nLen); delete[] pTmp; cout<<"最大堆:"<<endl; arrCopy(base, ptr, nLen); heap_sort(ptr, nLen); arrShow(ptr, nLen); cout<<"最小堆:"<<endl; arrCopy(base, ptr, nLen); heaps_sort(ptr, nLen); arrShow(ptr, nLen); cout<<"二叉樹:"<<endl; arrCopy(base, ptr, nLen); tree_sort(ptr, nLen); arrShow(ptr, nLen); delete[] ptr; return 0; }