#include <iostream>
using namespace std;
#define LEN 9
void swap(int* left, int* right)
{
int temp = *left;
*left = *right;
*right = temp;
}
void BubbleSort(int* arr, int len)
{
if (arr == nullptr || len <= 0)
{
return;
}
for (int i = 0; i < len; i++)
{
for (int j = i + 1; j < len; j++)
{
if (arr[i] > arr[j])
{
swap(&arr[i], &arr[j]);
}
}
}
}
void SelectSort(int* arr, int len)
{
if (arr == nullptr || len <= 0)
return;
for (int i = 0; i < len; i++)
{
int minPos = i;
for (int j = i + 1; j < len; j++)
{
if (arr[minPos] > arr[j])
{
minPos = j;
}
}
if (i != minPos)
{
swap(&arr[minPos], &arr[i]);
}
}
}
void InsertSort(int* arr, int len)
{
if (arr == nullptr || len <= 0)
return;
for (int i = 0; i < len; i++)
{
int j = i;
while (j > 0)
{
if (arr[j - 1] > arr[j])
{
swap(&arr[j - 1],&arr[j]);
}
j--;
}
}
}
void ShellSort(int* arr, int len)
{
if (arr == nullptr||len<=0)
return;
for (int step = len / 2; step > 0; step /= 2)
{
for (int i = step; i < len; i++)
{
int j = i;
while (arr[j - step] > arr[j] && j >= step)
{
swap(&arr[j - step], &arr[j]);
j -= step;
}
}
}
}
void MergeArr(int* arr, int left, int mid,int right)
{
if (arr == nullptr || left >= right)
return;
int curLeft = left;
int curMid = mid;
int len = right - left + 1;
int* tempArr = new int[len];
int* curArr = tempArr;
while (curLeft < mid && curMid <= right)
{
if (arr[curLeft] > arr[curMid])
{
*curArr++ = arr[curMid++];
}
else
{
*curArr++ = arr[curLeft++];
}
}
while (curLeft < mid)
{
*curArr++ = arr[curLeft++];
}
while (curMid <= right)
{
*curArr++ = arr[curMid++];
}
for (int i = left,k=0;i<=right;)
{
arr[i++] = tempArr[k++];
}
curArr = nullptr;
delete[] tempArr;
delete curArr;
}
void MergeSort(int* arr, int left, int right)
{
if (arr == nullptr || left >= right)
return;
int mid = (left + right) >> 1;
MergeSort(arr, left, mid);
MergeSort(arr, mid + 1, right);
MergeArr(arr, left, mid + 1, right);
}
void QuickSort1(int* arr, int left, int right)
{
if (arr == nullptr || left >= right)
return;
int curLeft = left;
int curRight = right;
int temp = arr[curRight];
while (curLeft != curRight)
{
while (curLeft < curRight && arr[curLeft] < temp)
{
curLeft++;
}
if (curLeft < curRight)
{
arr[curRight--] = arr[curLeft];
}
while (curLeft<curRight && arr[curRight] >temp)
{
curRight--;
}
if (curLeft < curRight)
{
arr[curLeft++] = arr[curRight];
}
}
arr[curLeft] = temp;
QuickSort1(arr, left, curLeft - 1);
QuickSort1(arr, curLeft + 1, right);
}
void QuickSort(int* arr, int left, int right)
{
if (arr == nullptr || left >= right)
return;
int curLeft = left;
int curRight = right;
int temp = arr[right];
while (curLeft != curRight)
{
while(arr[curLeft] < temp && curLeft<curRight)
{
curLeft++;
}
if(curLeft<curRight)
{
arr[curRight--] = arr[curLeft];
}
while(arr[curRight] > temp && curLeft<curRight)
{
curRight--;
}
if(curLeft<curRight)
{
arr[curLeft++] = arr[curRight];
}
}
arr[curLeft] = temp;
QuickSort(arr, left, curLeft-1);
QuickSort(arr, curLeft + 1, right);
}
void CountSort(int* arr, int len, int max_value)
{
if (arr == nullptr || len <= 0 || max_value < 0)
return;
int* tempArr = new int[max_value + 1]();
for (int i = 0; i < len; i++)
{
tempArr[arr[i]]++;
}
int index = 0;
for (int i = 0; i < len; i++)
{
while (tempArr[i] > 0)
{
arr[index++] = i;
tempArr[i]--;
}
}
delete[] tempArr;
}
int main()
{
int arr[LEN] = { 8,6,3,1,4,7,2,5,10};
for (int i = 0; i < LEN; i++)
cout << arr[i] << " ";
cout << endl;
//SelectSort(arr, LEN);
//InsertSort(arr, LEN);
//ShellSort(arr, LEN);
//MergeSort(arr, 0, LEN - 1);
//QuickSort(arr, 0, LEN - 1);
CountSort(arr, LEN, 10);
for (int i = 0; i < LEN; i++)
cout << arr[i] << " ";
cout << endl;
return 0;
}
可逐步调试的C++排序算法(快速,计数,归并,希尔,选择,冒泡,插入排序)
最新推荐文章于 2024-09-04 21:39:45 发布