chimier-c++-day06

選擇排序(Selection-Sort)

先把最小的拿出來
剩下的, 再把最小的拿出來
剩下的, 再把最小的拿出來
。。。
每次選擇還沒處理的元素里最小的元素

選擇排序就是重復(fù)“從待排序的數(shù)據(jù)中尋找最小值,將其與序列最左邊的數(shù)字進(jìn)行交換”這一操作的算法。在序列中尋找最小值時(shí)使用的是線性查找。

比如: 對數(shù)字1~9進(jìn)行排序。 使用線性查找在數(shù)據(jù)中尋找最小值,于是我們找到了最小值 1





  • 一層版
#include <iostream>
using namespace std;
int main(){
    int arr[] = {6, 1, 7, 8, 9, 3, 5, 4, 2};
    int  len = sizeof(arr)/sizeof(arr[0]);
    int minIndex = 0;
    for (int j = 0; j < len; ++j) {
        if (arr[j] < arr[minIndex])
            minIndex = j;
    }
    cout<<"minIndex = "<< minIndex <<endl;
    // 交換
    int temp = arr[0];
    arr[0] = arr[minIndex];
    arr[minIndex] = temp;
    for (int i = 0; i < len; ++i)
        cout<<arr[i]<<"\t";
    cout<<endl;
    cout<<"-------------------------------"<<endl;
    cout<<"-------------------------------"<<endl;
    minIndex = 1;
    for (int j = 1; j < len; ++j) {
        if (arr[j] < arr[minIndex])
            minIndex = j;
    }
    cout<<"minIndex = "<< minIndex <<endl;
    // 交換
    temp = arr[1];
    arr[1] = arr[minIndex];
    arr[minIndex] = temp;
    for (int i = 0; i < len; ++i)
        cout<<arr[i]<<"\t";
    cout<<endl;
    cout<<"-------------------------------"<<endl;
    cout<<"-------------------------------"<<endl;

    minIndex = 2;
    for (int j = 2; j < len; ++j) {
        if (arr[j] < arr[minIndex])
            minIndex = j;
    }
    cout<<"minIndex = "<< minIndex <<endl;
    // 交換
    temp = arr[2];
    arr[2] = arr[minIndex];
    arr[minIndex] = temp;
    for (int i = 0; i < len; ++i)
        cout<<arr[i]<<"\t";
    cout<<endl;
    cout<<"-------------------------------"<<endl;
    cout<<"-------------------------------"<<endl;

}
在這里插入圖片描述

最終版

#include <iostream>
using namespace std;

int main(){
    int arr[] = { 6, 1, 7, 8, 9, 3, 5, 4, 2};
    int len = sizeof(arr)/sizeof(arr[0]);

    for (int i = 0; i < len - 1; ++i) {
        int minIndex = i;
        for (int j = i; j < len; ++j) {
            if(arr[j] < arr[minIndex])
                minIndex = j;
        }
        // 小優(yōu)化
        if (minIndex != i){
            int temp = arr[i];
            arr[i] = arr[minIndex];
            arr[minIndex] = temp;
        }
    }

    for (int i = 0; i < len; ++i) {
        cout << arr[i] <<"\t";

    }
    cout <<endl;


}

在這里插入圖片描述

二維數(shù)組


int main(){
    const int M= 3, N=4;
    int arr[M][N] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};
    // 遍歷 二維矩陣
    for (int i = 0; i < M; ++i) {
        for(int j = 0; j < N; j++){
            cout << arr[i][j] <<"\t";
        }
        cout <<endl;
    }
}

二維數(shù)組的定義方式

1. 數(shù)據(jù)類型 數(shù)組名[行數(shù)][列數(shù)];
2. 數(shù)據(jù)類型 數(shù)組名[行數(shù)][列數(shù)] = {{數(shù)據(jù)1, 數(shù)據(jù)2...}, ..... {數(shù)據(jù)n, 數(shù)據(jù)n+1}....};
3. 數(shù)據(jù)類型 數(shù)組名[行數(shù)][列數(shù)] = {數(shù)據(jù)1, 數(shù)據(jù)2, ......};
4. 數(shù)據(jù)類型 數(shù)組名[][列數(shù)] = {數(shù)據(jù)1, 數(shù)據(jù)2, ......};

可以省略行, 不可以省略列


#include <iostream>

using namespace std;

