排序算法合集(更新中)
本帖最后由 香蕉那个不拿拿 于 2023-7-21 14:58 编辑已实现插入排序和选择排序,冒泡,计数,快速排序
#include<iostream>
using namespace std;
int carr;
int kuaisu (int arr,int n){
int max = 0;
for (int i = 1;i<=n;i++){
if (arr > max){
max = arr;
}
}
int jizhun =arr;
int count1=0,count2=0,arr1,arr2;
if (n == 1 ||n==0){
if (n == 1){
return arr;
}
}
for (int i = 1;i <= n;i++){
if (arr<=jizhun){
count1++;
arr1 = arr;
}
else if (arr >jizhun){
count2++;
arr2 = arr;
}
}
if (count1 != 0){
kuaisu(arr1,count1);
}
if (count2 !=0){
kuaisu(arr2,count2);
}
for (int i = 1;i<=n;i++){
if (i <=count1){
arr = arr1;
}
else{
arr = arr2;
}
}
}
int main(){
int arr,n,temp;
cin>>n;
for (int i = 1;i <=n;i++){
cin>>arr;
}
string nb;
cout<<"请选择排序方法"<<endl;
cin>>nb;
if (nb == "插入排序"){
for (int i = 2;i <= n;i++){
for (int j = i-1;j>=0;j--){
if (arr > arr){
temp = arr;
for (int k = i - 1;k >j;k--){
arr = arr;
}
arr = temp;
break;
}
}
}
}
else if (nb == "选择排序"){
long long less = 1000000000000000;
int less_j;
for (int i = 0;i<n;i++){
for (int j = i + 1;j <=n;j++){
if (arr < less){
less = arr;
less_j = j;
}
}
swap(arr,arr);
less = 1000000000000000;
}
}
else if (nb == "冒泡排序"){
for (int i = n;i>= 1;i--){
for (int j = 1; j < i; j++){
if (arr > arr){
swap (arr,arr);
}
}
}
}
else if (nb == "计数排序"){
int max,less,count;
count = 1;
cout<<"请输入最大最小值"<<endl;
cin>>max>>less;
for (int i = 1;i <= n ;i++){
carr - less + 1] = arr;
carr - less + 1] += 1;
}
for (int i = 1;i <= max; i++){
for (int j = carr;j >= 1;j--){
arr = carr;
count++;
}
}
}
else if (nb == "快速排序"){
kuaisu(arr,n);
}
for (int i = 1; i<=n;i++){
cout<<arr<<" ";
}
}
推荐网站https://visualgo.net/en
一个动画演示各种算法的平台 支持! liuhongrun2022 发表于 2023-7-20 17:38
支持!
每天进步一点点 代码破120!!!
页:
[1]