今天是去实现了一下排序算法
先是交换排序
最简单的冒泡排序:
#include <stdio.h>
#include <vector>
#include <iostream>
using namespace std;
int main(){
int n;
vector<int> result;
while(cin>>n){
result.push_back(n);
}
int a=0;
for(int i=0;i<result.size();i++){
int flag=0;
for(int j=result.size()-1;j>i;j--){
if(resultj<resultj-1){
int tmp=resultj;
resultj=resultj-1;
resultj-1=tmp;
flag=1;
}
}
if(flag==0)
break;
}
for(int i=0;i<result.size();i++){
cout<<resulti<<" ";
}
}
快速排序:
#include <stdio.h>
#include <vector>
#include <iostream>
using namespace std;
int pattern(vector<int> &a,int low,int high){
int tmp=alow;
while(low<high){
for(;low<high&&ahigh>tmp;high--){
}
swap(alow,ahigh);
for(;low<high&&alow<=tmp;low++){
}
swap(ahigh,alow);
}
alow=tmp;
return low;
}
void quicksort(vector<int> &a,int low,int high){
if(low>=high) return;
int tmp=pattern(a,low,high);
quicksort(a,low,tmp-1);
quicksort(a,tmp+1,high);
}
int main(){
int n;
vector<int> result;
while(cin>>n){
result.push_back(n);
}
quicksort(result,0,result.size()-1);
for(int i=0;i<result.size();i++){
cout<<resulti<<" ";
}
}