冒泡
#include<iostream>
#include<vector>
using namespace std;
void bubbleSort(vector<int>& nums)
{
bool flag=false;
for(int i=0;i<nums.size();i++)
{
flag=false;
for(int j=0;j<nums.size()-i-1;j++)
{
if(nums[j]>nums[j+1])
{
swap(nums[j],nums[j+1]);
flag=true;
}
}
if(!flag) break;
}
}
int main()
{
vector<int> nums={7,4,5,3,9,1};
bubbleSort(nums);
for(auto& num:nums)
{
cout<<num<<" ";
}
return 0;
}
插排
#include<iostream>
#include<vector>
using namespace std;
void insertSort(vector<int>& nums)
{
for(int i=1;i<nums.size();i++)
{
int insertNode=nums[i];
int j=i-1;
while(j>=0 && insertNode<nums[j])
{
nums[j+1]=nums[j];
j--;
}
nums[j+1]=insertNode;
}
}
int main()
{
vector<int> nums={7,4,5,3,9,1};
insertSort(nums);
for(auto& num:nums)
{
cout<<num<<" ";
}
return 0;
}
选择排序
#include<iostream>
#include<vector>
using namespace std;
void selectSort(vector<int>& nums)
{
for(int i=0;i<nums.size();i++)
{
int minIndex=i;
for(int j=i+1;j<nums.size();j++)
{
if(nums[j]<nums[minIndex]) minIndex=j;
}
swap(nums[i],nums[minIndex]);
}
}
int main()
{
vector<int> nums={7,4,5,3,9,1};
selectSort(nums);
for(auto& num:nums)
{
cout<<num<<" ";
}
return 0;
}
快排
#include<iostream>
#include<vector>
using namespace std;
void print(vector<int> &nums)
{
for (auto& num : nums)
{
cout << num << " ";
}
cout << endl;
}
void quickSort(vector<int>& nums, int L, int R)
{
if(L>=R) return;
int left=L,right=R;
int pivot=nums[left];
while(left<right)
{
while(left<right && nums[right]>=pivot) right--;
if(left<right) nums[left++]=nums[right];
while(left<right && nums[left]<=pivot) left++;
if(left<right) nums[right--]=nums[left];
}
nums[left]=pivot;
quickSort(nums,L,right-1);
quickSort(nums,right+1,R);
}
int main()
{
int T,num;
vector<int> nums={8,8,7,9,9,4};
quickSort(nums,0,nums.size()-1);
print(nums);
return 0;
}
归并排序
#include<iostream>
#include<vector>
using namespace std;
void print(vector<int> &nums)
{
for (auto& num : nums)
{
cout << num << " ";
}
cout << endl;
}
void merge(vector<int>& nums, int L, int mid, int R)
{
vector<int> temp(R-L+1);
int i=0;
int p1=L;
int p2=mid+1;
while(p1<=mid && p2<=R)
{
temp[i++]=(nums[p1]<=nums[p2])?nums[p1++]:nums[p2++];
}
while(p1<=mid)
{
temp[i++]=nums[p1++];
}
while(p2<=R)
{
temp[i++]=nums[p2++];
}
for(int j=0;j<temp.size();j++)
{
nums[L+j]=temp[j];
}
}
void mergeSort(vector<int>& nums, int L, int R)
{
if(L>=R) return;
int mid=L+(R-L)/2;
mergeSort(nums,L,mid);
mergeSort(nums,mid+1,R);
merge(nums,L,mid,R);
}
int main()
{
int T,num;
vector<int> nums={8,3,7,5,9,4};
mergeSort(nums,0,nums.size()-1);
print(nums);
return 0;
}
堆排
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
void heapfiy(vector<int>& nums, int n, int i){
if(i >= n){
return;
}
int leftCh = 2 * i + 1;
int rightCh = 2 * i + 2;
int max = i;
if(leftCh < n && nums[leftCh] > nums[max])
{
max = leftCh;
}
if(rightCh < n && nums[rightCh] > nums[max])
{
max = rightCh;
}
if(max != i){
swap(nums[i], nums[max]);
heapfiy(nums, n, max);
}
}
void buildHeap(vector<int>& nums, int n){
int lastNode = n - 1;
int parent = (lastNode - 1) / 2;
for(int i = parent; i >= 0; i--){
heapfiy(nums, n, i);
}
}
void heapSort(vector<int>& nums) {
int n = nums.size();
buildHeap(nums,n);
for(int i = n - 1; i >= 0; i--){
swap(nums[i], nums[0]);
heapfiy(nums, i ,0);
}
}
int main()
{
vector<int> nums={7,4,5,3,9,1};
heapSort(nums);
for(auto& num:nums)
{
cout<<num<<" ";
}
return 0;
}