1.选择排序算法
#include<iostream>
#include<vector>
#include"SelectionSort.h"
#include"Swap_Print.h"
using namespace std;
void SelectionSort::Solution(vector<int> &nums)
{
int min = 0;
for (int i = 0; i < nums.size(); i++)
for (int j = i + 1; j < nums.size(); j++)
{
if (nums[j] < nums[i])
swap(nums[i], nums[j]);
}
print(nums);
}
2.冒泡排序算法
#include<iostream>
#include<vector>
using namespace std;
#include"Swap_Print.h"
void BubbleSort(vector<int> & nums)
{
for(int i=nums.size()-1;i>0;i--)
for (int j = 0; j < i; j++)
{
if (nums[j] > nums[j + 1])
{
swap(nums[j], nums[j+ 1]);
}
}
print(nums);
}
3.插入排序算法
#include<iostream>
#include<vector>
#include"Swap_Print.h"
using namespace std;
void InsertionSort(vector<int>& nums)
{
for (int i = 1; i < nums.size(); i++)
for (int j = i - 1; j >= 0 && nums[j] > nums[j + 1]; j--)
swap(nums[j], nums[j + 1]);
}
4.在一个数组中,两种数出现奇数次,其他数出现偶数次,分别找出这两个数
#include<iostream>
#include<vector>
using namespace std;
//找出两个奇数个数,其余数字均为偶数个
void odd_find(vector<int> nums)
{
int eor = 0;
for (int i = 0; i < nums.size(); i++)
eor ^= nums[i];//eor =a^b
int eor_ab = eor;
int right_one = eor & (~eor + 1);
for(int i=0;i<nums.size();i++)
if ((right_one & nums[i]) == 0)
{
eor ^= nums[i];
}
cout << eor << " " << (eor^eor_ab)<<endl ;
}
5.生成随机数组
#include<iostream>
#include<vector>
#include<ctime>
using namespace std;
vector<int> radomArray(int maxSize,int maxValue) {
srand(time(0));
int random = rand();
double randomDouble = double(random) / RAND_MAX;
int length = (int)((maxSize + 1) * randomDouble);
vector<int> arr;
for (int i = 0; i++; i < length)
arr[i] = (int)((maxValue + 1) * randomDouble * maxValue);
return arr;
}
视频链接参考:1.认识复杂度和简单排序算法_哔哩哔哩_bilibili