網頁

2009年5月19日 星期二

產生100個亂數介於0~512, 並排序!

產生100個亂數介於0~512, 並排序!

72 則留言:

  1. #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 a[], int count){
     for (int i=0; i<count; i++)
      cout<<a[i]<<'\t';
     cout<<endl;
    }
    void bubbleSort(int a [], int count) {
       for ( int j = count; j >0 ; j-- ) {
          for ( int i = 0; i < j-1; i++ )
            if ( a[i+1] < a[i] )
                swap(a[i],a[i+1]); 
       };  
    }
    int main() {
      unsigned starttime=clock();
        int a[100];
        srand((unsigned)time(NULL));
        int i =rand();
        for (int x=0;x<101;x++)
        {
            a[x]=rand()%513;
        }
        bubbleSort(a, 100);        
     cout<<"\nFinal result:\n";
     output(a,100);
        unsigned endtime=clock();
        cout<<"CPU time:"
            <<(double)(endtime-starttime)/CLK_TCK
            <<"sec"<<endl; 
        system("PAUSE");
        return 0;
    }

    回覆刪除
  2. #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 a[], int count){
     for (int i=0; i<count; i++)
      cout<<a[i]<<'\t';
     cout<<endl;
    }
    void bubbleSort(int a [], int count) {
       for ( int j = count; j >0 ; j-- ) {
          for ( int i = 0; i < j-1; i++ )
            if ( a[i+1] < a[i] )
                swap(a[i],a[i+1]);
       };  
    }
    int main() {
        unsigned starttime=clock();
        int a[100];
        srand((unsigned)time(NULL));
        int i =rand();
        cout<<i<<endl;
        for (int x=0;x<101;x++)
        {
            a[x]=rand()%513;
        }
        bubbleSort(a, 100);        
     cout<<"\nFinal result:\n";
     output(a,100);
        unsigned endtime=clock();
        cout<<"CPU time:"
            <<(double)(endtime-starttime)/CLK_TCK
            <<"sec"<<endl;
        system("PAUSE");
        return 0;
    }

    回覆刪除
  3. #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 a[], int count){
     for (int i=0; i<count; i++)
      cout<<a[i]<<'\t';
     cout<<endl;
    }
    void bubbleSort(int a [], int count) {
       for ( int j = count; j >0 ; j-- ) {
          for ( int i = 0; i < j-1; i++ )
            if ( a[i+1] < a[i] )
                swap(a[i],a[i+1]);
       };  
    }
    int main() {
        unsigned starttime=clock();
        int a[100];
        srand((unsigned)time(NULL));
        int i =rand();
        cout<<i<<endl;
        for (int x=0;x<101;x++)
        {
            a[x]=rand()%513;
        }
     bubbleSort(a, 100);        
     cout<<"\nFinal result:\n";
     output(a,100);
        unsigned endtime=clock();
        cout<<"CPU time:"
            <<(double)(endtime-starttime)/CLK_TCK
            <<"sec"<<endl;
        system("PAUSE");
        return 0;
    }

    回覆刪除
  4. #include <iostream>
    #include <ctime>
    #include <cstdlib>
    using namespace std;
    void swap(int& X,int& Y){int temp=X;X=Y;Y=temp;}
    void output(int a[], int count){
     for (int i=0; i<count; i++)
      cout<<a[i]<<'\t';
         cout<<endl;
        }
    void bubbleSort(int a[], int count) {
       for ( int j = count; j >0 ; j-- ) {
          for ( int i = 0; i < j-1; i++ )
            if ( a[i+1] < a[i] )
                swap(a[i],a[i+1]);
       };  
    }
    int main()
    {
        unsigned starttime=clock();
        int a[100];
        srand((unsigned)time(NULL));
        int i =rand();
        for (int i=0;i<101;i++)
        {
            a[i]=rand()%513;
        }
        bubbleSort(a, 100);        
     cout<<"\nFinal result:\n";
     output(a,100);
        unsigned endtime=clock();
        cout<<"CPU time:"
            <<(double)(endtime-starttime)/CLK_TCK
            <<"sec"<<endl;
        system("PAUSE");
        return 0;
    }

    回覆刪除
  5. #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 a[], int count){
     for (int i=0; i<count; i++)
      cout<<a[i]<<'\t';
     cout<<endl;
    }
    void bubbleSort(int a [], int count) {
       for ( int j = count; j >0 ; j-- ) {
          for ( int i = 0; i < j-1; i++ )
            if ( a[i+1] < a[i] )
                swap(a[i],a[i+1]);
       };  
    }
    int main() {
        unsigned starttime=clock();
        int a[100];
        srand((unsigned)time(NULL));
        int i =rand();
        cout<<i<<endl;
        for (int x=0;x<101;x++)
        {
            a[x]=rand()%513;
            cout<<"a["<<x<<"]="<<a[x]<<endl;
        }
     bubbleSort(a, 100);        
     cout<<"\nFinal result:\n";
     output(a,100);
        unsigned endtime=clock();
        cout<<"CPU time:"
            <<(double)(endtime-starttime)/CLK_TCK
            <<"sec"<<endl;
        system("PAUSE");
        return 0;
    }

    回覆刪除
  6. #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 a[], int count){
     for (int i=0; i<count; i++)
      cout<<a[i]<<'\t';
     cout<<endl;
    }
    void bubbleSort(int a [], int count) {
       for ( int j = count; j >0 ; j-- ) {
          for ( int i = 0; i < j-1; i++ )
            if ( a[i+1] < a[i] )
                swap(a[i],a[i+1]);
         
       };  
    }
    int main() {
      unsigned starttime=clock();
        int a[100];
        srand((unsigned)time(NULL));
        int i =rand();
        for (int x=0;x<101;x++)
        {
            a[x]=rand()%513;
            cout<<"a["<<x<<"]="<<a[x]<<endl;
        }
        bubbleSort(a, 100);        
     cout<<"\nFinal result:\n";
     output(a,100);
        unsigned endtime=clock();
        cout<<"CPU time:"
            <<(double)(endtime-starttime)/CLK_TCK
            <<"sec"<<endl; 
        system("PAUSE");
        return 0;
    }

    回覆刪除
  7. #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 a[], int count){
     for (int i=0; i<count; i++)
      cout<<a[i]<<'\t';
     cout<<endl;
    }
    void bubbleSort(int a [], int count) {
       for ( int j = count; j >0 ; j-- ) {
          for ( int i = 0; i < j-1; i++ )
            if ( a[i+1] < a[i] )
                swap(a[i],a[i+1]);
       };  
    }
    int main() {
        unsigned starttime=clock();
        int a[100];
        srand((unsigned)time(NULL));
        int i =rand();
        cout<<i<<endl;
        for (int x=0;x<101;x++)
        {
            a[x]=rand()%513;
            cout<<"a["<<x<<"]="<<a[x]<<endl;
        }
        bubbleSort(a, 100);        
     cout<<"\nFinal result:\n";
     output(a,100);
        unsigned endtime=clock();
        cout<<"CPU time:"
            <<(double)(endtime-starttime)/CLK_TCK
            <<"sec"<<endl;
        system("PAUSE");
        return 0;
    }

    回覆刪除
  8. #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 a[], int count){
     for (int i=0; i<count; i++)
      cout<<a[i]<<'\t';
     cout<<endl;
    }
    void bubbleSort(int a [], int count) {
       for ( int j = count; j >0 ; j-- ) {
          for ( int i = 0; i < j-1; i++ )
            if ( a[i+1] < a[i] )
                swap(a[i],a[i+1]);
       };  
    }
    int main() {
        unsigned starttime=clock();
        int a[100];
        srand((unsigned)time(NULL));
        int i =rand();
        cout<<i<<endl;
        for (int x=0;x<101;x++)
        {
            a[x]=rand()%513;
            cout<<"a["<<x<<"]="<<a[x]<<endl;
        }
        bubbleSort(a, 100);        
     cout<<"\nFinal result:\n";
     output(a,100);
        unsigned endtime=clock();
        cout<<"CPU time:"
            <<(double)(endtime-starttime)/CLK_TCK
            <<"sec"<<endl;
        system("PAUSE");
        return 0;
    }

    回覆刪除
  9. #include <iostream>
    #include <ctime>
    #include <cstdlib>
    using namespace std;
    void swap(int& X,int& Y){int temp=X;X=Y;Y=temp;}
    void output(int a[], int count){
     for (int i=0; i<count; i++)
      cout<<a[i]<<'\t';
         cout<<endl;
        }
    void bubbleSort(int a[], int count) {
       for ( int j = count; j >0 ; j-- ) {
          for ( int i = 0; i < j-1; i++ )
            if ( a[i+1] < a[i] )
                swap(a[i],a[i+1]);
       };  
    }
    int main()
    {
        unsigned starttime=clock();
        int a[100];
        srand((unsigned)time(NULL));
        int i =rand();
        for (int i=0;i<101;i++)
        {
            a[i]=rand()%513;
            cout<<"a["<<i<<"]="<<a[i]<<endl;
        }
        bubbleSort(a, 100);        
     cout<<"\nFinal result:\n";
     output(a,100);
        unsigned endtime=clock();
        cout<<"CPU time:"
            <<(double)(endtime-starttime)/CLK_TCK
            <<"sec"<<endl;
        system("PAUSE");
        return 0;
    }

    回覆刪除
  10. #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 a[], int count){
     for (int i=0; i<count; i++)
      cout<<a[i]<<'\t';
     cout<<endl;
    }
    void bubbleSort(int a[], int count) {
       for ( int j = count; j >0 ; j-- ) {
          for ( int i = 0; i < j-1; i++ )
            if ( a[i+1] < a[i] )
                swap(a[i],a[i+1]);
            cout<<'\n'<<count-j+1<<" bubble(s) up->"<<endl;
      output(a,count);
       };  
    }
    int main()
    {
        unsigned starttime=clock();
        int a[100];
        srand((unsigned)time(NULL));
        for (int i=0;i<101;i++)
        {
            a[i]=rand()%513;
        }
        bubbleSort(a, 100);        
     cout<<"\nFinal result:\n";
     output(a,100);
        system("PAUSE");
        return 0;
    }

    回覆刪除
  11. #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 a[], int count){
     for (int i=0; i<count; i++)
      cout<<a[i]<<'\t';
     cout<<endl;
    }
    void bubbleSort(int a[], int count) {
       for ( int j = count; j >0 ; j-- ) {
          for ( int i = 0; i < j-1; i++ )
            if ( a[i+1] < a[i] )
                swap(a[i],a[i+1]);
            cout<<'\n'<<count-j+1<<" bubble(s) up->"<<endl;
      output(a,count);
       };  
    }
    int main()
    {
        unsigned starttime=clock();
        int a[100];
        srand((unsigned)time(NULL));
        for (int i=0;i<101;i++)
        {
            a[i]=rand()%513;
        }
        bubbleSort(a, 100);        
     cout<<"\nFinal result:\n";
     output(a,100);
        system("PAUSE");
        return 0;
    }

    回覆刪除
  12. #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 a[], int count){
     for (int i=0; i<count; i++)
      cout<<a[i]<<'\t';
     cout<<endl;
    }
    void bubbleSort(int a[], int count) {
       for ( int j = count; j >0 ; j-- ) {
          for ( int i = 0; i < j-1; i++ )
            if ( a[i+1] < a[i] )
                swap(a[i],a[i+1]);
            cout<<'\n'<<count-j+1<<" bubble(s) up->"<<endl;
      output(a,count);
       };  
    }
    int main()
    {
        unsigned starttime=clock();
        int a[100];
        srand((unsigned)time(NULL));
        for (int i=0;i<101;i++)
        {
            a[i]=rand()%513;
        }
        bubbleSort(a, 100);        
     cout<<"\nFinal result:\n";
     output(a,100);
        system("PAUSE");
        return 0;
    }

    回覆刪除
  13. #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 a[], int count){
     for (int i=0; i<count; i++)
      cout<<a[i]<<'\t';
     cout<<endl;
    }
    void bubbleSort(int a[], int count) {
       for ( int j = count; j >0 ; j-- ) {
          for ( int i = 0; i < j-1; i++ )
            if ( a[i+1] < a[i] )
                swap(a[i],a[i+1]);
       };  
    }
    int main()
    {
        unsigned starttime=clock();
        int a[100];
        srand((unsigned)time(NULL));
        for (int i=1;i<101;i++)
        {
            a[i]=rand()%513;
            cout<<"a["<<i<<"]="<<a[i]<<endl;
        }
        bubbleSort(a, 100);        
     cout<<"\nFinal result:\n";
     output(a,100);
        system("PAUSE");
        return 0;
    }

    回覆刪除
  14. #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 a[], int count){
     for (int i=0; i<count; i++)
      cout<<a[i]<<'\t';
     cout<<endl;
    }
    void bubbleSort(int a[], int count) {
       for ( int j = count; j >0 ; j-- ) {
          for ( int i = 0; i < j-1; i++ )
            if ( a[i+1] < a[i] )
                swap(a[i],a[i+1]);
            cout<<'\n'<<count-j+1<<" bubble(s) up->"<<endl;
      output(a,count);
       };  
    }
    int main()
    {
        unsigned starttime=clock();
        int a[100];
        srand((unsigned)time(NULL));
        for (int i=0;i<101;i++)
        {
            a[i]=rand()%513;
        }
        bubbleSort(a, 100);        
     cout<<"\nFinal result:\n";
     output(a,100);
        system("PAUSE");
        return 0;
    }

    回覆刪除
  15. #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 a[], int count){
     for (int i=0; i<count; i++)
      cout<<a[i]<<'\t';
     cout<<endl;
    }
    void random100(int a[], int len)
    {
        srand((unsigned)time(NULL));
        for (int x=0;x<len;x++)
        {
            a[x]=rand()%513;
            cout<<"a["<<x<<"]="<<a[x]<<endl;
        }
    }
    void bubbleSort(int a [], int count) {
       for ( int j = count; j >0 ; j-- ) {
          for ( int i = 0; i < j-1; i++ )
            if ( a[i+1] < a[i] )
                swap(a[i],a[i+1]);
       };  
    }
    int main()
    {
        int a[100];
        random100(a,100);
     bubbleSort(a,100);        
     cout<<"\nFinal result:\n";
     output(a,100);
        system("PAUSE");
        return 0;
    }

    回覆刪除
  16. #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 a[], int count){
     for (int i=0; i<count; i++)
      cout<<a[i]<<'\t';
     cout<<endl;
    }
    void bubbleSort(int a[], int count) {
       for ( int j = count; j >0 ; j-- ) {
          for ( int i = 0; i < j-1; i++ )
            if ( a[i+1] < a[i] )
                swap(a[i],a[i+1]);
            cout<<'\n'<<count-j+1<<" bubble(s) up->"<<endl;
      output(a,count);
       };  
    }
    int main()
    {
        unsigned starttime=clock();
        int a[100];
        srand((unsigned)time(NULL));
        for (int i=0;i<101;i++)
        {
            a[i]=rand()%513;
        }
        bubbleSort(a, 100);        
     cout<<"\nFinal result:\n";
     output(a,100);
        system("PAUSE");
        return 0;
    }

    回覆刪除
  17. #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 a[], int count){
     for (int i=0; i<count; i++)
      cout<<a[i]<<'\t';
     cout<<endl;
    }
    void random100(int a[], int len)
    {
        srand((unsigned)time(NULL));
        for (int x=0;x<len;x++)
        {
            a[x]=rand()%513;
            cout<<"a["<<x<<"]="<<a[x]<<endl;
        }
    }
    void bubbleSort(int a [], int count) {
       for ( int j = count; j >0 ; j-- ) {
          for ( int i = 0; i < j-1; i++ )
            if ( a[i+1] < a[i] )
                swap(a[i],a[i+1]);
       };  
    }
    int main()
    {
        int a[100];
        random100(a,100);
     bubbleSort(a,100);        
     cout<<"\nFinal result:\n";
     output(a,100);
        system("PAUSE");
        return 0;
    }

    回覆刪除
  18. #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 a[], int count){
     for (int i=0; i<count; i++)
      cout<<a[i]<<'\t';
     cout<<endl;
    }
    void random100(int a[], int len)
    {
        srand((unsigned)time(NULL));
        for (int x=0;x<len;x++)
        {
            a[x]=rand()%513;
            cout<<"a["<<x<<"]="<<a[x]<<endl;
        }
    }
    void bubbleSort(int a [], int count) {
       for ( int j = count; j >0 ; j-- ) {
          for ( int i = 0; i < j-1; i++ )
            if ( a[i+1] < a[i] )
                swap(a[i],a[i+1]);
       };  
    }
    int main()
    {
        int a[100];
        random100(a,100);
     bubbleSort(a,100);        
     cout<<"\nFinal result:\n";
     output(a,100);
        system("PAUSE");
        return 0;
    }

    回覆刪除
  19. #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 a[], int count){
     for (int i=0; i<count; i++)
      cout<<a[i]<<'\t';
     cout<<endl;
    }
    void bubbleSort(int a [], int count) {
       for ( int j = count; j >0 ; j-- ) {
          for ( int i = 0; i < j-1; i++ )
            if ( a[i+1] < a[i] )
                swap(a[i],a[i+1]);
       };  
    }
    int main() {
        unsigned starttime=clock();
        int a[100];
        srand((unsigned)time(NULL));
        for (int x=0;x<101;x++)
        {
            a[x]=rand()%513;
            cout<<"a["<<x<<"]="<<a[x]<<endl;
        }
        bubbleSort(a, 100);        
     cout<<"\nFinal result:\n";
     output(a,100);
        unsigned endtime=clock();
        cout<<"CPU time:"
            <<(double)(endtime-starttime)/CLK_TCK
            <<"sec"<<endl;
        system("PAUSE");
        return 0;
    }

    回覆刪除
  20. #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 a[], int count){
     for (int i=0; i<count; i++)
      cout<<a[i]<<'\t';
     cout<<endl;
    }
    void bubbleSort(int a [], int count) {
       for ( int j = count; j >0 ; j-- ) {
          for ( int i = 0; i < j-1; i++ )
            if ( a[i+1] < a[i] )
                swap(a[i],a[i+1]);
       };  
    }
    int main() {
        unsigned starttime=clock();
        int a[100];
        srand((unsigned)time(NULL));
        for (int x=0;x<101;x++)
        {
            a[x]=rand()%513;
            cout<<"a["<<x<<"]="<<a[x]<<endl;
        }
     bubbleSort(a, 100);        
     cout<<"\nFinal result:\n";
     output(a,100);
        unsigned endtime=clock();
        cout<<"CPU time:"
            <<(double)(endtime-starttime)/CLK_TCK
            <<"sec"<<endl;
        system("PAUSE");
        return 0;
    }

    回覆刪除
  21. #include <stdio.h>
    #include <iostream>
    using namespace std;
    #include <stdlib.h>
    #include <time.h>
    void random100(int a[],int len)
    {
    srand((unsigned)time(NULL));
    printf("排序前\n");
    for (int x=0;x<len ;x++)
    {
    a[x]=rand()%512;
    printf("%d\t",a[x]);
    }
    }
    void swap(int& X,int& Y){int temp=X;X=Y;Y=temp;}
    void output(int a[], int count){
    for (int i=0; i<count; i++)
    cout<<a[i]<<'\t';
    cout<<endl;
    }
    void bubbleSort(int a [], int count)
    {
    for ( int j = count; j >0 ; j-- )
    {
    for ( int i = 0; i < j-1; i++ )
    {
    if ( a[i+1] < a[i] )
    swap(a[i],a[i+1]);
    }
    }
    }

    int main()
    {
    int a[100];
    random100(a,100);
    bubbleSort(a,100);
    cout<<"排序後"<<"\t"<<endl;
    output(a,100);
    system ("pause");
    return 0;
    }

    回覆刪除
  22. #include <stdio.h>
    #include <iostream>
    using namespace std;
    #include <stdlib.h>
    #include <time.h>
    void random100(int a[],int len)
    {
    srand((unsigned)time(NULL));
    printf("排序前\n");
    for (int x=0;x<len ;x++)
    {
    a[x]=rand()%512;
    printf("%d\t",a[x]);
    }
    }
    void swap(int& X,int& Y){int temp=X;X=Y;Y=temp;}
    void output(int a[], int count){
    for (int i=0; i<count; i++)
    cout<<a[i]<<'\t';
    cout<<endl;
    }
    void bubbleSort(int a [], int count)
    {
    for ( int j = count; j >0 ; j-- )
    {
    for ( int i = 0; i < j-1; i++ )
    {
    if ( a[i+1] < a[i] )
    swap(a[i],a[i+1]);
    }
    }
    }

    int main()
    {
    int a[100];
    random100(a,100);
    bubbleSort(a,100);
    cout<<"排序後"<<"\t"<<endl;
    output(a,100);
    system ("pause");
    return 0;
    }

    回覆刪除
  23. #include <stdio.h>
    #include <iostream>
    using namespace std;
    #include <stdlib.h>
    #include <time.h>
    void random100(int a[],int len)
    {
    srand((unsigned)time(NULL));
    printf("排序前\n");
    for (int x=0;x<len ;x++)
    {
    a[x]=rand()%512;
    printf("%d\t",a[x]);
    }
    }
    void swap(int& X,int& Y){int temp=X;X=Y;Y=temp;}
    void output(int a[], int count){
    for (int i=0; i<count; i++)
    cout<<a[i]<<'\t';
    cout<<endl;
    }
    void bubbleSort(int a [], int count)
    {
    for ( int j = count; j >0 ; j-- )
    {
    for ( int i = 0; i < j-1; i++ )
    {
    if ( a[i+1] < a[i] )
    swap(a[i],a[i+1]);
    }
    }
    }

    int main()
    {
    int a[100];
    random100(a,100);
    bubbleSort(a,100);
    cout<<"排序後"<<"\t"<<endl;
    output(a,100);
    system ("pause");
    return 0;
    }

    回覆刪除
  24. #include <stdio.h>
    #include <iostream>
    using namespace std;
    #include <stdlib.h>
    #include <time.h>
    void random100(int a[],int len)
    {
    srand((unsigned)time(NULL));
    printf("排序前\n");
    for (int x=0;x<len ;x++)
    {
    a[x]=rand()%512;
    printf("%d\t",a[x]);
    }
    }
    void swap(int& X,int& Y){int temp=X;X=Y;Y=temp;}
    void output(int a[], int count){
    for (int i=0; i<count; i++)
    cout<<a[i]<<'\t';
    cout<<endl;
    }
    void bubbleSort(int a [], int count)
    {
    for ( int j = count; j >0 ; j-- )
    {
    for ( int i = 0; i < j-1; i++ )
    {
    if ( a[i+1] < a[i] )
    swap(a[i],a[i+1]);
    }
    }
    }

    int main()
    {
    int a[100];
    random100(a,100);
    bubbleSort(a,100);
    cout<<"排序後"<<"\t"<<endl;
    output(a,100);
    system ("pause");
    return 0;
    }

    回覆刪除
  25. #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 a[], int count){
    for (int i=0; i<count; i++)
    cout<<a[i]<<'\t';
    cout<<endl;
    }
    void bubbleSort(int a[], int count) {
      for ( int j = count; j >0 ; j-- ) {
         for ( int i = 0; i < j-1; i++ )
           if ( a[i+1] < a[i] )
               swap(a[i],a[i+1]);
      };   
    }
    int main()
    {
       unsigned starttime=clock();
       int a[100];
       srand((unsigned)time(NULL));
       for (int i=1;i<101;i++)
       {
           a[i]=rand()%513;
           cout<<"a["<<i<<"]="<<a[i]<<endl;
       }
       bubbleSort(a, 100);         
    cout<<"\nFinal result:\n";
    output(a,100);
       system("PAUSE");
       return 0;
    }

    回覆刪除
  26. #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 a[], int count){
    for (int i=0; i<count; i++)
    cout<<a[i]<<'\t';
    cout<<endl;
    }
    void bubbleSort(int a[], int count) {
      for ( int j = count; j >0 ; j-- ) {
         for ( int i = 0; i < j-1; i++ )
           if ( a[i+1] < a[i] )
               swap(a[i],a[i+1]);
      };   
    }
    int main()
    {
       unsigned starttime=clock();
       int a[100];
       srand((unsigned)time(NULL));
       for (int i=1;i<101;i++)
       {
           a[i]=rand()%513;
           cout<<"a["<<i<<"]="<<a[i]<<endl;
       }
       bubbleSort(a, 100);         
    cout<<"\nFinal result:\n";
    output(a,100);
       system("PAUSE");
       return 0;
    }

    回覆刪除
  27. #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 a[], int count){
    for (int i=0; i<count; i++)
    cout<<a[i]<<'\t';
    cout<<endl;
    }
    void bubbleSort(int a[], int count) {
      for ( int j = count; j >0 ; j-- ) {
         for ( int i = 0; i < j-1; i++ )
           if ( a[i+1] < a[i] )
               swap(a[i],a[i+1]);
      };   
    }
    int main()
    {
       unsigned starttime=clock();
       int a[100];
       srand((unsigned)time(NULL));
       for (int i=1;i<101;i++)
       {
           a[i]=rand()%513;
           cout<<"a["<<i<<"]="<<a[i]<<endl;
       }
       bubbleSort(a, 100);         
    cout<<"\nFinal result:\n";
    output(a,100);
       system("PAUSE");
       return 0;
    }

    回覆刪除
  28. #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 a[], int count){
    for (int i=0; i<count; i++)
    cout<<a[i]<<'\t';
    cout<<endl;
    }
    void bubbleSort(int a[], int count) {
      for ( int j = count; j >0 ; j-- ) {
         for ( int i = 0; i < j-1; i++ )
           if ( a[i+1] < a[i] )
               swap(a[i],a[i+1]);
      };   
    }
    int main()
    {
       unsigned starttime=clock();
       int a[100];
       srand((unsigned)time(NULL));
       for (int i=1;i<101;i++)
       {
           a[i]=rand()%513;
           cout<<"a["<<i<<"]="<<a[i]<<endl;
       }
       bubbleSort(a, 100);         
    cout<<"\nFinal result:\n";
    output(a,100);
       system("PAUSE");
       return 0;
    }

    回覆刪除
  29. #include <stdio.h>
    #include <iostream>
    using namespace std;
    #include <stdlib.h>
    #include <time.h>
    void random100(int a[],int len)
    {
    srand((unsigned)time(NULL));
    printf("排序前\n");
    for (int x=0;x<len ;x++)
    {
    a[x]=rand()%512;
    printf("%d\t",a[x]);
    }
    }
    void swap(int& X,int& Y){int temp=X;X=Y;Y=temp;}
    void output(int a[], int count){
    for (int i=0; i<count; i++)
    cout<<a[i]<<'\t';
    cout<<endl;
    }
    void bubbleSort(int a [], int count)
    {
    for ( int j = count; j >0 ; j-- )
    {
    for ( int i = 0; i < j-1; i++ )
    {
    if ( a[i+1] < a[i] )
    swap(a[i],a[i+1]);
    }
    }
    }

    int main()
    {
    int a[100];
    random100(a,100);
    bubbleSort(a,100);
    cout<<"排序後"<<"\t"<<endl;
    output(a,100);
    system ("pause");
    return 0;
    }

    回覆刪除
  30. #include <stdio.h>
    #include <iostream>
    using namespace std;
    #include <stdlib.h>
    #include <time.h>
    void random100(int a[],int len)
    {
    srand((unsigned)time(NULL));
    printf("排序前\n");
    for (int x=0;x<len ;x++)
    {
    a[x]=rand()%512;
    printf("%d\t",a[x]);
    }
    }
    void swap(int& X,int& Y){int temp=X;X=Y;Y=temp;}
    void output(int a[], int count){
    for (int i=0; i<count; i++)
    cout<<a[i]<<'\t';
    cout<<endl;
    }
    void bubbleSort(int a [], int count)
    {
    for ( int j = count; j >0 ; j-- )
    {
    for ( int i = 0; i < j-1; i++ )
    {
    if ( a[i+1] < a[i] )
    swap(a[i],a[i+1]);
    }
    }
    }

    int main()
    {
    int a[100];
    random100(a,100);
    bubbleSort(a,100);
    cout<<"排序後"<<"\t"<<endl;
    output(a,100);
    system ("pause");
    return 0;
    }

    回覆刪除
  31. #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 a[], int count){
    for (int i=0; i<count; i++)
    cout<<a[i]<<'\t';
    cout<<endl;
    }
    void bubbleSort(int a[], int count) {
      for ( int j = count; j >0 ; j-- ) {
         for ( int i = 0; i < j-1; i++ )
           if ( a[i+1] < a[i] )
               swap(a[i],a[i+1]);
      };   
    }
    int main()
    {
       unsigned starttime=clock();
       int a[100];
       srand((unsigned)time(NULL));
       for (int i=1;i<101;i++)
       {
           a[i]=rand()%513;
           cout<<"a["<<i<<"]="<<a[i]<<endl;
       }
       bubbleSort(a, 100);         
    cout<<"\nFinal result:\n";
    output(a,100);
       system("PAUSE");
       return 0;
    }

    回覆刪除
  32. #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 a[], int count){
    for (int i=0; i<count; i++)
    cout<<a[i]<<'\t';
    cout<<endl;
    }
    void bubbleSort(int a[], int count) {
      for ( int j = count; j >0 ; j-- ) {
         for ( int i = 0; i < j-1; i++ )
           if ( a[i+1] < a[i] )
               swap(a[i],a[i+1]);
      };  
    }
    int main()
    {
       unsigned starttime=clock();
       int a[100];
       srand((unsigned)time(NULL));
       for (int i=1;i<101;i++)
       {
           a[i]=rand()%513;
           cout<<"a["<<i<<"]="<<a[i]<<endl;
       }
       bubbleSort(a, 100);        
    cout<<"\nFinal result:\n";
    output(a,100);
       system("PAUSE");
       return 0;
    }

    回覆刪除
  33. #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 a[], int count){
     for (int i=0; i<count; i++)
      cout<<a[i]<<'\t';
     cout<<endl;
    }
    void bubbleSort(int a[], int count) {
       for ( int j = count; j >0 ; j-- ) {
          for ( int i = 0; i < j-1; i++ )
            if ( a[i+1] < a[i] )
                swap(a[i],a[i+1]);
              };  
    }
    int main()
    {
        unsigned starttime=clock();
        int a[100];
        srand((unsigned)time(NULL));
        for (int x=0;x<100;x++)
        {
            a[x]=rand()%513;
            cout<<"a["<<x<<"]="<<a[x]<<endl;
        }
        bubbleSort(a, 100); 
     cout<<"\nFinal result:\n";
     output(a,100);
        system("PAUSE");
        return 0;
    }

    回覆刪除
  34. #include <iostream>
    #include <ctime>
    #include <cstdlib>
    using namespace std;
    void swap(int& X,int& Y){int temp=X;X=Y;Y=temp;}
    void output(int a[], int count){
     for (int i=0; i<count; i++)
      cout<<a[i]<<'\t';
     cout<<endl;
    }
    void bubbleSort(int a [], int count) {
         int i,j,temp;
       for ( int j = count; j >0 ; j-- ) {
          for ( int i = 0; i < j-1; i++ )
            if ( a[i+1] < a[i] )
                swap(a[i],a[i+1]);
       };  
    }
    int main(){
        unsigned starttime=clock();
        int a[100];
        srand((unsigned)time(NULL));
        int i =rand();
        cout<<i<<endl;
        for (int x=0;x<100;x++)
        {
            a[x]=rand()%513;
            cout<<"a["<<x<<"]="<<a[x]<<endl;
        }
        bubbleSort(a,100);
        cout<<"\nFinal result:\n";
        output(a,100);                     
        system("pause");
        return 0;
    }

    回覆刪除
  35. #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]);
             };  
    }
    void random100(int data[], int len)
    {
        srand((unsigned)time(NULL));
        for (int x=0;x<len;x++)
        {
            data[x]=rand()%513;
            printf("%d\t",data[x]);
        }
    }
    int main()
    {
        int data[100];
        random100(data,100);
        bubbleSort(data, 100);
        printf("排序後:\n");
        output(data,100);
        system("Pause");
        return 0;  
    }

    回覆刪除
  36. #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]);
             };  
    }
    void random100(int data[], int len)
    {
        srand((unsigned)time(NULL));
        for (int x=0;x<len;x++)
        {
            data[x]=rand()%513;
            printf("%d\t",data[x]);
        }
    }
    int main()
    {
        int data[100];
        random100(data,100);
        bubbleSort(data, 100);
        printf("排序後:\n");
        output(data,100);
        system("Pause");
        return 0;  
    }

    回覆刪除
  37. #include <iostream>
    #include <ctime>
    #include <cstdlib>
    using namespace std;
    void swap(int& X,int& Y){int temp=X;X=Y;Y=temp;}
    void output(int a[], int count){
    for (int i=0; i<count; i++)
    cout<<a[i]<<'\t';
    cout<<endl;
    }
    void bubbleSort(int a [], int count) {
    for ( int j = count; j >0 ; j-- ) {
    for ( int i = 0; i < j-1; i++ )
    if ( a[i+1] < a[i] )
    swap(a[i],a[i+1]);

    };
    }
    void random100(int a[], int len)
    {
    srand((unsigned)time(NULL));
    for (int x=0;x<len;x++)
    {
    a[x]=rand()%513;
    cout<<"a["<<x<<"]="<<a[x]<<endl;
    }
    }
    int main()
    {
    int a[100];
    random100(a,100);
    bubbleSort(a, 100);
    cout<<"\nFinal result:\n" ;
    output(a,100);
    system("Pause");
    return 0;
    }

    回覆刪除
  38. #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]);
             };  
    }
    void random100(int data[], int len)
    {
        srand((unsigned)time(NULL));
        for (int x=0;x<len;x++)
        {
            data[x]=rand()%513;
            printf("%d\t",data[x]);
        }
    }
    int main()
    {
        int data[100];
        random100(data,100);
        bubbleSort(data, 100);
        printf("排序後:\n");
        output(data,100);
        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]);
             };  
    }
    void random100(int data[], int len)
    {
        srand((unsigned)time(NULL));
        for (int x=0;x<len;x++)
        {
            data[x]=rand()%513;
            printf("%d\t",data[x]);
        }
    }
    int main()
    {
        int data[100];
        random100(data,100);
        bubbleSort(data, 100);
        printf("排序後:\n");
        output(data,100);
        system("Pause");
        return 0;  
    }

    回覆刪除
  40. #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]);
             };  
    }
    void random100(int data[], int len)
    {
        srand((unsigned)time(NULL));
        for (int x=0;x<len;x++)
        {
            data[x]=rand()%513;
            printf("%d\t",data[x]);
        }
    }
    int main()
    {
        int data[100];
        random100(data,100);
        bubbleSort(data, 100);
        printf("排序後:\n");
        output(data,100);
        system("Pause");
        return 0;  
    }

    回覆刪除
  41. #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]);
       } 
    }
    void random100(int data[], int len)
    {
        srand((unsigned)time(NULL));
        for (int x=0;x<len;x++)
        {
            data[x]=rand()%513;
         
            printf("%d\t",data[x]);
        }
    }
    int main()
    {
        int data[100];
        random100(data,100);
        bubbleSort(data, 100);
        printf("排序後:\n");
        output(data,100);
        system("Pause");
        return 0;  
    }
    425     320     74      289     305     507     428     483     214     99
    213     235     170     379     11      303     473     116     427     337
    309     268     388     129     492     488     126     10      434     189
    212     416     189     172     393     284     146     131     192     401
    49      366     67      218     324     179     32      192     311     207
    319     177     182     253     383     409     247     408     144     212
    437     465     80      406     416     32      407     46      281     265
    10      208     318     48      478     330     153     324     204     384
    383     407     8       45      428     379     411     501     308     126
    364     503     321     398     131     143     452     305     428     420
    排序後:
    8       10      10      11      32      32      45      46      48      49
    67      74      80      99      116     126     126     129     131     131
    143     144     146     153     170     172     177     179     182     189
    189     192     192     204     207     208     212     212     213     214
    218     235     247     253     265     268     281     284     289     303
    305     305     308     309     311     318     319     320     321     324
    324     330     337     364     366     379     379     383     383     384
    388     393     398     401     406     407     407     408     409     411
    416     416     420     425     427     428     428     428     434     437
    452     465     473     478     483     488     492     501     503     507
    請按任意鍵繼續 . . .

    回覆刪除
  42. #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]);
             };  
    }
    void random100(int data[], int len)
    {
        srand((unsigned)time(NULL));
        for (int x=0;x<len;x++)
        {
            data[x]=rand()%513;
            printf("%d\t",data[x]);
        }
    }
    int main()
    {
        int data[100];
        random100(data,100);
        bubbleSort(data, 100);
        printf("排序後:\n");
        output(data,100);
        system("Pause");
        return 0;  
    }

    回覆刪除
  43. #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]);
             };  
    }
    void random100(int data[], int len)
    {
        srand((unsigned)time(NULL));
        for (int x=0;x<len;x++)
        {
            data[x]=rand()%513;
            printf("%d\t",data[x]);
        }
    }
    int main()
    {
        int data[100];
        random100(data,100);
        bubbleSort(data, 100);
        printf("排序後:\n");
        output(data,100);
        system("Pause");
        return 0;  
    }

    回覆刪除
  44. #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]);
             };  
    }
    void random100(int data[], int len)
    {
        srand((unsigned)time(NULL));
        for (int x=0;x<len;x++)
        {
            data[x]=rand()%513;
            printf("%d\t",data[x]);
        }
    }
    int main()
    {
        int data[100];
        random100(data,100);
        bubbleSort(data, 100);
        printf("排序後:\n");
        output(data,100);
        system("Pause");
        return 0;  
    }

    回覆刪除
  45. #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]);
       };  
    }
    void random100(int data[], int len)
    {
        srand((unsigned)time(NULL));
        cout<<"排序前:"<<endl ;
        for (int x=0;x<len;x++)
        {
            data[x]=rand()%513;
            cout<<data[x]<<"\t";
        }
    }
    int main()
    {
        int data[100];
        random100(data,100);
        bubbleSort(data, 100);
        cout<<"排序後:"<<endl ;
        output(data,100);
        system("Pause");
        return 0;  
    }

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

    回覆刪除
  47. #include <iostream>
    #include <ctime>
    #include <cstdlib>
    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 random100(int data[],int len){
        srand((unsigned)time(NULL));
        printf("排序前:\n");
        for (int x=0;x<len;x++)
        {
            data[x]=rand()%513;
            printf("%d\t",data[x]);   
        }
    }
    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]);
    //     cout<<'\n'<<count-j+1<<" bubble(s) up->"<<endl;
    //    output(data,count);
       };  
    }
    int main()
    {
        int data[100];
        random100(data,100);
        bubbleSort(data,100);
         printf("排序後:\n");
        output(data,100);
        system("pause");
        return 0;
    }

    回覆刪除
  48. #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]);
     //       cout<<'\n'<<count-j+1<<" bubble(s) up->"<<endl;
    //  output(data,count);
       };  
    }
    void random100(int a[], int len)
    {
        srand((unsigned)time(NULL));
        for (int x=0;x<len;x++)
        {
            a[x]=rand()%513;
          cout<<"a["<<x<<"]="<<a[x]<<endl;
        }
    }
     

    int main()
    {
        int a[100];
        random100(a,100);
        bubbleSort (a,100);
        cout<<"nFinal result:\n"<<endl;
        output(a,100);
        system("Pause");
        return 0;  
    }

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

    回覆刪除
  50. #include <iostream>
    #include <ctime>
    #include <cstdlib>
    using namespace std;
    void swap(int& X,int& Y){int temp=X;X=Y;Y=temp;}
    void output(int a[], int count){
     for (int i=0; i<count; i++)
      cout<<a[i]<<'\t';
     cout<<endl;
    }
    void bubbleSort(int a [], int count) {
       for ( int j = count; j >0 ; j-- ) {
          for ( int i = 0; i < j-1; i++ )
            if ( a[i+1] < a[i] )
                swap(a[i],a[i+1]);
    //        cout<<'\n'<<count-j+1<<" bubble(s) up->"<<endl;
    //  output(a,count);
       };  
    }
    void random100(int a[], int len)
    {
        srand((unsigned)time(NULL));
        printf("排序前:\n");
        for (int x=0;x<len;x++)
        {
            a[x]=rand()%513;
           
            printf("%d\t",a[x]);
        }
    }
    int main()
    {
        int a[100];
        random100(a,100);
        bubbleSort(a, 100);
        printf("\n排序前:\n");        
     output(a,100);
        system("Pause");
        return 0;  
    }

    回覆刪除
  51. #include <iostream>
    #include <ctime>
    #include <cstdlib>
    using namespace std;
    void swap(int& X,int& Y){int temp=X;X=Y;Y=temp;}
    void output(int a[], int count){
     for (int i=0; i<count; i++)
      cout<<a[i]<<'\t';
     cout<<endl;
    }
    void bubbleSort(int a [], int count) {
       for ( int j = count; j >0 ; j-- ) {
          for ( int i = 0; i < j-1; i++ )
            if ( a[i+1] < a[i] )
                swap(a[i],a[i+1]);
    //        cout<<'\n'<<count-j+1<<" bubble(s) up->"<<endl;
    //  output(a,count);
       };  
    }
    void random100(int a[], int len)
    {
        srand((unsigned)time(NULL));
        printf("排序前:\n");
        for (int x=0;x<len;x++)
        {
            a[x]=rand()%513;
           
            printf("%d\t",a[x]);
        }
    }
    int main()
    {
        int a[100];
        random100(a,100);
        bubbleSort(a, 100);
        printf("排序前:\n");        
     output(a,100);
        system("Pause");
        return 0;  

    回覆刪除
  52. #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]);
    };
    }
    void random100(int data[], int len)
    {
    srand((unsigned)time(NULL));
    for (int x=0;x<len;x++)
    {
    data[x]=rand()%513;
    printf("%d\t",data[x]);
    }
    }
    int main()
    {
    int data[100];
    random100(data,100);
    bubbleSort(data, 100);
    printf("排序後:\n");
    output(data,100);
    system("Pause");
    return 0;
    }
    268 436 70 85 191 32 419 52 359 35
    307 216 41 16 269 91 382 390 495 238
    183 202 227 288 368 186 362 351 250 506
    232 32 495 123 55 230 179 32 151 358
    102 369 288 496 446 191 489 163 317 113
    401 471 95 121 161 240 161 390 156 94
    486 239 267 56 320 480 344 347 287 15
    120 36 6 67 140 21 177 101 34 251
    254 180 8 300 318 279 165 377 199 337
    443 291 426 101 10 356 355 490 56 373
    排序後:
    6 8 10 15 16 21 32 32 32 34
    35 36 41 52 55 56 56 67 70 85
    91 94 95 101 101 102 113 120 121 123
    140 151 156 161 161 163 165 177 179 180
    183 186 191 191 199 202 216 227 230 232
    238 239 240 250 251 254 267 268 269 279
    287 288 288 291 300 307 317 318 320 337
    344 347 351 355 356 358 359 362 368 369
    373 377 382 390 390 401 419 426 436 443
    446 471 480 486 489 490 495 495 496 506

    請按任意鍵繼續 . . .

    回覆刪除
  53. #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    void random100(int a[],int len)
    {
    srand((unsigned)time(NULL));
    printf("排序前\n");
    for (int x=0;x<len ;x++)
    {
    a[x]=rand()%513;
    printf("%d\t",a[x]);
    }
    }
    void swap(int& X,int& Y){int temp=X;X=Y;Y=temp;}
    void bubbleSort(int a [], int count)
    {
    for ( int j = count; j >0 ; j-- )
    {
    for ( int i = 0; i < j-1; i++ )
    {
    if ( a[i+1] < a[i] )
    swap(a[i],a[i+1]);
    }
    }
    }

    int main()
    {
    int a[101];
    random100(a,101);
    bubbleSort(a,101);
    printf("排序後 %d\t",a[x]);
    system ("pause");
    return 0;
    }

    回覆刪除
  54. #include <iostream>
    #include <ctime>
    #include <cstdlib>
    using namespace std;
    void swap(int& X,int& Y){int temp=X;X=Y;Y=temp;}
    void output(int a[], int count){
    for (int i=0; i<count; i++)
    cout<<a[i]<<'\t';
    cout<<endl;
    }
    void bubbleSort(int a [], int count) {
    for ( int j = count; j >0 ; j-- ) {
    for ( int i = 0; i < j-1; i++ )
    if ( a[i+1] < a[i] )
    swap(a[i],a[i+1]);

    };
    }
    void random100(int a[], int len)
    {
    srand((unsigned)time(NULL));
    for (int x=0;x<len;x++)
    {
    a[x]=rand()%512+1;
    cout<<"a["<<x<<"]="<<a[x]<<endl;
    }
    }
    int main()
    {
    int a[100];
    random100(a,100);
    bubbleSort(a, 100);
    cout<<"\nFinal result:\n" ;
    output(a,100);
    system("Pause");
    return 0;
    }

    回覆刪除
  55. #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    void random100(int a[],int len)
    {
    srand((unsigned)time(NULL));
    printf("排序前\n");
    for (int x=0;x<len ;x++)
    {
    a[x]=rand()%513;
    printf("%d\t",a[x]);
    }
    }
    void swap(int& X,int& Y){int temp=X;X=Y;Y=temp;}
    void bubbleSort(int a [], int count)
    {
    for ( int j = count; j >0 ; j-- )
    {
    for ( int i = 0; i < j-1; i++ )
    {
    if ( a[i+1] < a[i] )
    swap(a[i],a[i+1]);
    }
    }
    }

    int main()
    {
    int a[101];
    random100(a,101);
    bubbleSort(a,101);
    printf("排序後 %d\t",a[x]);
    system ("pause");
    return 0;
    }

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

    回覆刪除
  57. #include <iostream>
    #include <ctime>
    #include <cstdlib>
    using namespace std;
    void swap(int& X,int& Y){int temp=X;X=Y;Y=temp;}
    void output(int a[], int count){
     for (int i=0; i<count; i++)
      cout<<a[i]<<'\t';
     cout<<endl;
    }
    void bubbleSort(int a [], int count) {
         int i,j,temp;
       for ( int j = count; j >0 ; j-- ) {
          for ( int i = 0; i < j-1; i++ )
            if ( a[i+1] < a[i] )
                swap(a[i],a[i+1]);
       };  
    }
    int main(){
        unsigned starttime=clock();
        int a[100];
        srand((unsigned)time(NULL));
        int i =rand();
        cout<<i<<endl;
        for (int x=0;x<100;x++)
        {
            a[x]=rand()%513;
            cout<<"a["<<x<<"]="<<a[x]<<endl;
        }
        bubbleSort(a,100);
        cout<<"\nFinal result:\n";
        output(a,100);                     
        system("pause");
        return 0;
    }

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

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

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

    回覆刪除
  61. #include <iostream>
    #include <ctime>
    #include <cstdlib>
    using namespace std;
    void swap(int& X,int& Y){int temp=X;X=Y;Y=temp;}
    void output(int a[], int count){
    for (int i=0; i<count; i++)
    cout<<a[i]<<'\t';
    cout<<endl;
    }
    void bubbleSort(int a [], int count) {
    int i,j,temp;
    for ( int j = count; j >0 ; j-- ) {
    for ( int i = 0; i < j-1; i++ )
    if ( a[i+1] < a[i] )
    swap(a[i],a[i+1]);

    };
    }
    int main(){
    unsigned starttime=clock();
    int a[100];
    srand((unsigned)time(NULL));
    int i =rand();
    cout<<i<<endl;
    for (int x=0;x<100;x++)
    {
    a[x]=rand()%513;
    cout<<"a["<<x<<"]="<<a[x]<<endl;
    }
    bubbleSort(a,100);
    cout<<"\nFinal result:\n";
    output(a,100);
    system("pause");
    return 0;
    }

    回覆刪除
  62. #include <iostream>
    #include <ctime>
    #include <cstdlib>
    using namespace std;
    void swap(int& X,int& Y){int temp=X;X=Y;Y=temp;}
    void output(int a[], int count){
    for (int i=0; i<count; i++)
    cout<<a[i]<<'\t';
    cout<<endl;
    }
    void bubbleSort(int a [], int count) {
    int i,j,temp;
    for ( int j = count; j >0 ; j-- ) {
    for ( int i = 0; i < j-1; i++ )
    if ( a[i+1] < a[i] )
    swap(a[i],a[i+1]);
    };
    }
    int main(){
    unsigned starttime=clock();
    int a[100];
    srand((unsigned)time(NULL));
    int i =rand();
    cout<<i<<endl;
    for (int x=0;x<100;x++)
    {
    a[x]=rand()%513;
    cout<<"a["<<x<<"]="<<a[x]<<endl;
    }
    bubbleSort(a,100);
    cout<<"\nFinal result:\n";
    output(a,100);
    system("pause");
    return 0;
    }

    回覆刪除
  63. #include <iostream>
    #include <ctime>
    #include <cstdlib>
    using namespace std;
    void swap(int& X,int& Y){int temp=X;X=Y;Y=temp;}
    void output(int a[], int count){
    for (int i=0; i<count; i++)
    cout<<a[i]<<'\t';
    cout<<endl;
    }
    void bubbleSort(int a [], int count) {
    for ( int j = count; j >0 ; j-- ) {
    for ( int i = 0; i < j-1; i++ )
    if ( a[i+1] < a[i] )
    swap(a[i],a[i+1]);

    };
    }
    void random100(int a[], int len)
    {
    srand((unsigned)time(NULL));
    for (int x=0;x<len;x++)
    {
    a[x]=rand()%513;
    cout<<"a["<<x<<"]="<<a[x]<<endl;
    }
    }
    int main()
    {
    int a[100];
    random100(a,100);
    bubbleSort(a, 100);
    cout<<"\nFinal result:\n" ;
    output(a,100);
    system("Pause");
    return 0;
    }

    回覆刪除
  64. #include <iostream>
    #include <ctime>
    #include <cstdlib>
    using namespace std;
    void swap(int& X,int& Y){int temp=X;X=Y;Y=temp;}
    void output(int a[], int count){
    for (int i=0; i<count; i++)
    cout<<a[i]<<'\t';
    cout<<endl;
    }
    void bubbleSort(int a [], int count) {
    for ( int j = count; j >0 ; j-- ) {
    for ( int i = 0; i < j-1; i++ )
    if ( a[i+1] < a[i] )
    swap(a[i],a[i+1]);

    };
    }
    void random100(int a[], int len)
    {
    srand((unsigned)time(NULL));
    for (int x=0;x<len;x++)
    {
    a[x]=rand()%513;
    cout<<"a["<<x<<"]="<<a[x]<<endl;
    }
    }
    int main()
    {
    int a[100];
    random100(a,100);
    bubbleSort(a, 100);
    cout<<"\nFinal result:\n" ;
    output(a,100);
    system("Pause");
    return 0;
    }

    回覆刪除
  65. #include <iostream>
    #include <ctime>
    #include <cstdlib>
    using namespace std;
    void swap(int& X,int& Y){int temp=X;X=Y;Y=temp;}
    void output(int a[], int count){
    for (int i=0; i<count; i++)
    cout<<a[i]<<'\t';
    cout<<endl;
    }
    void bubbleSort(int a [], int count) {
    for ( int j = count; j >0 ; j-- ) {
    for ( int i = 0; i < j-1; i++ )
    if ( a[i+1] < a[i] )
    swap(a[i],a[i+1]);

    };
    }
    void random100(int a[], int len)
    {
    srand((unsigned)time(NULL));
    for (int x=0;x<len;x++)
    {
    a[x]=rand()%513;
    cout<<"a["<<x<<"]="<<a[x]<<endl;
    }
    }
    int main()
    {
    int a[100];
    random100(a,100);
    bubbleSort(a, 100);
    cout<<"\nFinal result:\n" ;
    output(a,100);
    system("Pause");
    return 0;
    }

    回覆刪除
  66. #include <iostream>
    #include <ctime>
    #include <cstdlib>
    using namespace std;
    void swap(int& X,int& Y){int temp=X;X=Y;Y=temp;}
    void output(int a[], int count){
    for (int i=0; i<count; i++)
    cout<<a[i]<<'\t';
    cout<<endl;
    }
    void bubbleSort(int a [], int count) {
    for ( int j = count; j >0 ; j-- ) {
    for ( int i = 0; i < j-1; i++ )
    if ( a[i+1] < a[i] )
    swap(a[i],a[i+1]);

    };
    }
    void random100(int a[], int len)
    {
    srand((unsigned)time(NULL));
    for (int x=0;x<len;x++)
    {
    a[x]=rand()%513;
    cout<<"a["<<x<<"]="<<a[x]<<endl;
    }
    }
    int main()
    {
    int a[100];
    random100(a,100);
    bubbleSort(a, 100);
    cout<<"\nFinal result:\n" ;
    output(a,100);
    system("Pause");
    return 0;
    }

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

    回覆刪除
  68. #include <iostream>
    #include <ctime>
    #include <cstdlib>
    using namespace std;
    void swap(int& X,int& Y){int temp=X;X=Y;Y=temp;}
    void output(int a[], int count){
    for (int i=0; i<count; i++)
    cout<<a[i]<<'\t';
    cout<<endl;
    }
    void bubbleSort(int a [], int count) {
    for ( int j = count; j >0 ; j-- ) {
    for ( int i = 0; i < j-1; i++ )
    if ( a[i+1] < a[i] )
    swap(a[i],a[i+1]);

    };
    }
    void random100(int a[], int len)
    {
    srand((unsigned)time(NULL));
    for (int x=0;x<len;x++)
    {
    a[x]=rand()%513;
    cout<<"a["<<x<<"]="<<a[x]<<endl;
    }
    }
    int main()
    {
    int a[100];
    random100(a,100);
    bubbleSort(a, 100);
    cout<<"\nFinal result:\n" ;
    output(a,100);
    system("Pause");
    return 0;
    }

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

    回覆刪除
  70. #include <iostream>
    #include <ctime>
    #include <cstdlib>
    using namespace std;
    void swap(int& X,int& Y){int temp=X;X=Y;Y=temp;}
    void output(int a[], int count){
    for (int i=0; i<count; i++)
    cout<<a[i]<<'\t';
    cout<<endl;
    }
    void bubbleSort(int a [], int count) {
    for ( int j = count; j >0 ; j-- ) {
    for ( int i = 0; i < j-1; i++ )
    if ( a[i+1] < a[i] )
    swap(a[i],a[i+1]);

    };
    }
    void random100(int a[], int len)
    {
    srand((unsigned)time(NULL));
    for (int x=0;x<len;x++)
    {
    a[x]=rand()%513;
    cout<<"a["<<x<<"]="<<a[x]<<endl;
    }
    }
    int main()
    {
    int a[100];
    random100(a,100);
    bubbleSort(a, 100);
    cout<<"\nFinal result:\n" ;
    output(a,100);
    system("Pause");
    return 0;
    }

    回覆刪除
  71. #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 a[], int count){
     for (int i=0; i<count; i++)
      cout<<a[i]<<'\t';
     cout<<endl;
    }
    void bubbleSort(int a [], int count) {
       for ( int j = count; j >0 ; j-- ) {
          for ( int i = 0; i < j-1; i++ )
            if ( a[i+1] < a[i] )
                swap(a[i],a[i+1]);
       };  
    }
    int main() {
        unsigned starttime=clock();
        int a[100];
        srand((unsigned)time(NULL));
        int i =rand();
        cout<<i<<endl;
        for (int x=0;x<101;x++)
        {
            a[x]=rand()%513;
            cout<<"a["<<x<<"]="<<a[x]<<endl;
        }
        bubbleSort(a, 100);        
     cout<<"\nFinal result:\n";
     output(a,100);
        unsigned endtime=clock();
        cout<<"CPU time:"
            <<(double)(endtime-starttime)/CLK_TCK
            <<"sec"<<endl;
        system("PAUSE");
        return 0;
    }

    回覆刪除
  72. #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 a[], int count){
     for (int i=0; i<count; i++)
      cout<<a[i]<<'\t';
     cout<<endl;
    }
    void bubbleSort(int a [], int count) {
       for ( int j = count; j >0 ; j-- ) {
          for ( int i = 0; i < j-1; i++ )
            if ( a[i+1] < a[i] )
                swap(a[i],a[i+1]);
       };  
    }
    int main() {
        unsigned starttime=clock();
        int a[100];
        srand((unsigned)time(NULL));
        int i =rand();
        cout<<i<<endl;
        for (int x=0;x<101;x++)
        {
            a[x]=rand()%513;
        }
        bubbleSort(a, 100);        
     cout<<"\nFinal result:\n";
     output(a,100);
        unsigned endtime=clock();
        cout<<"CPU time:"
            <<(double)(endtime-starttime)/CLK_TCK
            <<"sec"<<endl;
        system("PAUSE");
        return 0;
    }

    回覆刪除

HTML 編輯器