七大排序之选择排序
冒泡缺点是每次进入if语句后紧跟着就会发生swap交换,在这个交换的过程中会消耗时间,选择排序在每一轮外循环内最多进行一次swap循环;
选择排序 和 优化版的冒泡比较:
1 #include<iostream> 2 #include<time.h> 3 #include<stdlib.h> 4 #include<sys/timeb.h> 5 using namespace std; 6 const int Max = 5000; 7 8 void swap(int& a, int& b) { 9 int temp = a; 10 a = b; 11 b = temp; 12 } 13 long getSystemTime() { 14 struct timeb tb; 15 ftime(&tb); 16 return tb.time * 1000 + tb.millitm; 17 } 18 void Print(const int* arr, int length) { 19 for (int i = 0; i < length; i++) { 20 cout << arr[i] << " "; 21 } 22 23 } 24 25 void SelectSort(int *arr,int length) { 26 for (int i = 0; i < length - 1; i++) { 27 int min = i; 28 for (int j = i + 1; j < length; j++) { 29 if (arr[j] < arr[min]) min = j;//还是要俩俩比较,但是if成立后不立马发生交换二十记录较小数的下标 30 } 31 //内循环结束后会得到最小值的下标与当前arr[i]进行交换,所以减少了交换次数。。 32 swap(arr[i], arr[min]); 33 } 34 } 35 bool flag = false; 36 void Maopaosort(int* arr, int length) { 37 for (int i = 0; i < length - 1 && flag == false; i++) { 38 flag = true; //假设上一次排序后,已经完全有序了,如果这一轮排序没有发生内循环交换,表明真的有序则退出循环。 39 for (int j = 0; j < length - i - 1; j++) { 40 if (arr[j] > arr[j + 1]) { 41 swap(arr[j], arr[j + 1]); 42 flag = false; //表示在这一趟中发生了交换,则表示上一趟排序结果依然没有完全有序,但是也不能保证这一趟就完全有序了 43 } 44 45 } 46 } 47 } 48 int main() { 49 int arr[Max]; 50 int arr2[Max]; 51 srand((unsigned)time(NULL)); 52 for (int i = 0; i < Max; i++) { 53 arr[i] = rand() % Max; 54 arr2[i] = arr[i]; 55 } 56 long pt = getSystemTime(); 57 SelectSort(arr, Max); 58 long at = getSystemTime(); 59 cout << "\ntime of Selectsort:" << at - pt << "ms\n"; 60 pt = getSystemTime(); 61 Maopaosort(arr2, Max); 62 at = getSystemTime(); 63 cout << "\ntime of Maopaosort:" << at - pt << "ms\n"; 64 return 0; 65 }
当数组越大时俩者时间消耗越明显:
Max=10时:
Max=100时:
Max=1000时:
Max=10000时
#include<iostream>#include<time.h>#include<stdlib.h>#include<sys/timeb.h>using namespace std;const int Max = 5000;
void swap(int& a, int& b) {int temp = a;a = b;b = temp;}long getSystemTime() {struct timeb tb;ftime(&tb);return tb.time * 1000 + tb.millitm;}void Print(const int* arr, int length) {for (int i = 0; i < length; i++) {cout << arr[i] << " ";}
}
void SelectSort(int *arr,int length) {for (int i = 0; i < length - 1; i++) {int min = i;for (int j = i + 1; j < length; j++) {if (arr[j] < arr[min]) min = j;//还是要俩俩比较,但是if成立后不立马发生交换二十记录较小数的下标}//内循环结束后会得到最小值的下标与当前arr[i]进行交换,所以减少了交换次数。。swap(arr[i], arr[min]);}}bool flag = false;void Maopaosort(int* arr, int length) {for (int i = 0; i < length - 1 && flag == false; i++) {flag = true; //假设上一次排序后,已经完全有序了,如果这一轮排序没有发生内循环交换,表明真的有序则退出循环。for (int j = 0; j < length - i - 1; j++) {if (arr[j] > arr[j + 1]) {swap(arr[j], arr[j + 1]);flag = false; //表示在这一趟中发生了交换,则表示上一趟排序结果依然没有完全有序,但是也不能保证这一趟就完全有序了}
}}}int main() {int arr[Max];int arr2[Max];srand((unsigned)time(NULL));for (int i = 0; i < Max; i++) {arr[i] = rand() % Max;arr2[i] = arr[i];}long pt = getSystemTime();SelectSort(arr, Max);long at = getSystemTime();cout << "\ntime of Selectsort:" << at - pt << "ms\n";pt = getSystemTime();Maopaosort(arr2, Max);at = getSystemTime();cout << "\ntime of Maopaosort:" << at - pt << "ms\n";return 0;}