int main() {
//    1. 數(shù)據(jù)類型 數(shù)組名[行數(shù)][列數(shù)];
    int arr[2][3];
    // 沒有賦值時(shí), 數(shù)組中的變量是不確定的
    for (int i = 0; i < 2; ++i) {
        for (int j = 0; j < 3; ++j) {
            cout << arr[i][j] << "\t"; //第 i 行 第 j 列
        }
        cout << endl;
    }
    arr[0][0] = 1;
    arr[0][1] = 2;
    arr[0][2] = 3;
    arr[1][0] = 4;
    arr[1][1] = 5;
    arr[1][2] = 6;
    for (int i = 0; i < 2; ++i) {
        for (int j = 0; j < 3; ++j) {
            cout << arr[i][j] << "\t"; //第 i 行 第 j 列
        }
        cout << endl;
    }
//    2. 數(shù)據(jù)類型 數(shù)組名[行數(shù)][列數(shù)] = {{數(shù)據(jù)1, 數(shù)據(jù)2...}, ..... {數(shù)據(jù)n, 數(shù)據(jù)n+1}....};
    cout << "====================================" << endl;
    int arr2[2][3] = {{1, 2, 3},
                      {4, 5, 6}};
    for (int i = 0; i < 2; ++i) {
        for (int j = 0; j < 3; ++j) {
            cout << arr2[i][j] << "\t"; //第 i 行 第 j 列
        }
        cout << endl;
    }
//    3. 數(shù)據(jù)類型 數(shù)組名[行數(shù)][列數(shù)] = {數(shù)據(jù)1, 數(shù)據(jù)2, ......};
    cout << "====================================" << endl;

    int arr3[2][3] = {1, 2, 3, 4, 5, 6};
    for (int i = 0; i < 2; ++i) {
        for (int j = 0; j < 3; ++j) {
            cout << arr3[i][j] << "\t"; //第 i 行 第 j 列
        }
        cout << endl;
    }
//    4. 數(shù)據(jù)類型 數(shù)組名[][列數(shù)] = {數(shù)據(jù)1, 數(shù)據(jù)2, ......};
    cout << "====================================" << endl;

    int arr4[][3] = {1, 2, 3, 4, 5, 6};
    for (int i = 0; i < 2; ++i) {
        for (int j = 0; j < 3; ++j) {
            cout << arr4[i][j] << "\t"; //第 i 行 第 j 列
        }
        cout << endl;
    }
}

?維數(shù)組數(shù)組名

  • 查看?維數(shù)組所占內(nèi)存空間
  • 獲取?維數(shù)組?地址
#include <iostream>
using namespace std;

int main(){
    const int M= 3, N=4;
    int arr[M][N] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};
    cout <<"arr total size = "<<sizeof(arr)<<endl;
    cout <<"arr include elements = "<<sizeof(arr)/sizeof(arr[0][0])<<endl;
    cout <<"row total size = "<<sizeof(arr[0])<<endl;
    cout <<"row total size = "<<sizeof(arr[1])<<endl;
    cout <<"row include elements = "<<sizeof(arr[1])/sizeof(arr[0][0])<<endl;

    // 數(shù)組名存儲的是首地址
    cout <<"arr = "<<arr<<endl;  // 0x7ffee103e910
    cout <<"arr = "<<&arr[0][0]<<endl;  // 0x7ffee103e910  &變量名  是取出變量的地址
    cout <<"arr = "<<&arr[0][1]<<endl;  // 0x7ffee103e910
    cout <<"arr = "<<&arr[0][2]<<endl;  // 0x7ffeeaecd914
    cout <<"arr = "<<&arr[0][3]<<endl;  // 0x7ffeeaecd918
    cout <<"arr = "<<&arr[1][0]<<endl;  // 0x7ffeeaecd91c
    cout <<"arr = "<<&arr[1][1]<<endl;  // 0x7ffeeaecd920
    cout <<"arr = "<<&arr[1][2]<<endl;  // 0x7ffeeaecd924
    cout <<"arr = "<<&arr[1][3]<<endl;  // 0x7ffeeaecd928
}

image
int main(){
    // 計(jì)算三名同學(xué)的總成績
    int scores[][3] = {{100, 100, 100}, {90, 30, 100}, {60, 70, 80}};
    string names[] = {"zhangsan", "lisi", "wangwu"};
    for(int i = 0; i< 3;  i++){
        int  sum = 0;
        for(int j = 0; j < 3 ; j++){
            sum += scores[i][j];
        }
        cout <<names[i]<<" total scores  =  "<<sum<<endl;
        cout <<names[i]<<" mean scores  =  "<<sum/3.0<<endl;
    }
}

編寫程序?qū)崿F(xiàn)矩陣的轉(zhuǎn)置操作(行變列, 列變行)

#include <iostream>
using namespace std;

int main(){
    int X[5][4]={{1, 2, 3, 4}, { 5, 6, 7, 8,}, {9, 10, 11, 12}, {13, 14, 15, 16}, {17, 18, 19, 20}};
    int XT[4][5];
    cout<< "T before ------------------------------" <<endl;
    for (int i = 0; i < 5 ; ++i) {
        for (int j = 0; j < 4; ++j) {
            cout<<X[i][j]<<"\t";
        }
        cout<<endl;
    }
    // 轉(zhuǎn)置
    for (int i = 0; i < 5 ; ++i) {
        for (int j = 0; j < 4; ++j) {
            // 核心  行變列, 列變行
           XT[j][i]  = X[i][j];
        }
    }

    cout<< "T after ------------------------------" <<endl;
    for (int i = 0; i < 4 ; ++i) {
        for (int j = 0; j < 5; ++j) {
            cout<<XT[i][j]<<"\t";
        }
        cout<<endl;
    }
}

作業(yè)

統(tǒng)計(jì)數(shù)組a和b對應(yīng)位置元素相等的個(gè)數(shù)

int a[3][4]={1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12},
int  b[3][4]={12, 2, 3, 4, 10, 6, 7, 11, 9, 5, 8, 1};
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容