标签:
数据定义
typedef int DataType; enum IStatus{IERROR, ITRUE}; template<class T> void exchange(T & f, T & s) { T t = f; f = s; s = t; };
冒泡
void bubble_sort( DataType datas[], int begin, int end ) { DataType tdt; for (int i = end - 1; i > begin; -- i) for (int j = begin; j < i; j ++) if (datas[j] > datas[j + 1]) exchange(datas[j], datas[j + 1]); }
插入
void insert_sort( DataType datas[], int begin, int end ) { for (int i = begin + 1; i < end; ++ i) { DataType tdt = datas[i]; int j = i - 1; for (; j >= begin && datas[j] > datas[i]; -- j) datas[j + 1] = datas[j]; datas[j + 1] = tdt; } }
快排
int partition( DataType datas[], int begin, int end ) { int i(begin - 1); int j(begin); while (j < end - 1) { if (datas[j] <= datas[end - 1]) exchange(datas[++ i], datas[j]); j ++; } exchange(datas[++ i], datas[j]); return i; } void quick_sort( DataType datas[], int begin, int end ) { if (begin < end - 1) { int mid = partition(datas, begin, end); quick_sort(datas, begin, mid); quick_sort(datas, mid + 1, end); } }
标签:
原文地址:http://www.cnblogs.com/datakv/p/5633090.html