பாடம் 8 / 8 · Functions-ம் Arrays-ம் · படிக்க 4 min · புதுப்பிக்கப்பட்டது

Multi-dimensional arrays: sorting, searching

மூணு அல்லது அதுக்கு மேல dimensions, table-ல ஒரு value-ஐ கண்டுபிடிக்கிறது, rows-ஐயோ முழு table-ஐயோ sort பண்றது.

C-ல எத்தனை dimensions வேணும்னாலும் வைக்கலாம். int sales[2][3][4]-ல 2 வருஷம் × 3 கடைகள் × 4 quarters = 24 values வைக்கலாம்; sales[1][0][3]-ன்னா year 1, shop 0, quarter 3. ஒவ்வொரு கூடுதல் dimension-ம் இன்னொரு index-ஐயும் இன்னொரு nested loop-ஐயும் சேர்க்கும், அவ்வளவு தான்.

c
#include <stdio.h>

int main(void)
{
    int sales[2][3][4] = {0};          /* all 24 values start at 0 */
    sales[1][0][3] = 950;

    int count = sizeof(sales) / sizeof(sales[0][0][0]);
    printf("%d values, sales[1][0][3] = %d\n", count, sales[1][0][3]);
    return 0;
}
Output
24 values, sales[1][0][3] = 950

2D array-ல தேடுறது

c
#include <stdio.h>

int main(void)
{
    int a[3][4] = {{15, 8, 23, 4}, {42, 16, 9, 31}, {7, 12, 27, 3}};
    int key = 27, found = 0;

    for (int r = 0; r < 3 && !found; r++) {
        for (int c = 0; c < 4; c++) {
            if (a[r][c] == key) {
                printf("%d found at [%d][%d]\n", key, r, c);
                found = 1;
                break;
            }
        }
    }
    if (!found) {
        printf("%d not found\n", key);
    }
    return 0;
}
Output
27 found at [2][2]

found flag ரெண்டு வேலை செய்யுது: break உள் loop-ஐ விட்டு வெளிய வரும், வெளி test-ல இருக்கிற && !found வெளி loop-ஐயும் நிறுத்தும். Loops முடிஞ்ச பிறகு, "not found" print பண்ணணுமான்னு flag சொல்லும்.

ஒவ்வொரு row-ஐயும், அப்புறம் முழு table-ஐயும் sort பண்றது

c
#include <stdio.h>

#define ROWS 3
#define COLS 4

void print(int a[ROWS][COLS])
{
    for (int r = 0; r < ROWS; r++) {
        for (int c = 0; c < COLS; c++) {
            printf("%4d", a[r][c]);
        }
        printf("\n");
    }
}

int main(void)
{
    int a[ROWS][COLS] = {{15, 8, 23, 4}, {42, 16, 9, 31}, {7, 12, 27, 3}};

    /* 1. Bubble-sort every row on its own */
    for (int r = 0; r < ROWS; r++) {
        for (int i = 0; i < COLS - 1; i++) {
            for (int j = 0; j < COLS - 1 - i; j++) {
                if (a[r][j] > a[r][j + 1]) {
                    int t = a[r][j];
                    a[r][j] = a[r][j + 1];
                    a[r][j + 1] = t;
                }
            }
        }
    }
    printf("Each row sorted:\n");
    print(a);

    /* 2. Sort the whole table: number the cells 0..11 row by row */
    int n = ROWS * COLS;
    for (int i = 0; i < n - 1; i++) {
        for (int j = 0; j < n - 1 - i; j++) {
            int r1 = j / COLS, c1 = j % COLS;
            int r2 = (j + 1) / COLS, c2 = (j + 1) % COLS;
            if (a[r1][c1] > a[r2][c2]) {
                int t = a[r1][c1];
                a[r1][c1] = a[r2][c2];
                a[r2][c2] = t;
            }
        }
    }
    printf("Whole table sorted:\n");
    print(a);
    return 0;
}
Output
Each row sorted:
   4   8  15  23
   9  16  31  42
   3   7  12  27
Whole table sorted:
   3   4   7   8
   9  12  15  16
  23  27  31  42

முழு table-ஐ sort பண்ற ரகசியம்: ஒவ்வொரு cell-க்கும் row row-ஆ 0 முதல் 11 வரைக்கும் ஒரு single number k கொடுங்க. Cell k row k / COLS, column k % COLS-ல இருக்கும். அப்புறம் சாதாரண ஒரு-dimensional bubble sort table மேல வேலை செய்யும்.

Note

#define ROWS 3 அளவுக்கு ஒரு பேரு கொடுக்குது. ஒரே இடத்துல மாத்தினா எல்லா loop-ம் declaration-ம் அதைப் பின்பற்றும் — பத்து இடத்துல 3 type பண்றதை விட ரொம்ப பாதுகாப்பானது.

4 columns இருக்கிற table-ல, cell number k = 9 (0-ல இருந்து row row-ஆ எண்ணினா) எந்த row, column-ல இருக்கும்?

இப்போ இதை செஞ்சு பாருங்க

  1. 3 × 4 table-ல பெரிய value-ஐ கண்டுபிடிச்சு, அதோட row, column-ஐ print பண்ணுங்க.
  2. ஒவ்வொரு row-க்கு பதிலா ஒவ்வொரு column-ஐயும் sort பண்ணுங்க.
  3. முழு table-ஐ sort பண்ண பிறகு, ஒவ்வொரு cell-ஐயும் எட்ட k / COLS, k % COLS use பண்ணி binary search வெச்சு தேடுங்க.

Create a free account to save your progress, or sign in.