赞
踩
冒泡排序:O{n2} 从后往前,两两比较换位
选择排序:O(n2) 选择最小的,与最前面的换位
插入排序:O(n2) 从前往后,两两比较换位
堆排序: O(nlogn) 使用树结构,树顶点最大,取出后重新排列剩余树。
归并排序:O(nlogn) 分两组,在组内继续细分两组,直到一个为一组,然后比较相邻两组,比较换位
快速排序:O(nlogn) 找基准值,分左右两侧,左侧比基准小,右侧比基准大,在两侧内继续执行快速排序。
冒泡排序:
- #include <iostream>
-
- using namespace std;
-
- void bubbleSort1(int list[], int length){
- if (list == NULL){
- cout << "list is null, return;";
- return ;
- }
- cout << "bubble sort, list length="<< length<<endl;
- for (int h = 0; h < length; h++){
- cout << list[h] << "\t";
- }
- cout <<endl;
- int temp;
- bool swapped = true;
- int runCount = 0;
- for (int j = 0; j < length && swapped; j++){
- swapped = false;
- for (int i = length - 1; i > 0 && (j == 0 || list[i] > list[j-1]); i--){
- cout << runCount++ <<" ";
- if (list[i] < list[i-1]){
- temp = list[i];
- list[i] = list[i-1];
- list[i-1] = temp;
- swapped =true;
- }
- }
- cout <<endl<< "run "<< j+1 <<"th round: \t";
- for (int k = 0; k < length; k++){
- cout << "\t" << list[k] ;
- }
- cout <<endl;
- }
- cout <<endl;
- cout <<endl;
- cout <<endl;
- }
-
- int bubbleSort2(int* list, int length){
- if (list == NULL || length < 2){
- cout << " list is NULL, or not need to sort!" <<endl;
- return -1;
- }
- int temp;
- int shouldRun = true;
- int swapPosition = 0;
- int len = length - 1;
- int runCount = 0;
- for (int i = 0; shouldRun && i < length; i++){
- int* tempList = list;
- shouldRun = false;
- for (int j=0; j < len; j ++){
- cout << runCount++ <<" ";
- if (*tempList > *(tempList + 1)){
- temp = *tempList;
- *tempList = *(tempList + 1);
- *(tempList + 1) = temp;
- shouldRun = true;
- swapPosition = j;
- }
- tempList ++;
- }
- len = swapPosition;
- cout <<endl<< "run "<< i+1 <<"th round: \t";
- for (int k = 0; k < length; k++){
- cout << "\t" << *(list+k) ;
- }
- cout <<endl;
- }
- return 0;
- }
-
-
- int main(){
- // int list[10] = {0,9,8,7,6,5,4,3,2,1};
- int list[10] = {9,8,7,6,5,4,3,2,1, 0};
- // int list[10] = NULL;
- /*
- cout << "please input 10 number to sort!" << endl;
- for(int j = 0; j<10;j++){
- cin >> list[j] ;
- }
- */
- bubbleSort1(list, sizeof(list)/sizeof(list[0]));
- // int list1[10] = {9,8,7,6,5,4,3,2,1,0};
- // int list1[10] = {0,0,0,0,0,0,0,0,0,0};
- int list1[10] = {0,0,0,0,0,1,0,0,0,0};
- bubbleSort2(list1, sizeof(list1)/sizeof(list1[0]));
-
- cout <<" Sorted number list is ";
- for (int i = 0; i < 10; i++){
- cout << list[i] << "\t";
- }
-
- cout <<endl;
- return 0;
- }

