马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 香蕉那个不拿拿 于 2023-7-21 14:58 编辑
已实现插入排序和选择排序,冒泡,计数,快速排序#include<iostream>
using namespace std;
int carr[1000000][3];
int kuaisu (int arr[1000],int n){
int max = 0;
for (int i = 1;i<=n;i++){
if (arr[i] > max){
max = arr[i];
}
}
int jizhun =arr[1];
int count1=0,count2=0,arr1[999],arr2[999];
if (n == 1 ||n==0){
if (n == 1){
return arr[1];
}
}
for (int i = 1;i <= n;i++){
if (arr[i]<=jizhun){
count1++;
arr1[count1] = arr[i];
}
else if (arr[i] >jizhun){
count2++;
arr2[count2] = arr[i];
}
}
if (count1 != 0){
kuaisu(arr1,count1);
}
if (count2 !=0){
kuaisu(arr2,count2);
}
for (int i = 1;i<=n;i++){
if (i <=count1){
arr[i] = arr1[i];
}
else{
arr[i] = arr2[i - count1];
}
}
}
int main(){
int arr[1000],n,temp;
cin>>n;
for (int i = 1;i <=n;i++){
cin>>arr[i];
}
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[i] > arr[j]){
temp = arr[i];
for (int k = i - 1;k >j;k--){
arr[k+1] = arr[k];
}
arr[j+1] = 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[j] < less){
less = arr[j];
less_j = j;
}
}
swap(arr[less_j],arr[i+1]);
less = 1000000000000000;
}
}
else if (nb == "冒泡排序"){
for (int i = n;i >= 1;i--){
for (int j = 1; j < i; j++){
if (arr[j] > arr[j + 1]){
swap (arr[j],arr[j+1]);
}
}
}
}
else if (nb == "计数排序"){
int max,less,count;
count = 1;
cout<<"请输入最大最小值"<<endl;
cin>>max>>less;
for (int i = 1;i <= n ;i++){
carr[arr[i] - less + 1][1] = arr[i];
carr[arr[i] - less + 1][2] += 1;
}
for (int i = 1;i <= max; i++){
for (int j = carr[i][2];j >= 1;j--){
arr[count] = carr[i][1];
count++;
}
}
}
else if (nb == "快速排序"){
kuaisu(arr,n);
}
for (int i = 1; i<=n;i++){
cout<<arr[i]<<" ";
}
}
推荐网站https://visualgo.net/en
一个动画演示各种算法的平台 |