# 数据结构——排序（C++代码的具体实现无分析）

2021-01-14 04:32:33  阅读：15  来源： 互联网

# 排序

## 冒泡排序

``````template <typename T>
void BubbleSort(vector<T>& list)
{
const int n = list.size();
for (int i = 0; i < n - 1; i++)
{
for (int j = n - 1; j > i; j--)
if (list[j] < list[j - 1])
swap(list[j], list[j - 1]);
}
}

``````

## 插入排序

### 1. 直接插入排序

``````template<typename T>
void InsertSort(vector<T>& list,int sizeOfSorted=1)
{
const int n = list.size();
for (int i = sizeOfSorted; i <= n-1; i++)
{
T temp = list[i];
int j = i - 1;
for ( j = i-1; j >= 0 && temp < list[j]; j--)
{
list[j+1] =list[j];
}
list[j+1] = temp;
}
}
``````

### 1.折半插入排序

• while 循环的条件包括 =, 和`high = mid-1`以及`low = mid+1`可以保证当循环结束时, 要插入的位置刚好是当前 low 的前一位.
• 随着不断插入, low 和 high 每次循环时都要重新赋值.
``````template<typename T>
void BinaryInsertSort(vector<T>& list,const int sizeOfSorted=1)
{
const int n = list.size();
int low = 0;
int high = sizeOfSorted - 1;
int mid = 0;
for (int i = sizeOfSorted; i < n; i++)
{
T temp = list[i];
high = i - 1; low = 0;
while (low<=high)
{
mid = (low + high) / 2;
if (list[mid] > list[i])
high = mid-1;
else
low = mid+1;

}
for (int j = i; j > low; j--)
list[j] = list[j - 1];
list[low] = temp;
}
}
``````

### 3. 希尔排序法

``````template<typename T>
void ShellInsertSort(vector<T>& list)
{
int gap = 0;
const int n = list.size();
for (gap = list.size() / 3; gap > 0; gap = gap / 3 + 1)
{
//各个子列轮流做直接插入, 将每个子列第零个元素设为基准
for (int i = gap; i < n; i++)
{
T temp = list[i];
int j = 0;
for ( j = i - gap;  j >= 0 && temp < list[j] ; j -= gap)
{
list[j + gap] = list[j];
}
list[j + gap] = temp;
}
if (gap == 1) break;
}
}
``````

## 快速排序法

### 快速排序

``````//双指针
template <typename T>
int FindPos(vector<T>& list,const int left,const int right)
{
T base = list[left];
int smallerPos = left;
int greaterPos = right;
while ( smallerPos< greaterPos) {
while (list[greaterPos] >= base && smallerPos < greaterPos)
{
greaterPos--;
}
list[smallerPos] = list[greaterPos];
while (list[smallerPos] <= base && smallerPos < greaterPos)
{
smallerPos++;
}
list[greaterPos] = list[smallerPos];
}
list[smallerPos] = base;
return smallerPos;
}
//普通
template <typename T>
int FindPos(vector<T>& list, int left, int right)
{
T base = list[left];
int smallerPos =left;
for(int i=left+1;i<=right;i++)
{
if(list[i]<base)
{
smallerPos++;
if(i!=smallerPos)
swap(list[smallerPos],list[i]);
}
}
swap(list[left],list[smallerPos]);
return smallerPos;
}

template <typename T>
void QuickSort(vector<T>& list,int left,int right)
{
if (left < right) {
int pos = FindPos(list, left, right);
QuickSort(list, left,	pos - 1);
QuickSort(list, pos + 1, right);
}
}
``````

### 2.三路划分快速排序

``````template <typename T>
void QuickSort(vector<T>& list, int left, int right)
{
if (left >= right) return;
T base = list[left];
int lessPos = left;
int greaterPos = right + 1;
for (int i = left + 1; i < greaterPos; i++)
{
if (list[i] < base)
swap(list[++lessPos], list[i]);
else if (list[i] > base)
{
swap(list[i], list[--greaterPos]);
i--;
}
}
swap(list[left], list[lessPos]);
QuickSort(list, left, lessPos - 1);
QuickSort(list, greaterPos, right);
}
``````

## 选择排序

### 1. 直接选择

``````template<typename T>
void selectSort(vector<T>& list)
{
for (int i = 0; i < list.size(); i++)
{
int indexOfmin = i;
int j;
for (j = i + 1; j < list.size(); j++)
{
if (list[j] < list[indexOfmin]) indexOfmin = j;
}
swap(list[i], list[indexOfmin]);
}
}
``````

### 2.堆排序

``````空太小放不下
``````

## 基数排序

``````void radixSort(vector<int>& array)
{
vector<vector<int>> bucket(10);
int max=array[0];
for (int i = 1; i < array.size(); i++)
{
if (max < array[i]) max = array[i];
}
for (int round = 1; round <=max; round *= 10)
{
//进桶

for (int i = 0; i < array.size(); i++)
{
bucket[((array[i]) / round) % 10].push_back(array[i]);
}
int count = 0;
int i = 0;
while (count != array.size())
{
while (!bucket[i].empty())
{
array[count++] = bucket[i].front();
bucket[i].erase(bucket[i].begin());
}
i++;
}
}
}
``````