插入排序:
- #include <iostream>
-
- using namespace std;
-
- int insertSort(int array[], int length);
- void print(int array[], int length);
- int swapValue(int& a, int& b, int& temp);
-
- int main(){
- int array[] = {9,8,7,6,5,4,3,2,1,0};
- int length = sizeof(array)/sizeof(array[0]);
- insertSort(array, length);
- print(array, length);
- return 0;
- }
-
- int insertSort(int array[], int length){
-
- if (array == NULL || length < 2){
- return -1;
- }
- int temp;
- for (int i = 0; i < length; i++){
- for (int j = i; j > 0 && array[j] < array[j-1]; j--){
- swapValue(array[j], array[j-1], temp);
- }
-
- }
-
- return 0;
- }
-
-
- int swapValue(int& a, int& b, int& temp){
- temp = a;
- a = b;
- b = temp;
- return 0;
- }
- void print(int array[], int length){
-
- cout<<endl<< "print array, length="<<length<<endl;
- for(int i = 0; i< length;i++){
- cout<<"\t"<<array[i];
- }
- cout<<endl;
- }
-

归并排序:
- #include <iostream>
-
- using namespace std;
-
- int mergeSort(int array[], int length);
- void print(int array[], int length);
- int swapValue(int& a, int& b, int& temp);
- int doMerge(int* a, int leftLength, int* b, int rightLength);
-
- int count = 1;
-
- int main(){
- int array[] = {9,8,7,6,5,4,3,2,1,0};
- // int array[] = {9,8};
- int length = sizeof(array)/sizeof(array[0]);
- mergeSort(array, length);
- print(array, length);
- return 0;
- }
- int mergeSort(int array[], int length){
- if (array == NULL || length <2){
- return -1;
- }
- int subLength = length / 2;
- doMerge(array, subLength, array + subLength, length - subLength);
- return 0;
- }
- int doMerge(int* a, int leftLength, int* b, int rightLength){
- if (a == NULL || b == NULL){
- cout<<" array is null, please check!"<<endl;
- return -1;
- }
- int temp;
- // cout << "leftL="<<leftLength << " rightL="<<rightLength << endl;
- if (leftLength == 1 && rightLength == 1){
- cout <<endl<<count++<< " swap:"<<*a<<"--"<<*b<<endl;
- swapValue(*a, *b, temp);
- return true;
- }
- if (leftLength > 1){
- int subLLength = leftLength / 2;
- doMerge(a, subLLength, a + subLLength, leftLength - subLLength);
- }
- if (rightLength > 1){
- int subRLength = rightLength / 2;
- doMerge(b, subRLength, a + subRLength, rightLength - subRLength);
- }
- for (int i = 0; i < rightLength; i++){
- for (int j = leftLength - 1; j >= 0; j--){
- if (*(a+i) > *(b+j)){
- cout <<endl<<count++<< " swap:"<<*(a+i)<<"--"<<*(b+j)<<endl;
- swapValue(*(a+i), *(b+j), temp);
- }
- }
- }
- return 0;
- }
-
- void print(int array[], int length){
-
- cout<<endl<< "print array, length="<<length<<endl;
- for(int i = 0; i< length;i++){
- cout<<"\t"<<array[i];
- }
- cout<<endl;
- }
- int swapValue(int& a, int& b, int& temp){
- temp = a;
- a = b;
- b = temp;
- return 0;
- }

