六種內排序比較次數、交換次數比較
阿新 • • 發佈:2018-12-24
- 內容:隨機生成1萬個資料儲存在檔案中,使用六個內排序演算法,分別對這個檔案進行排序,統計排序過程中各演算法的比較次數及資料交換次序。列表輸出。
- 程式碼:
-
/*測試環境:VS2017*/ #include<iostream> #include<fstream> #include<stdlib.h> #include<iomanip> using namespace std; const int N = 10000; int a[N], temp[N]; unsigned int count_Exc, count_Com; inline void Swap(int&a, int&b) { int temp = a; a = b; b = temp; count_Exc++; } inline void CopyData() { for (int i = 0; i < N;i++) temp[N] = a[N]; } inline int Compare(int a,int b) { count_Com++; return a - b; } inline void ReadDate() { count_Exc = 0; count_Com = 0; ifstream datafile("data.txt"); int i=0; if (!datafile.is_open()) cout << "無法開啟檔案data.txt!" << endl; else while (!datafile.eof()&&i<N) datafile >> a[i++]; datafile.close(); } void CreatData() { ofstream datafile("data.txt"); for (int i = 0; i < N; i++) { datafile << rand() << " "; if ((i + 1) % 10 == 0) datafile << endl; } datafile.close(); } void Print() { for (int i = 0; i < N; i++) { cout << a[i] << " "; if ((i + 1) % 10 == 0) cout << endl; } cout << "******************************************************************************" << endl; } void bubbleSort() { for (int i = 0; i<N - 1; i++) for (int j = 0; j < N - 1 - i; j++) { if (Compare(a[j],a[j+1])>0) Swap(a[j], a[j + 1]); } } void selectSort() { int index; for (int i = 0; i < N - 1; i++) { index = i; for (int j = i + 1; j < N; j++) { if (Compare(a[j],a[index])<0) index = j; } Swap(a[i], a[index]); } } void insertSort() { int preindex, current; for (int i = 1; i < N; i++) { preindex = i - 1; current = a[i]; while (preindex > -1 && Compare(a[preindex] , current)>0) { a[preindex + 1] = a[preindex]; count_Exc++;// preindex--; } a[preindex + 1] = current; count_Exc++;// } } void shellSort() { int temp, gap = 1; while (gap < N / 3) gap = gap * 3 + 1; for (gap; gap > 0; gap = floor(gap / 3)) for (int i = gap, j; i < N; i++) { temp = a[i]; for (j = i - gap; j >= 0 && Compare(a[j], temp) > 0; j -= gap) { a[j + gap] = a[j]; count_Exc++; } a[j + gap] = temp; //count_Exc++; } } void mergeSort(int arry[] = a, int b[] = temp, int left = 0, int right = N - 1) { if (left == right) return; int mid = (left + right) / 2; mergeSort(arry, temp, left, mid); mergeSort(arry, temp, mid + 1, right); for (int i = left; i <= right; i++) temp[i] = arry[i]; int i1 = left; int i2 = mid + 1; for (int curr = left; curr <= right; curr++) { if (Compare(i1 , mid + 1)==0) { arry[curr] = temp[i2++]; count_Exc++; } else if (Compare(i2 , right)>0) { arry[curr] = temp[i1++]; count_Exc++; } else if (Compare(temp[i1] , temp[i2])<0) { arry[curr] = temp[i1++]; count_Exc++; } else { arry[curr] = temp[i2++]; count_Exc++; } } } void quickSort(int arry[] = a, int left = 0, int right = N - 1) { if (right <= left) return; int pivotindex = (left + right) / 2; Swap(arry[pivotindex], arry[right]); //劃分 int L = left - 1, R = right; do { while (Compare(arry[++L] , arry[right])<0); while (L < R &&Compare(arry[right] , arry[--R])<0); Swap(arry[L], arry[R]); } while (L < R); Swap(arry[L], arry[right]); quickSort(arry, left, L - 1); quickSort(arry, L + 1, right); } int main() { CreatData(); cout << "排序演算法 " << "比較次數 " << "交換次數" << endl; ReadDate(); bubbleSort(); cout << "氣泡排序 " << setw(8) << count_Com << setw(24) << count_Exc << endl; ReadDate(); selectSort(); cout << "選擇排序 " << setw(8) << count_Com << setw(24) << count_Exc << endl; ReadDate(); insertSort(); cout << "插入排序 " << setw(8) << count_Com << setw(24) << count_Exc << endl; ReadDate(); shellSort(); cout << "希爾排序 " << setw(8) << count_Com << setw(24) << count_Exc << endl; ReadDate(); mergeSort(); cout << "歸併排序 " << setw(8) << count_Com << setw(24) << count_Exc << endl; ReadDate(); quickSort(); cout << "快速排序 " << setw(8) << count_Com << setw(24) << count_Exc << endl; system("pause"); }