排序算法
/************************************直接插入排序***************************************/
void insertSort(vector<int>& vec, int n) {
for (int i = 1; i < n; i++) {
int tmp = vec[i];
for (int j = i - 1; j >= 0; j--) {
if (vec[j] > tmp) {
vec[j + 1] = vec[j];
}
else {
vec[j + 1] = tmp;
break;
}
}
}
}
/************************************希尔排序***************************************/
void shellSort(vector<int>& vec, int n) {
for (int gap = n / 2; gap >= 1; gap = gap / 2) {
for (int i = gap; i < n; i++) {
int tmp = vec[i];
for (int j = i - gap; j >= 0; j = j - gap) {
if (vec[j] > tmp) {
vec[j + gap] = vec[j];
}
else {
vec[j + gap] = tmp;
break;
}
}
}
}
}
/************************************二分插入排序***************************************/
void binaryInsertSort(vector<int>& vec, int n) {
for (int i = 1; i < n; i++) {
int tmp = vec[i];
int low = 0;
int high = i - 1;
while (low <= high) { //注意这里的条件判读是 low <= high
int mid = (low + high) / 2;
if (vec[mid] < tmp) {
low = mid + 1;
}
else {
high = mid - 1;
}
}
for (int j = i - 1; j >= low; j--) {
vec[j + 1] = vec[j];
}
vec[low] = tmp;
}
}
/************************************冒泡排序*******************************************/
void bubbleSort(vector<int>& vec, int n) {
for (int i = n - 1; i >= 0; i--) { //从前向后冒泡,先填最后一个元素
int flag = true;
for (int j = 0; j < i; j++) {
flag = false;
if (vec[j] > vec[j + 1]) {
swap(vec[j], vec[j + 1]);
}
}
if (flag) break;
}
}
/************************************快速排序*******************************************/
int partition(vector<int>& vec, int low, int high) {
swap(vec[low], vec[(low + high) / 2]);
int pivotVal = vec[low];
while (low < high) {
while (low < high && vec[high] >= pivotVal) {
high--;
}
vec[low] = vec[high];
while (low < high && vec[low] <= pivotVal) {
low++;
}
vec[high] = vec[low];
}
vec[low] = pivotVal;
return low;
}
void quickSort(vector<int>& vec, int low, int high) {
if (low < high) {
int pivot = partition(vec, low, high);
quickSort(vec, low, pivot - 1);
quickSort(vec, pivot + 1, high);
}
}
/************************************简单选择排序*****************************************/
void sellectSort(vector<int>& vec, int n) {
for (int i = 0; i < n - 1; i++) { //一共n-1趟
int min = i;
for (int j = i + 1; j < n; j++) {
if (vec[j] < vec[min]) {
min = j;
}
}
if (i != min) swap(vec[i], vec[min]);
}
}
/************************************堆排序*******************************************/
void heapAdjust(vector<int>& vec, int k, int len) {
int val = vec[k];
for (int i = 2 * k + 1; i <= len; i = i * 2) { //循环的终止条件为,准备调整的分支结点没有孩子结点了
if (i < len && vec[i] < vec[i + 1]) {
i++;
}
if (val >= vec[i]) {
break;
}
else {
vec[k] = vec[i]; //将孩子结点中的较大的调整到双亲结点
k = i; //修改k值以便继续向下调整
}
}
vec[k] = val; //将筛选结点的值放入最终位置
}
void buildHeap(vector<int>& vec, int len) {
for (int i = (len - 1) / 2; i >= 0; i--) {
heapAdjust(vec, i, len);
}
}
void heapSort(vector<int>& vec, int len) {
buildHeap(vec, len);
for (int k = len; k > 0; k--) {
swap(vec[0], vec[k]);
heapAdjust(vec, 0, k - 1);
}
}
/************************************归并排序*******************************************/
void merge(vector<int>& vec, int low, int mid, int high) {
vector<int> tmp(vec.begin(), vec.end());
int k = low, i, j; //注意这里的k是从low开始的,而不是从0开始的
for (i = low, j = mid + 1; i <= mid && j <= high;) {
if (tmp[i] <= tmp[j]) {
vec[k++] = tmp[i++];
}
else {
vec[k++] = tmp[j++];
}
}
while (i <= mid) vec[k++] = tmp[i++];
while (j <= high) vec[k++] = tmp[j++];
}
void mergeSort(vector<int>& vec, int low, int high) {
if (low < high) {
int mid = (low + high) / 2;
mergeSort(vec, low, mid);
mergeSort(vec, mid + 1, high);
merge(vec, low, mid, high);
}
}