7 solutions

  • 3
    @ 2023-12-6 16:37:44

    搞了好久,原来有一个大坑

    #include<iostream>
    using namespace std;
    int main()
    {
     int n;
     cin>>n;//输入
     int a[n+1];
     for(int i=0;i<n;i++)//输入
     {
      cin>>a[i];
     }
     for(int i=1;i<n;i++)//要从一开始!!!
     {
      for(int j=i-1;j>-1;j--)//用于与前面的比较
      {
       if(a[j+1]>a[j])//看看有没有大于前一个
       {
        break;//已经确定位置了
       }
       else
       {
        swap(a[j+1],a[j]);//交换
       }
      }
      for(int i=0;i<n;i++)//输出
      {
       cout<<a[i]<<" ";
      }
      cout<<endl;
     }
     for(int i=0;i<n;i++)//大坑
     {
      cout<<a[i]<<" ";
     }
     cout<<endl;
     return 0;//完结散花
    }
    
  • 2
    @ 2023-11-15 16:40:35
    #include <bits/stdc++.h>
    using namespace std;
    int a[105];
    void swap(int& b,int& c){
    	int t;
    	t = b;
    	b = c;
    	c = t;
    }
    void Print(int n){
    	for(int i = 1;i <= n;i++){
    		printf("%d ",a[i]);
    	}
    	printf("\n");
    }
    int main(int argc, char **argv){
    	int n;
    	cin >> n;
    	for (int i = 1;i <= n;i++){
    		cin >> a[i];
    	}
    	for (int i = 2;i <= n;i++){
    		int j = i;
    		while (a[j] < a[j - 1]){
    			swap(a[j],a[j - 1]);
    			j--;
    		}
    		Print(n);
    	}
    	Print(n);
    	return 0;
    }
    
    • 1
      @ 2025-3-9 19:09:11

      快排

      #include<bits/stdc++.h>
      using namespace std;
      int n,ppp[100010];
      void quick_sort(int l,int r){
      	if(l>=r)return;
      	int pivot=l,ptr=l;
      	for(int i=l+1;i<=r;i++)if(ppp[i]<=ppp[l])swap(ppp[i],ppp[++ptr]);
      	swap(ppp[l],ppp[ptr]);
      	quick_sort(l,ptr-1);
      	quick_sort(ptr+1,r);
      }
      int main(){
      	cin>>n;
      	for(int i=0;i<n;i++)cin>>ppp[i];
      	quick_sort(0,n-1);
      	for(int i=0;i<n;i++)printf("%d ",ppp[i]);
      }
      
      • 1
        @ 2023-11-25 17:01:18
        #include<bits/stdc++.h>
        using namespace std;
        int n;
        int a[105];
        int main(){
        	cin>>n;
        	for(int i=0;i<n;i++) cin>>a[i];
        	for(int i=1;i<n;i++){//注意i是1 
        		int q=i;//从q往后搜 
        		while(a[q]<=a[q-1]){
        			swap(a[q],a[q-1]);//如果不符合就往下 
        			q--;
        		}
        		for(int j=0;j<n;j++) cout<<a[j]<<' ';
        		cout<<endl;
        	}
        	for(int i=0;i<n;i++) cout<<a[i]<<" ";//再输出一遍 
        	return 0;//不必多言 
        }
        
        • 0
          @ 2024-11-19 18:38:20
          #include<bits/stdc++.h>
          using namespace std;
          int n;
          int a[105];
          int main()
          {
          	cin >> n;
          	a[0] = 0; 
          	for(int i = 1;i <= n;i++)
          	{
          		cin >> a[i];
          	} 
          	int t;
          	for(int i = 2;i <= n;i++)//谁说i必须是1
          	{
          		int q = i;//从q往后搜
          //		 事例 
          //		 0    3    2    1    0
          //		 0    2    3    1    0
          //		 0    1    2    3    0
          //		 0    0    1    2    3
          //     a[0] a[1] a[2] a[3] a[4]
          		while(a[q] <= a[q-1])
          		{
          			t = a[q];
          			a[q] = a[q-1];
          			a[q-1] = t;
          //			swap(a[q], a[q-1]);//交换 
          			q--;
          		}
          		for(int j = 1;j <= n;j++)
          		{
          			cout<<a[j]<<" ";
          		}
          		cout << endl;
          	}
          	for(int i = 1;i <= n;i++)
          	{
          		cout<<a[i]<<" ";//输出最后的结果
          	}
          	return 0;//不必多言
          }
          
          • 0
            @ 2023-12-23 20:47:20
            #include<bits/stdc++.h>
            using namespace std;
            int n,a[105];
            int main(){
            	cin>>n;
            	for(int i=0;i<n;i++) cin>>a[i];
            	for(int i=1;i<n;i++){
            		int q=i;
            		while(a[q]<=a[q-1]){
            			swap(a[q],a[q-1]);
            			q--;
            		}
            		for(int j=0;j<n;j++) cout<<a[j]<<" ";
            		cout<<endl;
            	}
            	for(int i=0;i<n;i++) cout<<a[i]<<" ";
            	return 0;
            }
            
            • 0
              @ 2023-11-22 16:47:47
              #include<bits/stdc++.h>
              using namespace std;
              int arr[110];
              int main(){
                  int n;
                  cin>>n;
                  for(int i=0;i<n;i++) cin>>arr[i];
                  for(int i=2;i<=n+1;i++){
                      if(i<=n){
                          for(int j=i-1;j>=0;j--){
                              if(arr[j]<arr[j-1]){
                                  int tamp=arr[j];
                                  arr[j]=arr[j-1];
                                  arr[j-1]=tamp;
                              }
                              else break;
                          }
                      }
                      for(int x=0;x<n;x++) cout<<arr[x]<<' ';
                      cout<<endl;
                  }
              }
              
              • 1

              Information

              ID
              940
              Time
              1000ms
              Memory
              256MiB
              Difficulty
              5
              Tags
              (None)
              # Submissions
              130
              Accepted
              54
              Uploaded By