sort.hios
//#pragma once #ifndef _SORT #define _SORT #include <algorithm> //函數返回值 static int OK = 1; static int ERROR = 0; //插入排序 template<typename T> int insertSort(T *tData, int iLength); //冒泡排序 template<typename T> int bubbleSort(T *tData, int iLength); //查詢排序 template<typename T> int searchSort(T *tData, int iLength); //歸併排序 template<typename T> int margeSort(T *tData, int iBegin, int iEnd); template<typename T> int marge(T *tData, int iLBegin, int iLEnd, int iRBegin, int iREnd); //快速排序 template<typename T> inline int quikSort(T * tData, int iBegin, int iEnd) //交換數據位置 template<typename T> void swap(T &tLeft, T &tRight); #endif // !_SORT //插入排序 template<typename T> inline int insertSort(T * tData, int iLength) { if (iLength < 2) return ERROR; for (int j = 1; j < iLength; ++j) { T tKey = tData[j]; int i = j - 1; while (i >= 0 && tData[i] > tKey) { tData[i + 1] = tData[i]; --i; } tData[i + 1] = tKey; } return OK; } //冒泡排序 template<typename T> inline int bubbleSort(T * tData, int iLength) { if (iLength < 0) return ERROR; for (int i = 0; i < iLength; ++i) { for (int j = 0; j < i; ++j) { if (tData[j] > tData[i]) swap(tData[j], tData[i]); } } return OK; } //查詢排序 template<typename T> inline int searchSort(T * tData, int iLength) { if (iLength < 0) return ERROR; for (int i = 0; i < iLength; ++i) { //最小值下標 int index = i; //最小值 T tMin = tData[index]; int j = i; while (j < iLength) { if (tData[j] < tMin) { index = j; tMin = tData[j]; } ++j; } swap(tData[i], tData[index]); } return OK; } //歸併排序 template<typename T> inline int margeSort(T * tData, int iBegin, int iEnd) { if (iBegin < 0 || iEnd < 0) return ERROR; if (iBegin < iEnd) { int mid = (iBegin + iEnd) / 2; margeSort(tData, iBegin, mid); margeSort(tData, mid + 1, iEnd); marge(tData, iBegin, mid, mid + 1, iEnd); } return OK; } template<typename T> inline int marge(T * tData, int iLBegin, int iLEnd, int iRBegin, int iREnd) { int lBeg = iLBegin, rBeg = iRBegin, lNums = iLEnd - iLBegin + 1, rNums = iREnd - iRBegin + 1; T *tmp = new T[lNums + rNums]; //建立存放數據的空間 int index = 0; //排序 while (lBeg <= iLEnd && rBeg <= iREnd) if (tData[lBeg] < tData[rBeg]) tmp[index++] = tData[lBeg++]; else tmp[index++] = tData[rBeg++]; while (lBeg <= iLEnd) tmp[index++] = tData[lBeg++]; while (rBeg <= iREnd) tmp[index++] = tData[rBeg++]; //將已排序數據複製給tData memcpy(tData + iLBegin, tmp, sizeof(T) * (lNums + rNums)); //釋放申請的空間 delete[] tmp; return OK; } template<typename T> void swap(T &tLeft, T &tRight) { using std::swap; swap(tLeft, tRight); } //使數據部分有序 template<typename T> inline int quik(T *tData, int iBegin, int iEnd) { T tContent = tData[iEnd]; int index = iBegin; for (int i = iBegin; i < iEnd; ++i) { if (tData[i] <= tContent) swap(tData[index++], tData[i]); } swap(tData[index], tData[iEnd]); return index; } //快速排序 template<typename T> inline int quikSort(T * tData, int iBegin, int iEnd) { if (iBegin >= iEnd) return ERROR; int index = quik(tData, iBegin, iEnd); quikSort(tData, iBegin, index - 1); quikSort(tData, index + 1, iEnd); return OK; }
測試代碼:main.cpp函數
#include <iostream> #include "Sort.h" #include <cstring> using std::cout; using std::endl; static int iDefData[] = { 9, 7, 15, 19, 6, 7, 14, 21, 8, 17 }; int main() { //測試交換函數 int iLeft = 15; int iRight = 10; cout << "before swap : iLeft " << iLeft << " iRight " << iRight << endl; swap(iLeft, iRight); cout << "after swap : iLeft " << iLeft << " iRight " << iRight << endl; //待排序數據長度 const int iDataLen = sizeof(iDefData) / sizeof(int); //建立待排序數據 int iSortingData[iDataLen] = {0}; //測試插入排序 memcpy(iSortingData, iDefData, sizeof(iDefData)); int iRet = insertSort(iSortingData, iDataLen); if (iRet == OK) { cout << "after insert sorting :" << endl; for (int i = 0; i < iDataLen; ++i) cout << "data[" << i << "] = " << iSortingData[i] << endl; cout << endl; } //測試冒泡排序 memcpy(iSortingData, iDefData, sizeof(iDefData)); iRet = bubbleSort(iSortingData, iDataLen); if (iRet == OK) { cout << "after bubble sorting :" << endl; for (int i = 0; i < iDataLen; ++i) cout << "data[" << i << "] = " << iSortingData[i] << endl; cout << endl; } //測試查詢排序 memcpy(iSortingData, iDefData, sizeof(iDefData)); iRet = searchSort(iSortingData, iDataLen); if (iRet == OK) { cout << "after search sorting :" << endl; for (int i = 0; i < iDataLen; ++i) cout << "data[" << i << "] = " << iSortingData[i] << endl; cout << endl; } //測試歸併排序 memcpy(iSortingData, iDefData, sizeof(iDefData)); iRet = margeSort(iSortingData, 0, iDataLen - 1); if (iRet == OK) { cout << "after marge sorting :" << endl; for (int i = 0; i < iDataLen; ++i) cout << "data[" << i << "] = " << iSortingData[i] << endl; cout << endl; } //測試快速排序 memcpy(iSortingData, iDefData, sizeof(iDefData)); int iRet = quikSort(iSortingData, 0, iDataLen - 1); if (iRet == OK) { cout << "after quik sorting :" << endl; for (int i = 0; i < iDataLen; ++i) cout << "data[" << i << "] = " << iSortingData[i] << endl; cout << endl; } system("pause"); return 0; }
測試結果:測試
before swap : iLeft 15 iRight 10
after swap : iLeft 10 iRight 15
after insert sorting :
data[0] = 6
data[1] = 7
data[2] = 7
data[3] = 8
data[4] = 9
data[5] = 14
data[6] = 15
data[7] = 17
data[8] = 19
data[9] = 21
after bubble sorting :
data[0] = 6
data[1] = 7
data[2] = 7
data[3] = 8
data[4] = 9
data[5] = 14
data[6] = 15
data[7] = 17
data[8] = 19
data[9] = 21
after search sorting :
data[0] = 6
data[1] = 7
data[2] = 7
data[3] = 8
data[4] = 9
data[5] = 14
data[6] = 15
data[7] = 17
data[8] = 19
data[9] = 21
after marge sorting :
data[0] = 6
data[1] = 7
data[2] = 7
data[3] = 8
data[4] = 9
data[5] = 14
data[6] = 15
data[7] = 17
data[8] = 19
data[9] = 21
ui
after quik sorting :
data[0] = 6
data[1] = 7
data[2] = 7
data[3] = 8
data[4] = 9
data[5] = 14
data[6] = 15
data[7] = 17
data[8] = 19
data[9] = 21
code