今天把几个无聊的算法上传一下。都是N年前写过的小玩意,没什么很强的复杂度。实用就好。
template
class Sort
{
public:
void Bubblesort(T* arr, int len);//冒泡排序(改进)
void BucketSort(T* arr,int len);//桶排序
void selectSort(T* arr, int len);//选择排序
void Insertsort(T* arr, int len);//插入排序
void Shellsort(T* arr, int len);//希尔排序
void QuickSort(T* arr, int leftptr, int rightptr); //快速排序
void RecursionSort(T arr[], int left, int right);//归并排序 递归
int maxProfit(vector& prices);卖股票的最佳时机 III算法
int trap(vector& height);//接雨水
bitset<9> getPossibleStatus(int x, int y)
{
return ~(rows[x] | cols[y] | cells[x / 3][y / 3]);
}
vector getNext(vector>& board)
{
vector ret;
int minCnt = 10;
for (int i = 0; i < board.size(); i++)
{
for (int j = 0; j < board[i].size(); j++)
{
if (board[i][j] != '.') continue;
auto cur = getPossibleStatus(i, j);
if (cur.count() >= minCnt) continue;
ret = { i, j };
minCnt = cur.count();
}
}
return ret;
}
void fillNum(int x, int y, int n, bool fillFlag)
{
rows[x][n] = (fillFlag) ? 1 : 0;
cols[y][n] = (fillFlag) ? 1 : 0;
cells[x / 3][y / 3][n] = (fillFlag) ? 1 : 0;
}
bool dfs(vector>& board, int cnt)
{
if (cnt == 0) return true;
auto next = getNext(board);
auto bits = getPossibleStatus(next[0], next[1]);
for (int n = 0; n < bits.size(); n++)
{
if (!bits.test(n)) continue;
fillNum(next[0], next[1], n, true);
board[next[0]][next[1]] = n + '1';
if (dfs(board, cnt - 1)) return true;
board[next[0]][next[1]] = '.';
fillNum(next[0], next[1], n, false);
}
return false;
}
void solveSudoku(vector>& board)
{
rows = vector>(9, bitset<9>());
cols = vector>(9, bitset<9>());
cells = vector>>(3, vector>(3, bitset<9>()));
int cnt = 0;
for (int i = 0; i < board.size(); i++)
{
for (int j = 0; j < board[i].size(); j++)
{
cnt += (board[i][j] == '.');
if (board[i][j] == '.') continue;
int n = board[i][j] - '1';
rows[i] |= (1 << n);
cols[j] |= (1 << n);
cells[i / 3][j / 3] |= (1 << n);
}
}
dfs(board, cnt);
}
private:
vector> rows;
vector> cols;
vector>> cells;
private:
void MergeSort(T arr[], int start, int mid, int end); //归并排序 私有方法实现
};
template
void Sort::Bubblesort(T *arr, int len) //冒泡排序(改进)
{
// cout << "hahah\n";
bool istrue = true;
// for (int i = 0; i < len && istrue; i++)
// {
// istrue = false;
// for (int j = 0; j < len - 1 - i; j++)
// {
// if (arr[j] > arr[j + 1])
// {
// int temp = 0;
// temp = arr[j];
// arr[j] = arr[j + 1];
// arr[j + 1] = temp;
// istrue = true;
// }
// for (int i = 0; i < len; i++)
// {
// cout << arr[i] << " ";
// }
//
// cout << "j:" << j << "\n"; //内循环次数
// }
// }
for (int i = 0; i < len && istrue; i++)
{
istrue = false;
for (int j = len - 1; j >= i; j--)
{
if (arr[j] > arr[j + 1])
{
int temp = 0;
temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
istrue = true;
}
}
}
}
template
void Sort::BucketSort(T *arr,int len)//根据最大位来确定容器大小 巨浪费空间算法 桶排序
{
//int* ptr;
// ptr =(int *)malloc(sizeof(int)*128);
// if(ptr==nullptr)
// {
// return 0;
// }
// memset(ptr,0,sizeof(int)*128);
// *(ptr)=1;
// free(ptr);
// ptr=nullptr;;
T* ret = new T[len]();
if (ret == nullptr)
{
return ;
}
int t;
for (int i = 0; i < len; i++)
{
ret[arr[i]] = ret[arr[i]] + 1;
}
for (int k = 0; k < len; k++)
{
cout< 0)
{
//cout< 0)
{
cout << k << endl;
t--;
}
}
}
delete []ret;
ret = nullptr;
}
template
void Sort::selectSort(T* arr, int len)//选择排序
{
int maxPos, temp;
for (int i = 0; i < len - 1; i++)
{
maxPos = i;
for (int j = i + 1; j < len; j++)
{
cout << "打印J:" << j << " \n";
if (arr[j] > arr[maxPos])
{
maxPos = j;
}
}
temp = arr[i];
arr[i] = arr[maxPos];
arr[maxPos] = temp;
}
/* cout << "打印排序过程:";
for (int ik = 0;ik < len; ik++)
{
cout << a[ik] << " ";
}
cout << endl;*/
}
template
void Sort::Insertsort(T* arr, int len)//插入排序
{
for (int i = 0; i < len - 1; i++)
{
for (int j = i; j >= 0; j--)
{
if (arr[j] > arr[j + 1])
{
int temp = 0;
temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
cout << j << endl;
}
}
}
template
void Sort::Shellsort(T* arr, int len)//希尔排序
{
//for (int gap = len/2; gap > 0; gap = gap / 2) { // 1/2间隔序列 通用排序方法
// for (int i = gap;i < len;i++)
// {
// for (int j = i;j > gap - 1;j -= gap)
// {
// if (arr[j] > arr[j - gap])
// {
// int temp = 0;
// temp = arr[j];
// arr[j] = arr[j - gap];
// arr[j - gap] = temp;
// }
// }
// }
//}
//Knuth序列,唐纳德。 牛斯序列 排序方法
//h=1;
//h=3*h+1;
int h = 1;
while (h <= len / 3)
{
h = h * 3 + 1;
}
for (int gap = h; gap > 0; gap = (gap - 1) / 3) // /h=3*h+1间隔序列
{
for (int i = gap; i < len; i++)
{
for (int j = i; j > gap - 1; j -= gap)
{
if (arr[j] > arr[j - gap])
{
int temp = 0;
temp = arr[j];
arr[j] = arr[j - gap];
arr[j - gap] = temp;
}
}
}
}
}
template
void Sort::QuickSort(T* arr, int leftptr, int rightptr) //快速排序
{
if (leftptr >= rightptr) return;
int pivot = arr[rightptr];
int left = leftptr;
int right = rightptr - 1;
while (left <= right)
{
while (left <= right && arr[left] <= pivot)
{
left++;
if (left == right) break;
}
while (left <= right && arr[right] > pivot)
{
right--;
if (right == left) break;
}
if (left < right)
{
int temp = arr[left];
arr[left] = arr[right];
arr[right] = temp;
}
}
int temp = arr[left];
arr[left] = arr[rightptr];
arr[rightptr] = temp;
QuickSort(arr, leftptr, right);
QuickSort(arr, right + 1, rightptr);
}
template
void Sort::MergeSort(T arr[], int start, int mid, int end) //merge sort 归并排序
{
//思路1.先把整个数组先传过来,然后一分为二。
int* temp = new int[end - start + 1];
int i = start;
int j = mid + 1;
int k = 0;
while (i <= mid && j <= end)
{
temp[k++] = arr[i] <= arr[j] ? arr[i++] : arr[j++];
}
while (i <= mid) temp[k++] = arr[i++];
while (j <= end) temp[k++] = arr[j++];
for (i = 0; i < k; i++)
arr[start + i] = temp[i];
delete[]temp; //清理内存回收
temp = nullptr;
}
template
void Sort::RecursionSort(T arr[], int left, int right)
{
if (left == right) return;
if (right < 0)cout << "Error:arr argments error\n";
//数组分开
int mid = left + (right - left) / 2;
//左分开
RecursionSort(arr, left, mid);
//右分开
RecursionSort(arr, mid + 1, right);
//归并
MergeSort(arr, left, mid, right);
}
//卖股票的最佳时机 III算法
template
int Sort::maxProfit(vector& prices) {
int n = prices.size();
int buy1 = -prices[0], sell1 = 0;
int buy2 = -prices[0], sell2 = 0;
for (int i = 1; i < n; ++i) {
buy1 = max(buy1, -prices[i]);
sell1 = max(sell1, buy1 + prices[i]);
buy2 = max(buy2, sell1 - prices[i]);
sell2 = max(sell2, buy2 + prices[i]);
}
return sell2;
}
template
int Sort::trap(vector& height) //接雨水
{
int n = height.size();
if (n == 0) {
return 0;
}
vector leftMax(n);
leftMax[0] = height[0];
for (int i = 1; i < n; ++i) {
leftMax[i] = max(leftMax[i - 1], height[i]);
}
vector rightMax(n);
rightMax[n - 1] = height[n - 1];
for (int i = n - 2; i >= 0; --i) {
rightMax[i] = max(rightMax[i + 1], height[i]);
}
int ans = 0;
for (int i = 0; i < n; ++i) {
ans += min(leftMax[i], rightMax[i]) - height[i];
}
return ans;
}