数组基本算法

来源:互联网 发布:关于网络诈骗ppt 编辑:程序博客网 时间:2024/04/29 07:07

一维数组算法:

1. 数组的遍历算法

#define NUM 10

   int main(){

      int array[NUM];

      for (int i; i < NUM; i ++){

          cout << "Please input NO."<<i<<" number of the array."<< endl;

          cin >> array[i] ;

          cout << endl;

       }

     cout << endl;

     for (int i; i < NUM; i ++){

           cout << array[i] << endl ;

      }  

    return 0;

   }

2. 求数组最大/最小值算法

#define NUM 10

     void  main(){

        int max = 0;

        int min = 0;

        int array[NUM];

        for (int i = 0; i < NUM; i ++){

            cout << "The NO. "<< array[i]<<" number of the array is "<<endl;

            cin >> array[i];

            cout << endl;

        }

        for (int i = 0; i < NUM; i ++){

            if (array[i] > max){

                     max = array[i];

             }

           if (array[i] < min){

                     min = array[i];

            }

        }

        cout << "the max value of the array is  "<<max<<endl;

        cout << "the min value of the array is  "<<min<<endl;

     }

3. 数组排序算法

    选择排序算法:

    将array[0]与后面每一个数字进行比较,选出最小的存入array[0]; 将array[1] 与后面每个数字进行比较,选出最小的存入array[1]...以此类推.

 #define NUM 10

    void sort_direct(int array[], int size){

        for (int i = 0; i < size - 1; i ++){

             for (int j = i + 1; j < size ; j ++){

                       if (array[j] < array[i]){

                          t = array[i];

                          array[i] = array[j];

                         array[j] = t;

                  }

             }

        }

    return array[];

   }

  void main(){

       int array[NUM];

       int a[NUM];

       for (int i = 0; i < NUM; i ++){

           cout << "Please input the NO. "<<array[i]<<" number of the array."<< endl;

           cin >> array[i];

           cout << endl;

      }

      sort_direct(array, NUM);

  }

    冒泡排序算法:

 每一个元素和它之后的一个元素比较,如果较大,则交换。一趟比较下来,最大的元素沉底。

#define NUM 10

  int[] sort_bubble(int array[], int size){

      for (int i = 0; i < size - 1; i ++){

           for (int j = 0; j < size  - 1 - i; j ++){

                          if (array[j] > array[j + 1]){

                                   int t = array[j];

                                   array[j] = array[j + 1];

                                   array[j + 1] = t;

                         }

              }               

     }

     return array[];

  }

 int main(){

     int array[NUM];

     int a[NUM];

       for (int i = 0; i < NUM; i ++){

           cout << "Please input the NO. "<<array[i]<<" number of the array."<< endl;

           cin >> array[i];

           cout << endl;

      }

      sort_bubble(array, NUM);

}

4. 数组元素逆序存放算法

#define NUM 10

  void main(){

       int array[NUM];

       for (int i = 0; i < NUM; i ++){

           cout << "Please input the NO. "<<array[i]<<" number of the array."<< endl;

           cin >> array[i];

           cout << endl;

      }

    for (int i = 0; i < NUM/2; i ++){

                      t = array[i];

                     array[i] = array[NUM-i];

                     array[NUM-i] = t;     

     }

      for (int i = 0; i < NUM; i ++){

            cout<<array[i]<<" ";

      }

      cout << endl;


  }

5. 数组的插入算法 (插入已经排好序的数组中)

   void main(){

        int array[5] = {2,4,6,7,9};

        int number;

       int i, j;

        cout << "Please input the number you want to insert: " << endl;

        cin >> number;

        for ( i = 0; i < 5; i ++){

            if (number < array[i]) break;

        }

        for ( j = 5; j <= i + 1; j -- ){

             array[j] = array[j - 1];

       }

       array[i] = number;

        for (i = 0; i < 11; i ++){

             cout << array[i];

       }

  }

6. 数组的查找算法

   折半查找

#define NUM 10

 int search(int array[NUM], int number){

    int left = 0;

    int right = NUM - 1;

    int mid;

    while (array[left] <= array[right]){

         mid = (right + left)/2;

          if (number = array[mid]) {

                  return mid;

                  break;

          }

          if (number < array[mid]){

                 right = mid - 1;

          }

         if (number > array[mid]){

                  left = mid + 1;

          }

    }

}

二维数组算法:

1. 二维数组的遍历算法

 void main(){

      int a[2][3];

      for (int i = 0; i < 2; i ++){

          for (int j = 0; j < 3; j ++){

             cout << "Please input the number of the array"<< endl;

             cin >> a[i][j];

             cout << endl;

         }

     }

   for (int i = 0; i < 2; i ++){

           for (int j = 0; j < 3; j ++){

                cout <<"the numbers in the array are " << endl;

                cout << a[i][j] << endl;

         }

    }

 }

2. 求二维数组的最大/最小值算法

   int main(){

          int a[2][3] = {{3,5,1},{2,4,7}};

          int max = 0;

          int min = 0;

          for (int i = 0; i < 2; i ++){

               for (int j = 0; j < 3; j ++){

                     if (max < a[i][j] ) max = a[i][j];

                     if (min > a[i][j]) min = a[i][j];

              }

          }

      return max, min;

   }

3 转置矩阵

    void main(){

         int a[2][3] = {{3,5,1},{2,4,7}};

         int b[3][2];

         for (int i = 0; i < 2; i ++){

             for (int j = 0; j < 3; j ++){

                        b[j][i]=a[i][j]

            }

        }

        for (int i = 0; i < 3; i ++){

            for (int j = 0; j < 2; j ++){

                   cout << b[i][j];

           }

        }

   }

4 方阵对角线元素之和

 int main(){

        int a[3][3]={{3,5,1},{2,4,7},{1,4,9}};

         int sum = 0;

         for (int i = 0; i < 3; i ++){

              for (int j = 0; j < 3; j ++){

                       if (i = j)

                       sum += a[i][i];

             }

    }

     return sum;   

  }

5 方阵对角线以上/以下元素之和

int main(){

     int a[3][3]={{3,5,1},{2,4,7},{1,4,9}};

     int sum = 0;

     for (int i = 0; i < 3; i ++){

          for (int j = 0; j < 3; j ++){

                if (i<=j) sum += a[i][j];

           }

     }

    return sum;

}

原创粉丝点击