網頁

2010年5月25日 星期二

排序

利用srand(), rand()產生值為0~9999的int亂數存入陣列a[100],

利用 bubblesort排序a!

44 則留言:

  1. #define N 100
    #include <iostream>
    #include <cstdlib>
    #include <ctime>
    using namespace std;
    void swap(int &x,int &y){int t=x;x=y;y=t;}
    void output(int data[], int count){
     for (int i=0; i<count; i++)
      cout<<data[i]<<'\t';
     cout<<endl;
    }
    void bubbleSort(int data [], int count) {
       for ( int j = count; j >0 ; j-- ) {
          for ( int i = 0; i < j-1; i++ )
            if ( data[i+1] < data[i] )
                swap(data[i],data[i+1]);
       };  
    }
    int main() {
     int data[N];
     srand((unsigned)time(0));
     cout<<"排序前\n";
        for (int c=0;c<N;c++){
            data[c]=rand()%10000;
            cout<<data[c]<<'\t';
            }
     bubbleSort(data,N);
     cout<<"排序完成!!\n";
     output(data,N);
        system("PAUSE");
        return 0;
    }

    回覆刪除
  2. #define N 100
    #include <iostream>
    #include <cstdlib>
    #include <ctime>
    using namespace std;
    void swap(int &x, int &y){int t=x;x=y;y=t;}
    void output (int data[], int count){
         for (int i=0; i<count; i++)
              cout<<data[i]<<'\t';
              cout<<endl;
    }
    void bubbleSort(int data [], int count){
         for ( int j = count; j >0 ; j--){
             for ( int i = 0 ; i < j-1; i++)
                 if (data[i+1] < data[i] )
                    swap(data[i],data[i+1]);
    };
    }
    int main(){
        int data[N];
        srand((unsigned)time(0));
        cout<<"排序前\n";
        for (int c=0;c<N;c++){
            data[c]=rand()%10000;
            cout<<data[c]<<'\t';
            }
        bubbleSort(data,N);
        cout<<"排序完成!!\n";
        output(data,N);
        system("PAUSE");
        return 0;
    }

    回覆刪除
  3. #include <iostream>
    #include <cstdlib>
    #include <ctime>
    using namespace std;
    void output(int data[],int count){
    for(int i=0;i<count;i++)
    cout<<data[i]<<'\t';
    cout<<endl;
    }
    void bubbleSort(int data [], int count)
     {
         int temp;
     for ( int j = count-1; j >0 ; j-- )
      {
     for ( int i = 0; i < j; i++ )
     if ( data[i+1] < data[i] )
         {
         temp=data[i+1];
         data[i+1]=data[i];
         data[i]=temp;
         }
      }
      }
    int main(){
        int data[100];
        srand((unsigned)time(NULL));
        int i=rand();
        for(int x=0;x<100;x++)
        {
        data[x]=rand()%10000;
        }  
    cout<<"排序前\n";
    output(data,100);
    bubbleSort(data, 100);
    cout<<"排序後\n";
    output(data,100);
    system("pause");  
    return 0;
    }

    回覆刪除
  4. #define N 100
    #include <iostream>
    #include <cstdlib>
    #include <ctime>
    using namespace std;
    void swap(int &x, int &y){int t=x;x=y;y=t;}
    void output (int data[], int count){
         for (int i=0; i<count; i++)
              cout<<data[i]<<'\t';
              cout<<endl;
    }
    void bubbleSort(int data [], int count){
         for ( int j = count; j >0 ; j--){
             for ( int i = 0 ; i < j-1; i++)
                 if (data[i+1] < data[i] )
                    swap(data[i],data[i+1]);
    };
    }
    int main(){
        int data[N];
        srand((unsigned)time(0));
        cout<<"排序前\n";
        for (int c=0;c<N;c++){
            data[c]=rand()%10000;
            cout<<data[c]<<'\t';
            }
        bubbleSort(data,N);
        cout<<"排序完成!!\n";
        output(data,N);
        system("PAUSE");
        return 0;
    }

    回覆刪除
  5. #define N 100
    #include <iostream>
    #include <cstdlib>
    #include <ctime>
    using namespace std;
    void swap(int &x, int &y){int t=x;x=y;y=t;}
    void output (int data[], int count){
         for (int i=0; i<count; i++)
              cout<<data[i]<<'\t';
              cout<<endl;
    }
    void bubbleSort(int data [], int count){
         for ( int j = count; j >0 ; j--){
             for ( int i = 0 ; i < j-1; i++)
                 if (data[i+1] < data[i] )
                    swap(data[i],data[i+1]);
    };
    }
    int main(){
        int data[N];
        srand((unsigned)time(0));
        cout<<"排序前\n";
        for (int c=0;c<N;c++){
            data[c]=rand()%10000;
            cout<<data[c]<<'\t';
            }
        bubbleSort(data,N);
        cout<<"排序完成!!\n";
        output(data,N);
        system("PAUSE");
        return 0;
    }

    回覆刪除
  6. #define N 100
    #include <iostream>
    #include <cstdlib>
    #include <ctime>
    using namespace std;
    void swap(int &x,int &y){int t=x;x=y;y=t;}
    void output (int data[],int count)
    {
         for(int i=0;i<count;i++)
            cout<<data[i]<<'\t';
         cout<<endl;
    }
    void bubbleSort(int data[],int count)
    {
         for (int j=count;j>0;j--)
         {
             for(int i=0;i<j-1;i++)
                if (data[i+1]<data[i])
                   swap(data[i],data[i+1]);
         }
    }
    int main()
    {
        int data[N];
        srand((unsigned)time(NULL));
        cout<<"排序前\n";
        for(int c=0;c<N;c++)
        {
                data[c]=rand()%9999;
                cout<<data[c]<<'\t';
        }
        bubbleSort(data,N);
        cout<<"排序完成\n";
        output(data,N);
        system("pause");
        return 0;
    }

    回覆刪除
  7. #define N 100
    #include <iostream>
    #include <cstdlib>
    #include <ctime>
    using namespace std;
    void swap(int&x,int&y){int t=x;x=y;y=t;}
    void output(int data[],int count)
    {
         for(int i=0; i<count;i++)
             cout<<data[i]<<'\t';
         cout<<endl;
    }
    void bubbleSort (int data[],int count)
    {
         for(int j=count;j>0;j--)
         {
           for(int i=0;i<j-1;i++)
               if(data[i+1]<data[i])
                  swap(data[i],data[i+1]);
         };
    }
    int main()
    {
        int data[N];
        srand((unsigned)time(0));
        cout<<"排序前\n";
        for(int c=0;c<N;c++)
        {
            data[c]=rand()%10000;
            cout<<data[c]<<'\t';   
        }
        bubbleSort(data, N);
        cout<<"排序完成!!\n";
        output(data, N);
        system("pause");
        return 0;
    }

    回覆刪除
  8. #define N 100
    #include <iostream>
    #include <cstdlib>
    #include <ctime>
    using namespace std;
    void swap(int &x, int &y){int t=x;x=y;y=t;}
    void output (int data[], int count){
         for (int i=0; i<count; i++)
              cout<<data[i]<<'\t';
              cout<<endl;
    }
    void bubbleSort(int data [], int count){
         for ( int j = count; j >0 ; j--){
             for ( int i = 0 ; i < j-1; i++)
                 if (data[i+1] < data[i] )
                    swap(data[i],data[i+1]);
    };
    }
    int main(){
        int data[N];
        srand((unsigned)time(0));
        cout<<"排序前\n";
        for (int c=0;c<N;c++){
            data[c]=rand()%10000;
            cout<<data[c]<<'\t';
            }
        bubbleSort(data,N);
        cout<<"排序完成!!\n";
        output(data,N);
        system("PAUSE");
        return 0;
    }

    回覆刪除
  9. #define N 100
    #include <iostream>
    #include <cstdlib>
    #include <ctime>
    using namespace std;
    void swap(int&x,int&y){int t=x;x=y;y=t;}
    void output(int data[],int count)
    {
         for(int i=0; i<count;i++)
             cout<<data[i]<<'\t';
         cout<<endl;
    }
    void bubbleSort (int data[],int count)
    {
         for(int j=count;j>0;j--)
         {
           for(int i=0;i<j-1;i++)
               if(data[i+1]<data[i])
                  swap(data[i],data[i+1]);
         };
    }
    int main()
    {
        int data[N];
        srand((unsigned)time(0));
        cout<<"排序前\n";
        for(int c=0;c<N;c++)
        {
            data[c]=rand()%10000;
            cout<<data[c]<<'\t';   
        }
        bubbleSort(data, N);
        cout<<"排序完成!!\n";
        output(data, N);
        system("pause");
        return 0;
    }

    回覆刪除
  10. #define N 100
    #include <iostream>
    #include <cstdlib>
    #include <ctime>
    using namespace std;
    void swap(int &x, int &y){int t=x;x=y;y=t;}
    void output (int data[], int count){
         for (int i=0; i<count; i++)
              cout<<data[i]<<'\t';
              cout<<endl;
    }
    void bubbleSort(int data [], int count){
         for ( int j = count; j >0 ; j--){
             for ( int i = 0 ; i < j-1; i++)
                 if (data[i+1] < data[i] )
                    swap(data[i],data[i+1]);
    };
    }
    int main(){
        int data[N];
        srand((unsigned)time(0));
        cout<<"排序前\n";
        for (int c=0;c<N;c++){
            data[c]=rand()%10000;
            cout<<data[c]<<'\t';
            }
        bubbleSort(data,N);
        cout<<"排序完成!!\n";
        output(data,N);
        system("PAUSE");
        return 0;
    }

    回覆刪除
  11. #define N 100
    #include <iostream>
    #include <cstdlib>
    #include <ctime>
    using namespace std;
    void swap(int &x, int &y){int t=x;x=y;y=t;}
    void output (int data[], int count){
         for (int i=0; i<count; i++)
              cout<<data[i]<<'\t';
              cout<<endl;
    }
    void bubbleSort(int data [], int count){
         for ( int j = count; j >0 ; j--){
             for ( int i = 0 ; i < j-1; i++)
                 if (data[i+1] < data[i] )
                    swap(data[i],data[i+1]);
    };
    }
    int main(){
        int data[N];
        srand((unsigned)time(0));
        cout<<"排序前\n";
        for (int c=0;c<N;c++){
            data[c]=rand()%10000;
            cout<<data[c]<<'\t';
            }
        bubbleSort(data,N);
        cout<<"排序完成!!\n";
        output(data,N);
        system("PAUSE");
        return 0;
    }

    回覆刪除
  12. #define N 100
    #include <iostream>
    #include <cstdlib>
    #include <ctime>
    using namespace std;
    void swap(int &x, int &y){int t=x;x=y;y=t;}
    void output (int data[], int count){
    for (int i=0; i<count; i++)
    cout<<data[i]<<'\t';
    cout<<endl;
    }
    void bubbleSort(int data [], int count){
    for ( int j = count; j >0 ; j--){
    for ( int i = 0 ; i < j-1; i++)
    if (data[i+1] < data[i] )
    swap(data[i],data[i+1]);
    };
    }
    int main(){
    int data[N];
    srand((unsigned)time(0));
    cout<<"排序前\n";
    for (int c=0;c<N;c++){
    data[c]=rand()%10000;
    cout<<data[c]<<'\t';
    }
    bubbleSort(data,N);
    cout<<"排序完成!!\n";
    output(data,N);
    system("PAUSE");
    return 0;
    }

    回覆刪除
  13. #define N 100
    #include <iostream>
    #include <cstdlib>
    #include <ctime>
    using namespace std;
    void swap(int& X,int& Y){int temp=X;X=Y;Y=temp;}
    void output(int data[], int count){
     for (int i=0; i<count; i++)
      cout<<data[i]<<'\t';
     cout<<endl;
    }
    void bubbleSort(int data [], int count) {
       for ( int j = count; j >0 ; j-- ) {
          for ( int i = 0; i < j-1; i++ )
            if ( data[i+1] < data[i] )
                swap(data[i],data[i+1]);
      
       };  
    }
                           int main(){
                               int data[N];
                               srand((unsigned)time(0));
                               cout<<"排序前\n";
                               for (int c=0;c<N;c++){
                                   data[c]=rand()%10000;
                                   cout<<data[c]<<'\t';
                                   }
                                   bubbleSort(data,N);
                                   cout<<"排序完成!!\n";
                                   output(data,N);
                                   system("PAUSE");
                                   return 0;
                                   }

    回覆刪除
  14. #define N 100
    #include <iostream>
    #include <cstdlib>
    #include <ctime>
    using namespace std;
    void swap(int &x, int &y){int t=x;x=y;y=t;}
    void output (int data[], int count){
         for (int i=0; i<count; i++)
              cout<<data[i]<<'\t';
              cout<<endl;
    }
    void bubbleSort(int data [], int count){
         for ( int j = count; j >0 ; j--){
             for ( int i = 0 ; i < j-1; i++)
                 if (data[i+1] < data[i] )
                    swap(data[i],data[i+1]);
    };
    }
    int main(){
        int data[N];
        srand((unsigned)time(0));
        cout<<"排序前\n";
        for (int c=0;c<N;c++){
            data[c]=rand()%10000;
            cout<<data[c]<<'\t';
            }
        bubbleSort(data,N);
        cout<<"排序完成!!\n";
        output(data,N);
        system("PAUSE");
        return 0;
    }

    回覆刪除
  15. #define N 100
    #include <iostream>
    #include <cstdlib>
    #include <ctime>
    using namespace std;
    void swap(int&x,int&y){int t=x;x=y;y=t;}
    void output(int data[],int count)
    {
         for(int i=0; i<count;i++)
             cout<<data[i]<<'\t';
         cout<<endl;
    }
    void bubbleSort (int data[],int count)
    {
         for(int j=count;j>0;j--)
         {
           for(int i=0;i<j-1;i++)
               if(data[i+1]<data[i])
                  swap(data[i],data[i+1]);
         };
    }
    int main()
    {
        int data[N];
        srand((unsigned)time(0));
        cout<<"排序前\n";
        for(int c=0;c<N;c++)
        {
            data[c]=rand()%10000;
            cout<<data[c]<<'\t';   
        }
        bubbleSort(data, N);
        cout<<"排序完成!!\n";
        output(data, N);
        system("pause");
        return 0;
    }

    回覆刪除
  16. #define N 100
    #include <iostream>
    #include <cstdlib>
    #include <ctime>
    using namespace std;
    void swap(int&x,int&y){int t=x;x=y;y=t;}
    void output(int data[],int count)
    {
         for(int i=0; i<count;i++)
             cout<<data[i]<<'\t';
         cout<<endl;
    }
    void bubbleSort (int data[],int count)
    {
         for(int j=count;j>0;j--)
         {
           for(int i=0;i<j-1;i++)
               if(data[i+1]<data[i])
                  swap(data[i],data[i+1]);
         };
    }
    int main()
    {
        int data[N];
        srand((unsigned)time(0));
        cout<<"排序前\n";
        for(int c=0;c<N;c++)
        {
            data[c]=rand()%10000;
            cout<<data[c]<<'\t';   
        }
        bubbleSort(data, N);
        cout<<"排序完成!!\n";
        output(data, N);
        system("pause");
        return 0;
    }

    回覆刪除
  17. #define N 100
    #include <iostream>
    #include <cstdlib>
    #include <ctime>
    using namespace std;
    void swap(int &x, int &y){int t=x;x=y;y=t;}
    void output (int data[], int count){
         for (int i=0; i<count; i++)
              cout<<data[i]<<'\t';
              cout<<endl;
    }
    void bubbleSort(int data [], int count){
         for ( int j = count; j >0 ; j--){
             for ( int i = 0 ; i < j-1; i++)
                 if (data[i+1] < data[i] )
                    swap(data[i],data[i+1]);
    };
    }
    int main(){
        int data[N];
        srand((unsigned)time(0));
        cout<<"排序前\n";
        for (int c=0;c<N;c++){
            data[c]=rand()%10000;
            cout<<data[c]<<'\t';
            }
        bubbleSort(data,N);
        cout<<"排序完成!!\n";
        output(data,N);
        system("PAUSE");
        return 0;
    }

    回覆刪除
  18. #define N 100
    #include <iostream>
    #include <cstdlib>
    #include <ctime>
    using namespace std;
    void swap(int&x,int&y){int t=x;x=y;y=t;}
    void output(int data[],int count)
    {
         for(int i=0; i<count;i++)
             cout<<data[i]<<'\t';
         cout<<endl;
    }
    void bubbleSort (int data[],int count)
    {
         for(int j=count;j>0;j--)
         {
           for(int i=0;i<j-1;i++)
               if(data[i+1]<data[i])
                  swap(data[i],data[i+1]);
         };
    }
    int main()
    {
        int data[N];
        srand((unsigned)time(0));
        cout<<"排序前\n";
        for(int c=0;c<N;c++)
        {
            data[c]=rand()%10000;
            cout<<data[c]<<'\t';   
        }
        bubbleSort(data, N);
        cout<<"排序完成!!\n";
        output(data, N);
        system("pause");
        return 0;
    }

    回覆刪除
  19. #define N 100
    #include <iostream>
    #include <cstdlib>
    #include <ctime>
    using namespace std;
    void swap (int & X,int & Y){int temp=X;X=Y;Y=temp;}
    void output (int data[], int count){
         for (int i=0;i<count; i++)
         cout<<data[i]<<'\t';
         cout<<endl;
         }
         void bubbleSort(int data [], int count){
              for ( int j = count; j>0 ; j--){
                  for ( int i = 0; i < j-1; i++)
                          if(data[i+1] < data[i])
                          swap(data[i],data[i+1]);
                          };
                          }
                          int main (){
                              int data[N];
                              srand((unsigned)time(0));
                              cout<<"排序前\n";
                              for (int c=0;c<N;c++){
                                  data[c]=rand()%10000;
                                  cout<<data[c]<<'\t';
                                  }
                                  bubbleSort(data,N);
                                  cout<<"排序完成!!\n";
                                  output(data,N);
                                  system("pause");
                                  return 0;
                                  }

    回覆刪除
  20. #define N 100
    #include <iostream>
    #include <cstdlib>
    #include <ctime>
    using namespace std;
    void swap(int &x, int &y){int t=x;x=y;y=t;}
    void output (int data[], int count){
         for (int i=0; i<count; i++)
              cout<<data[i]<<'\t';
              cout<<endl;
    }
    void bubbleSort(int data [], int count){
         for ( int j = count; j >0 ; j--){
             for ( int i = 0 ; i < j-1; i++)
                 if (data[i+1] < data[i] )
                    swap(data[i],data[i+1]);
    };
    }
    int main(){
        int data[N];
        srand((unsigned)time(0));
        cout<<"排序前\n";
        for (int c=0;c<N;c++){
            data[c]=rand()%10000;
            cout<<data[c]<<'\t';
            }
        bubbleSort(data,N);
        cout<<"排序完成!!\n";
        output(data,N);
        system("PAUSE");
        return 0;
    }

    回覆刪除
  21. #define N 100
    #include <iostream>
    #include <cstdlib>
    #include <ctime>
    using namespace std;
    void swap(int &x, int &y){int t=x;x=y;y=t;}
    void output (int data[], int count){
         for (int i=0; i<count; i++)
              cout<<data[i]<<'\t';
              cout<<endl;
    }
    void bubbleSort(int data [], int count){
         for ( int j = count; j >0 ; j--){
             for ( int i = 0 ; i < j-1; i++)
                 if (data[i+1] < data[i] )
                    swap(data[i],data[i+1]);
    };
    }
    int main(){
        int data[N];
        srand((unsigned)time(0));
        cout<<"排序之前_\n";
        for (int c=0;c<N;c++){
            data[c]=rand()%10000;
            cout<<data[c]<<'\t';
            }
        bubbleSort(data,N);
        cout<<"排序完成_\n";
        output(data,N);
        system("PAUSE");
        return 0;
    }

    回覆刪除
  22. #define N 100
    #include <iostream>
    #include <cstdlib>
    #include <ctime>
    using namespace std;
    void swap(int&x,int&y){int t=x;x=y;y=t;}
    void output(int data[],int count)
    {
         for(int i=0; i<count;i++)
             cout<<data[i]<<'\t';
         cout<<endl;
    }
    void bubbleSort (int data[],int count)
    {
         for(int j=count;j>0;j--)
         {
           for(int i=0;i<j-1;i++)
               if(data[i+1]<data[i])
                  swap(data[i],data[i+1]);
         };
    }
    int main()
    {
        int data[N];
        srand((unsigned)time(0));
        cout<<"排序前\n";
        for(int c=0;c<N;c++)
        {
            data[c]=rand()%10000;
            cout<<data[c]<<'\t';   
        }
        bubbleSort(data, N);
        cout<<"排序完成!!\n";
        output(data, N);
        system("pause");
        return 0;
    }

    回覆刪除
  23. #define N 100
    #include<iostream>
    #include<cstdlib>
    #include<ctime>
    using namespace std;
    void swap (int& X,int& Y){int temp=X;X=Y;Y=temp;}
    void output(int data[],int count)
    {
         for (int i=0;i<count; i++)
             cout<<data[i]<<'\t';
             cout<<endl;
    }
    void bubbleSort(int data [],int count)
    {
         for(int j = count-1; j >=0 ; j-- )
         {
                 for(int i=0;i<j;i++)
                 if(data[i+1]<data[i])
                 swap(data[i],data[i+1]);
                 };
    }
    int main(){int data[N];
    srand ((unsigned)time(NULL));
    cout<<"排序前"<<endl;
    for (int x=0;x<N;x++)
    {
        data[x]=rand()%10000;
        cout<<"data["<<x<<"]="<<data[x]<<endl;
    }
        bubbleSort(data, N);
        cout<<"排序完成"<<endl;
        output(data, N);
    system ("pause");
    return 0;
    }

    回覆刪除
  24. #define N 100
    #include<iostream>
    #include<cstdlib>
    #include<ctime>
    using namespace std;
    void swap(int& X,int& Y){int temp=X;X=Y;Y=temp;}
    void output(int data[],int count){
         for(int i=0;i<count;i++)
         cout<<data[i]<<'\t';
         cout<<endl;
         }
        
    void bubbleSort(int data[],int count){
         for(int j=count;j>0;j--){
                 for(int i=0;i<j-1;i++)
                 if(data[i+1]<data[i])
                 swap(data[i],data[i+1]);
                 };
         }
    int main()
    {
        int data[N];
        srand((unsigned)time(0));
        cout<<"排序前\n";
        for (int c=0;c<N;c++){
            data[c]=rand()%10000;
            cout<<data[c]<<'\t';
            }
        bubbleSort(data,N);
        cout<<"排序完成!!\n";
        output(data,N);
        system("Pause");
        return 0;
    }
                

    回覆刪除
  25. <DIV id=yiv1671782922>#define N 100<BR>#include &lt;iostream&gt;<BR>#include &lt;cstdlib&gt;<BR>#include &lt;ctime&gt;<BR>using namespace std;<BR>void swap(int &amp;x, int &amp;y){int t=x;x=y;y=t;}<BR>void output (int data[], int count){<BR>&nbsp;&nbsp;&nbsp;&nbsp; for (int i=0; i&lt;count; i++)<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; cout&lt;&lt;data[i]&lt;&lt;'\t';<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; cout&lt;&lt;endl;<BR>}<BR>void bubbleSort(int data [], int count){<BR>&nbsp;&nbsp;&nbsp;&nbsp; for ( int j = count; j &gt;0 ; j--){<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; for ( int i = 0 ; i &lt; j-1; i++)<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (data[i+1] &lt; data[i] )<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; swap(data[i],data[i+1]);<BR>};<BR>}<BR>int main(){<BR>&nbsp;&nbsp;&nbsp; int data[N];<BR>&nbsp;&nbsp;&nbsp; srand((unsigned)time(0));<BR>&nbsp;&nbsp;&nbsp; cout&lt;&lt;"排序前\n";<BR>&nbsp;&nbsp;&nbsp; for (int c=0;c&lt;N;c++){<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; data[c]=rand()%10000;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; cout&lt;&lt;data[c]&lt;&lt;'\t';<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<BR>&nbsp;&nbsp;&nbsp; bubbleSort(data,N);<BR>&nbsp;&nbsp;&nbsp; cout&lt;&lt;"排序完成!!\n";<BR>&nbsp;&nbsp;&nbsp; output(data,N);<BR>&nbsp;&nbsp;&nbsp; system("PAUSE");<BR>&nbsp;&nbsp;&nbsp; return 0;<BR>}<BR></DIV>

    回覆刪除
  26. <DIV id=yiv1671782922>#define N 100<BR>#include &lt;iostream&gt;<BR>#include &lt;cstdlib&gt;<BR>#include &lt;ctime&gt;<BR>using namespace std;<BR>void swap(int &amp;x, int &amp;y){int t=x;x=y;y=t;}<BR>void output (int data[], int count){<BR>&nbsp;&nbsp;&nbsp;&nbsp; for (int i=0; i&lt;count; i++)<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; cout&lt;&lt;data[i]&lt;&lt;'\t';<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; cout&lt;&lt;endl;<BR>}<BR>void bubbleSort(int data [], int count){<BR>&nbsp;&nbsp;&nbsp;&nbsp; for ( int j = count; j &gt;0 ; j--){<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; for ( int i = 0 ; i &lt; j-1; i++)<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (data[i+1] &lt; data[i] )<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; swap(data[i],data[i+1]);<BR>};<BR>}<BR>int main(){<BR>&nbsp;&nbsp;&nbsp; int data[N];<BR>&nbsp;&nbsp;&nbsp; srand((unsigned)time(0));<BR>&nbsp;&nbsp;&nbsp; cout&lt;&lt;"排序前\n";<BR>&nbsp;&nbsp;&nbsp; for (int c=0;c&lt;N;c++){<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; data[c]=rand()%10000;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; cout&lt;&lt;data[c]&lt;&lt;'\t';<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<BR>&nbsp;&nbsp;&nbsp; bubbleSort(data,N);<BR>&nbsp;&nbsp;&nbsp; cout&lt;&lt;"排序完成!!\n";<BR>&nbsp;&nbsp;&nbsp; output(data,N);<BR>&nbsp;&nbsp;&nbsp; system("PAUSE");<BR>&nbsp;&nbsp;&nbsp; return 0;<BR>}<BR></DIV>

    回覆刪除
  27. <DIV id=yiv1671782922>#define N 100<BR>#include &lt;iostream&gt;<BR>#include &lt;cstdlib&gt;<BR>#include &lt;ctime&gt;<BR>using namespace std;<BR>void swap(int &amp;x, int &amp;y){int t=x;x=y;y=t;}<BR>void output (int data[], int count){<BR>&nbsp;&nbsp;&nbsp;&nbsp; for (int i=0; i&lt;count; i++)<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; cout&lt;&lt;data[i]&lt;&lt;'\t';<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; cout&lt;&lt;endl;<BR>}<BR>void bubbleSort(int data [], int count){<BR>&nbsp;&nbsp;&nbsp;&nbsp; for ( int j = count; j &gt;0 ; j--){<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; for ( int i = 0 ; i &lt; j-1; i++)<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (data[i+1] &lt; data[i] )<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; swap(data[i],data[i+1]);<BR>};<BR>}<BR>int main(){<BR>&nbsp;&nbsp;&nbsp; int data[N];<BR>&nbsp;&nbsp;&nbsp; srand((unsigned)time(0));<BR>&nbsp;&nbsp;&nbsp; cout&lt;&lt;"排序前\n";<BR>&nbsp;&nbsp;&nbsp; for (int c=0;c&lt;N;c++){<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; data[c]=rand()%10000;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; cout&lt;&lt;data[c]&lt;&lt;'\t';<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<BR>&nbsp;&nbsp;&nbsp; bubbleSort(data,N);<BR>&nbsp;&nbsp;&nbsp; cout&lt;&lt;"排序完成!!\n";<BR>&nbsp;&nbsp;&nbsp; output(data,N);<BR>&nbsp;&nbsp;&nbsp; system("PAUSE");<BR>&nbsp;&nbsp;&nbsp; return 0;<BR>}<BR></DIV>

    回覆刪除
  28. class="p_other pic_padding">#define N 100
    #include <iostream>
    #include <cstdlib>
    #include <ctime>
    using namespace std;
    void swap(int &x, int &y){int t=x;x=y;y=t;}
    void output (int data[], int count){
    for (int i=0; i<count; i++)
    cout<<data[i]<<'\t';
    cout<<endl;
    }
    void bubbleSort(int data [], int count){
    for ( int j = count; j >0 ; j--){
    for ( int i = 0 ; i < j-1; i++)
    if (data[i+1] < data[i] )
    swap(data[i],data[i+1]);
    };
    }
    int main(){
    int data[N];
    srand((unsigned)time(0));
    cout<<"排序前\n";
    for (int c=0;c<N;c++){
    data[c]=rand()%10000;
    cout<<data[c]<<'\t';
    }
    bubbleSort(data,N);
    cout<<"排序完成!!\n";
    output(data,N);
    system("PAUSE");
    return 0;
    }

    回覆刪除
  29. #define N 100
    #include <iostream>
    #include <cstdlib>
    #include <ctime>
    using namespace std;
    void swap(int& x,int& y){int t=x;x=y;y=t;}
    void output(int data[],int count){
         for (int i=0;i<count;i++)
         cout<<data[i]<<'\t';
         cout<<endl;
    }
    void bubbleSort(int data[],int count){
         for (int j=count-1;j>=0;j--){
             for (int i=0;i<j;i++)
             if (data[i+1]<data[i])
             swap(data[i],data[i+1]);
             };
    }
    int main(){
        int data[N];
        srand((unsigned)time(0));
        cout<<"排序前\n";
        for (int c=0; c<N; c++){
            data[c]=rand()%10000;
            cout<<data[c]<<'\t';
            }
            bubbleSort (data,N);
            cout<<"排序完成!!\n";
            output(data,N);
        system("pause");
        return 0;
    }

    回覆刪除
  30. #define N 100
    #include <iostream>
    #include <cstdlib>
    #include <ctime>
    using namespace std;
    void swap(int &x, int &y){int t=x;x=y;y=t;}
    void output (int data[], int count){
    for (int i=0; i<count; i++)
    cout<<data[i]<<'\t';
    cout<<endl;
    }
    void bubbleSort(int data [], int count){
    for ( int j = count; j >0 ; j--){
    for ( int i = 0 ; i < j-1; i++)
    if (data[i+1] < data[i] )
    swap(data[i],data[i+1]);
    };
    }
    int main(){
    int data[N];
    srand((unsigned)time(0));
    cout<<"排序前\n";
    for (int c=0;c<N;c++){
    data[c]=rand()%10000;
    cout<<data[c]<<'\t';
    }
    bubbleSort(data,N);
    cout<<"排序完成!!\n";
    output(data,N);
    system("PAUSE");
    return 0;
    }

    回覆刪除
  31. #define N 100
    #include <iostream>
    #include <cstdlib>
    #include <ctime>
    using namespace std;
    void swap(int &x, int &y){int t=x;x=y;y=t;}
    void output (int data[], int count){
    for (int i=0; i<count; i++)
    cout<<data[i]<<'\t';
    cout<<endl;
    }
    void bubbleSort(int data [], int count){
    for ( int j = count; j >0 ; j--){
    for ( int i = 0 ; i < j-1; i++)
    if (data[i+1] < data[i] )
    swap(data[i],data[i+1]);
    };
    }
    int main(){
    int data[N];
    srand((unsigned)time(0));
    cout<<"排序前\n";
    for (int c=0;c<N;c++){
    data[c]=rand()%10000;
    cout<<data[c]<<'\t';
    }
    bubbleSort(data,N);
    cout<<"排序完成!!\n";
    output(data,N);
    system("PAUSE");
    return 0;
    }

    回覆刪除
  32. #define N 100
    #include <iostream>
    #include <cstdlib>
    #include <ctime>
    using namespace std;
    void swap(int &x, int &y){int t=x;x=y;y=t;}
    void output (int data[], int count){
    for (int i=0; i<count; i++)
    cout<<data[i]<<'\t';
    cout<<endl;
    }
    void bubbleSort(int data [], int count){
    for ( int j = count; j >0 ; j--){
    for ( int i = 0 ; i < j-1; i++)
    if (data[i+1] < data[i] )
    swap(data[i],data[i+1]);
    };
    }
    int main(){
    int data[N];
    srand((unsigned)time(0));
    cout<<"排序前\n";
    for (int c=0;c<N;c++){
    data[c]=rand()%10000;
    cout<<data[c]<<'\t';
    }
    bubbleSort(data,N);
    cout<<"排序完成!!\n";
    output(data,N);
    system("PAUSE");
    return 0;
    }

    回覆刪除
  33. #include <iostream>
    #include <cstdlib>
    #include <ctime>
    using namespace std;
    void swap(int &x, int &y){int t=x;x=y;y=t;}
    void output (int data[], int count){
        for (int i=0; i<count; i++)
             cout<<data[i]<<'\t';
             cout<<endl;
    }
    void bubbleSort(int data [], int count){
        for ( int j = count; j >0 ; j--){
            for ( int i = 0 ; i < j-1; i++)
                if (data[i+1] < data[i] )
                   swap(data[i],data[i+1]);
    };
    }
    int main(){
       int data[N];
       srand((unsigned)time(0));
       cout<<"排序前\n";
       for (int c=0;c<N;c++){
           data[c]=rand()%10000;
           cout<<data[c]<<'\t';
           }
       bubbleSort(data,N);
       cout<<"排序完成!!\n";
       output(data,N);
       system("PAUSE");
       return 0;
    }

    回覆刪除
  34. #define N 100
    #include <iostream>
    #include<cstdlib>
    #include<ctime>
    using namespace std;
    void swap(int&x,int&y){int t=x;x=y;y=t;}
    void output(int data[],int count)
    {
         for(int i=0;i<count;i++)
         cout<<data[i]<<'\t';
         cout<<endl;
    }
    void bubbleSort(int data [], int count)
    {    
      for(int j=count;j>0;j--)
      {
              for (int i=0;i<j-1;i++)
              if(data[i+1]<data[i])
              swap(data[i],data[i+1]);
              };
    }
    int main()
    {
        int data[N];
        srand((unsigned)time(0));
        cout<<"Before\n";
        for(int c=0;c<N;c++)
        {
                data[c]=rand()%10000;
                cout<<data[c]<<'\t';
        } 
        bubbleSort(data,N);
        cout<<"After\n";
        output(data,N);
        system("pause");
        return 0;
    }

    回覆刪除
  35. #include <iostream>
    #include <cstdlib>
    #include <ctime>
    using namespace std;
    void swap(int &x, int &y){int t=x;x=y;y=t;}
    void output (int data[], int count){
       for (int i=0; i<count; i++)
            cout<<data[i]<<'\t';
            cout<<endl;
    }
    void bubbleSort(int data [], int count){
       for ( int j = count; j >0 ; j--){
           for ( int i = 0 ; i < j-1; i++)
               if (data[i+1] < data[i] )
                  swap(data[i],data[i+1]);
    };
    }
    int main(){
      int data[N];
      srand((unsigned)time(0));
      cout<<"排序前\n";
      for (int c=0;c<N;c++){
          data[c]=rand()%10000;
          cout<<data[c]<<'\t';
          }
      bubbleSort(data,N);
      cout<<"排序完成!!\n";
      output(data,N);
      system("PAUSE");
      return 0;
    }

    回覆刪除
  36. #define N 100
    #include <iostream>
    #include <cstdlib>
    #include <ctime>
    using namespace std;
    void swap(int &x, int &y){int t=x;x=y;y=t;}
    void output (int data[], int count){
    for (int i=0; i<count; i++)
    cout<<data[i]<<'\t';
    cout<<endl;
    }
    void bubbleSort(int data [], int count){
    for ( int j = count; j >0 ; j--){
    for ( int i = 0 ; i < j-1; i++)
    if (data[i+1] < data[i] )
    swap(data[i],data[i+1]);
    };
    }
    int main(){
    int data[N];
    srand((unsigned)time(0));
    cout<<"排序前\n";
    for (int c=0;c<N;c++){
    data[c]=rand()%10000;
    cout<<data[c]<<'\t';
    }
    bubbleSort(data,N);
    cout<<"排序完成!!\n";
    output(data,N);
    system("PAUSE");
    return 0;
    }

    回覆刪除
  37. #define N 100
    #include <iostream>
    #include <cstdlib>
    #include <ctime>
    using namespace std;
    void swap(int& X,int& Y) {int temp=X;X=Y;Y=temp;}
    void output(int data[], int count){
         for (int i=0; i<count; i++)
         cout<<data[i]<<'\t';
         cout<<endl;
    }
         void bubbleSort(int data [], int count) {
              for ( int j=count-1; j >=0; j--) {
                  for (int i=0;i<j;i++)
                  if ( data[i+1] < data[i] )
                  swap(data[i],data[i+1]);
                
                  };
    }
                  int main()  {
                      int data [N];
                      srand ((unsigned)time(NULL));
                      cout<<"排序前"<<endl;
                      for (int x=0;x<N;x++)
                      {
                          data[x]=rand()%10000;
                          cout<<"data["<<x<<"="<<data[x]<<endl;
                          }
                      bubbleSort(data, N);
                      cout<<"排序後"<<endl;
                      output(data,N);
                      system("PAUSE");
                      return 0;
                      }

    回覆刪除
  38. #define N 100
    #include <iostream>
    #include <cstdlib>
    #include <ctime>
    using namespace std;
    void swap(int&x,int&y){int t=x;x=y;y=t;}
    void output(int data[],int count)
    {
         for(int i=0; i<count;i++)
             cout<<data[i]<<'\t';
         cout<<endl;
    }
    void bubbleSort (int data[],int count)
    {
         for(int j=count;j>0;j--)
         {
           for(int i=0;i<j-1;i++)
               if(data[i+1]<data[i])
                  swap(data[i],data[i+1]);
         };
    }
    int main()
    {
        int data[N];
        srand((unsigned)time(0));
        cout<<"排序前\n";
        for(int c=0;c<N;c++)
        {
            data[c]=rand()%10000;
            cout<<data[c]<<'\t';   
        }
        bubbleSort(data, N);
        cout<<"排序完成!!\n";
        output(data, N);
        system("pause");
        return 0;
    }

    回覆刪除
  39. #include <iostream>
    #include <cstdlib>
    #include <ctime>
    using namespace std;
    void swap(int& X,int& Y){int temp=X;X=Y;Y=temp;}
    void output(int data[], int count){
     for (int i=0; i<count; i++)
      cout<<data[i]<<'\t';
     cout<<endl;
    }
    void bubbleSort(int data [], int count) {
       for ( int j = count; j >0 ; j-- ) {
          for ( int i = 0; i < j-1; i++ )
            if ( data[i+1] < data[i] )
                swap(data[i],data[i+1]);
      }; 
    }
    int main() {
     int a[100];
     srand((unsigned)time(NULL));
     int i=rand();
     for(int x=0; x<100; x++)
     {
        a[x]=rand()%10000;       
        }
     cout<<"排序前\n";
     output(a,100);
        bubbleSort(a, 100);        
     cout<<"排序後\n";
     output(a,100);
        system("PAUSE");
        return 0;
    }

    回覆刪除
  40. #define N 100
    #include <iostream>
    #include <cstdlib>
    #include <ctime>
    using namespace std;
    void swap(int&x,int&y){int t=x;x=y;y=t;}
    void output(int data[],int count)
    {
        for(int i=0; i<count;i++)
            cout<<data[i]<<'\t';
        cout<<endl;
    }
    void bubbleSort (int data[],int count)
    {
        for(int j=count;j>0;j--)
        {
          for(int i=0;i<j-1;i++)
              if(data[i+1]<data[i])
                 swap(data[i],data[i+1]);
        };
    }
    int main()
    {
       int data[N];
       srand((unsigned)time(0));
       cout<<"排序前\n";
       for(int c=0;c<N;c++)
       {
           data[c]=rand()%10000;
           cout<<data[c]<<'\t';    
       }
       bubbleSort(data, N);
       cout<<"排序完成!!\n";
       output(data, N);
       system("pause");
       return 0;
    }

    回覆刪除
  41. #define N 100
    #include <iostream>
    #include <cstdlib>
    #include <ctime>
    using namespace std;
    void swap(int &x, int &y){int t=x;x=y;y=t;}
    void output (int data[], int count){
    for (int i=0; i<count; i++)
    cout<<data[i]<<'\t';
    cout<<endl;
    }
    void bubbleSort(int data [], int count){
    for ( int j = count; j >0 ; j--){
    for ( int i = 0 ; i < j-1; i++)
    if (data[i+1] < data[i] )
    swap(data[i],data[i+1]);
    };
    }
    int main(){
    int data[N];
    srand((unsigned)time(0));
    cout<<"排序前\n";
    for (int c=0;c<N;c++){
    data[c]=rand()%10000;
    cout<<data[c]<<'\t';
    }
    bubbleSort(data,N);
    cout<<"排序完成!!\n";
    output(data,N);
    system("PAUSE");
    return 0;
    }

    回覆刪除
  42. #define N 100
    #include <iostream>
    #include <cstdlib>
    #include <ctime>
    using namespace std;
    void swap(int &x, int &y){int t=x;x=y;y=t;}
    void output (int data[], int count){
         for (int i=0; i<count; i++)
              cout<<data[i]<<'\t';
              cout<<endl;
    }
    void bubbleSort(int data [], int count){
         for ( int j = count; j >0 ; j--){
             for ( int i = 0 ; i < j-1; i++)
                 if (data[i+1] < data[i] )
                    swap(data[i],data[i+1]);
    };
    }
    int main(){
        int data[N];
        srand((unsigned)time(0));
        cout<<"排序前\n";
        for (int c=0;c<N;c++){
            data[c]=rand()%10000;
            cout<<data[c]<<'\t';
            }
        bubbleSort(data,N);
        cout<<"排序完成!!\n";
        output(data,N);
        system("PAUSE");
        return 0;
    }

    回覆刪除
  43. #define N 100
    #include <iostream>
    #include <cstdlib>
    #include <ctime>
    using namespace std;
    void swap(int&x,int&y){int t=x;x=y;y=t;}
    void output(int data[],int count)
    {
         for(int i=0; i<count;i++)
             cout<<data[i]<<'\t';
         cout<<endl;
    }
    void bubbleSort (int data[],int count)
    {
         for(int j=count;j>0;j--)
         {
           for(int i=0;i<j-1;i++)
               if(data[i+1]<data[i])
                  swap(data[i],data[i+1]);
         };
    }
    int main()
    {
        int data[N];
        srand((unsigned)time(0));
        cout<<"排序前\n";
        for(int c=0;c<N;c++)
        {
            data[c]=rand()%10000;
            cout<<data[c]<<'\t';   
        }
        bubbleSort(data, N);
        cout<<"排序完成!!\n";
        output(data, N);
        system("pause");
        return 0;
    }

    回覆刪除
  44. #define N 100
    #include <iostream>
    #include <cstdlib>
    #include <ctime>
    using namespace std;
    void swap(int&x,int&y){int t=x;x=y;y=t;}
    void output(int data[],int count)
    {
         for(int i=0; i<count;i++)
             cout<<data[i]<<'\t';
         cout<<endl;
    }
    void bubbleSort (int data[],int count)
    {
         for(int j=count;j>0;j--)
         {
           for(int i=0;i<j-1;i++)
               if(data[i+1]<data[i])
                  swap(data[i],data[i+1]);
         };
    }
    int main()
    {
        int data[N];
        srand((unsigned)time(0));
        cout<<"排序前\n";
        for(int c=0;c<N;c++)
        {
            data[c]=rand()%10000;
            cout<<data[c]<<'\t';   
        }
        bubbleSort(data, N);
        cout<<"排序完成!!\n";
        output(data, N);
        system("pause");
        return 0;
    }

    回覆刪除

HTML 編輯器