快速排序:
- #include <iostream>
- using namespace std;
-
- void print(int list[], int length);
- void print(int i, int j, int& runCount, int list[], int length);
- int swapValue(int& a, int& b);
- int quickSort2(int* list, int length);
- int quickSort3(int list[], int low, int high);
- int runQuickSort(int list[], int low, int high);
- int runQuickSort2(int list[], int low, int high);
-
-
-
-
- int main(){
- // int list1[] = {9,2,5,1,8,7,0,6,3,4};
- int list1[] = {0,1,2,3,4,5,6,7,8,9};
- // int list1[] = {9,8,7,6,5,4,3,2,1,0};
- // int list1[] = {3,2,0,1,4};
-
- int length = sizeof(list1)/sizeof(list1[0]);
- // quickSort2(list1, length);
- quickSort3(list1, 0, length - 1);
-
- cout <<endl<<"quick sort list: ";
- for (int h = 0; h< length; h++){
- cout << "\t"<<list1[h];
- }
- cout <<endl;
- return 0;
- };
-
- int quickSort2(int* list, int length){
- if (list == NULL || length < 2){
- return -1;
- }
- int temp;
- int* runList = list;
- int compare = *(runList + length - 1);
- int last = length - 2;
- int changePosition = -1;
- // cout <<endl<<"quick sort2: "<< length << " compare:"<< compare<<endl;
- // print(list, length);
- for (int i = 0; i <= last; i ++){
- if (*(runList+i) > compare){
- for (; last > i; last --){
- if(*(runList+last) < compare){
- swapValue(*(runList + i), * (runList + last));
- break;
- }
- }
- }
- changePosition = i;
- }
-
- // cout <<endl<<"quick sort2: "<< length << " changePosition:"<< changePosition <<endl;
-
- if (changePosition > -1 && changePosition < length){
- swapValue(*(list+changePosition), *(list+ + length - 1));
- }
- if(changePosition > 1 && changePosition < length){
- cout << "left side: "<<endl;
- print(list, changePosition);
- quickSort2(list, changePosition);
- }
- if (changePosition > -1 && changePosition < length - 2){
- cout << "right side: "<<endl;
- print(list + changePosition + 1, length - changePosition - 1);
- quickSort2(list + changePosition + 1, length - changePosition - 1);
-
- }
-
- return 0;
- }
-
- int quickSort3(int list[], int low, int high){
-
- if(low < high){
- // int comparePosition = runQuickSort(list, low, high);
- int comparePosition = runQuickSort2(list, low, high);
- cout<<endl<<"run begin:::";
-
- for(int h = low; h <high+1; h++){
- cout <<"\t"<<list[h];
- }
- // if (comparePosition > low + 1 && comparePosition < high){
- cout<<endl<<"left left ccc="<<comparePosition<<endl;
- quickSort3(list, low, comparePosition -1);
- // }
- // if (comparePosition > low && comparePosition < high - 1){
- cout<<endl<<"right right cccc==="<<comparePosition<<endl;
- quickSort3(list, comparePosition + 1, high);
- // }
- }
-
-
- return 0;
- }
-
-
- int runQuickSort(int list[], int low, int high){
- if (list != NULL && high <= low){
- return low;
- }
- cout <<endl<<"run quick sort begion " <<endl;
- for(int h = low; h <high+1; h++){
- cout <<"\t"<<list[h];
- }
- int i = low;
- int j = high - 1;
- int compareValue = list[high];
- while(i <= j){
- if (list[i] > compareValue){
- while(j > i){
- if (list[j] < compareValue){
- swapValue(list[i], list[j]);
- break;
- }
- j--;
- }
- }
- cout << endl << "i="<<i<<" j="<<j<<endl;
- if (i == j){
- swapValue(list[i],list[high]);
- cout <<endl<<"swap comapre is : "<<"i = "<<i <<"; value="<<list[i] << ", high value:"<< list[high] <<endl;
- break;
- }
- i++;
- }
- cout <<endl<<"run quick sort end,: "<<compareValue <<endl;
- for(int h = low; h <high+1; h++){
- cout <<"\t"<<list[h];
- }
- return i;
-
- }
-
- int runQuickSort2(int list[], int low, int high){
- int key = list[low];
- cout <<"run sort begin: low ="<<low<<" high="<<high<<" key="<<key<<endl;
- while(low < high){
- while (low < high && list[high] >= key){
- high --;
- }
- if (low < high){
- list[low] = list[high];
- }
- while(low < high && list[low] <= key){
- low++;
- }
- if (low < high){
- list[high] = list[low];
- }
- }
- list[low] = key;
- return low;
- }
-
- int swapValue(int& a, int& b){
-
- int temp = a;
- a = b;
- b = temp;
- return 0;
- }
- void print(int list[], int length){
- for (int h = 0; h< length; h++){
- cout << "\t"<<list[h];
- }
- cout <<endl;
-
- }
- void print(int i, int j, int& runCount, int list[], int length){
- cout <<"run count: "<< runCount++ <<"\t" << i<<" "<<j;
- cout <<endl;
- cout <<endl;
- for (int h = 0; h< length; h++){
- cout << "\t"<<list[h];
- }
- cout <<endl;
- }
-

Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。