C Programming Language/C

C์–ธ์–ด ์ •๋ ฌ๊ณผ ํ–‰๋ ฌ์— ๋Œ€ํ•ด ํ•™์Šตํ•˜๊ธฐ

567Rabbit 2024. 7. 1. 21:45

 

์„ ํƒ ์ •๋ ฌ(selection sort)

 

: ์ •๋ ฌ์ด ์•ˆ๋œ ์ˆซ์ž๋“ค ์ค‘์—์„œ ์ตœ์†Œ ๊ฐ’์„ ์„ ํƒํ•˜์—ฌ ๋ฐฐ์—ด์˜ ์ฒซ๋ฒˆ์งธ ์š”์†Œ์™€ ๊ตํ™˜ํ•œ๋‹ค.

 

5, 3, 8, 1, 2, 7 ์ด๋ผ๋Š” ์ˆซ์ž๋ฅผ ์ •๋ ฌํ•ด๋ณธ๋‹ค๋ฉด?

-> ๊ฒฐ๊ณผ์ ์œผ๋กœ 1, 2, 3, 5, 7, 8 ์œผ๋กœ ์ •๋ ฌ์ด ๋˜์—ˆ๋‹ค.

 

 

 

๋‚ด๋ถ€ for ๋ฃจํ”„๋กœ์„œ, (i+1)๋ฒˆ์งธ ์›์†Œ๋ถ€ํ„ฐ ๋ฐฐ์—ด์˜ ๋งˆ์ง€๋ง‰ ์›์†Œ ์ค‘์—์„œ ์ตœ์†Œ๊ฐ’์„ ์ฐพ๋Š”๋‹ค.

ํ˜„์žฌ์˜ ์ตœ์†Œ๊ฐ’๊ณผ ๋น„๊ตํ•˜์—ฌ ๋” ์ž‘์€ ์ •์ˆ˜๊ฐ€ ๋ฐœ๊ฒฌ๋˜๋ฉด ๊ทธ ์ •์ˆ˜๊ฐ€ ๋“ค์–ด์žˆ๋Š” ์ธ๋ฑ์Šค๋ฅผ least์— ์ €์žฅํ•œ๋‹ค.

#include <stdio.h>
#define SIZE 10

int main(void)
{
    int list[SIZE] = { 3, 2, 9, 7, 1 ; 4, 8, 0, 6, 5};
    int i, j, temp, least;
    
    for(i = 0 ; i < SIZE-1 ; i++)
    {
    	least = i;
        for(j = i + 1 ; j < SIZE ; j++)
            if(list[j] < list[least])
                least = j;

        temp = list[i];
        list[i] = list[least]
        list[least] = temp;
    }
}

 

 

 

- ๋ณ€์ˆ˜์˜ ๊ฐ’์„ ์„œ๋กœ ๊ตํ™˜ํ•  ๋•Œ ์ฃผ์˜ํ•  ์ 

 

// ๋‹ค์Œ๊ณผ ๊ฐ™์ด ํ•˜๋ฉด ์•ˆ๋จ

score[i] = score[least];  // score[i]์˜ ๊ธฐ์กด ๊ฐ’์€ ํŒŒ๊ดด๋œ๋‹ค!

score[least] = score[i];

 

// ์˜ฌ๋ฐ”๋ฅธ ๋ฐฉ๋ฒ•

temp = list[i];

list[i] = list[least];

list[least] = temp;

 

 

 

 

 

 

์ˆœ์ฐจ ํƒ์ƒ‰ (search algorigm)

 

: ์ˆœ์ฐจ ํƒ์ƒ‰์€ ๋ฐฐ์—ด์˜ ์›์†Œ๋ฅผ ์ˆœ์„œ๋Œ€๋กœ ํ•˜๋‚˜์”ฉ ๊บผ๋‚ด์„œ ํƒ์ƒ‰ํ‚ค์™€ ๋น„๊ตํ•˜์—ฌ ์›ํ•˜๋Š” ๊ฐ’์„ ์ฐพ์•„๊ฐ€๋Š” ๋ฐฉ๋ฒ•์ด๋‹ค.

 

 

for ๋ฃจํ”„๋ฅผ ์ด์šฉํ•˜์—ฌ list[i]์™€ key๋ฅผ ๋น„๊ตํ•˜๋Š” ์—ฐ์‚ฐ์„ ๋ฐฐ์—ด์˜ ํฌ๊ธฐ๋งŒํผ ๋ฐ˜๋ณตํ•œ๋‹ค.

๋งŒ์•ฝ list[i]์™€ key๊ฐ€ ๊ฐ™์œผ๋ฉด ํƒ์ƒ‰์€ ์„ฑ๊ณต๋˜๊ณ  ํ‚ค๊ฐ’์ด ๋ฐœ๊ฒฌ๋œ ๋ฐฐ์—ด์˜ ์ธ๋ฑ์Šค๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค.

 

#include <stdio.h>
#define SIZE 10

int main(void)
{
    int key, i;
    int list[SIZE] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
    
    printf("ํƒ์ƒ‰ํ•  ๊ฐ’์„ ์ž…๋ ฅํ•˜์‹œ์˜ค: ");
    scanf("%d", &key);
    
    for(i = 0 ; i < SIZE ; i++)
    	if(list[i] == key)
        
    printf("ํƒ์ƒ‰ ์„ฑ๊ณต ์ธ๋ฑ์Šค= %d\n", i);
    printf("ํƒ์ƒ‰ ์ข…๋ฃŒ\n");
    
    return 0;
}

 

- ๋งŒ์•ฝ 7์„ ์ž…๋ ฅํ•˜๋ฉด ํƒ์ƒ‰ ์„ฑ๊ณต ์ธ๋ฑ์Šค๋Š” 6์ด ๋œ๋‹ค.

 

 

 

 

 

 

 

์ด์ง„ ํƒ์ƒ‰(binary search)

 

: ์ •๋ ฌ๋œ ๋ฐฐ์—ด์˜ ์ค‘์•™์— ์œ„์น˜ํ•œ ์›์†Œ์™€ ๋น„๊ต ๋˜ํ’€์ด

: ์†๋„๊ฐ€ ๋น ๋ฅด๋‹ค

 

 

#include <stdio.h>
#define SIZE 16
int binary_search(int list[], int n, int key)

int main(void)
{
    int key;
    int grade[SIZE] = { 2, 6, 11, 13, 18, 20, 22, 27, 29, 30, 34, 38, 41, 42, 45, 47 };
    
    printf("ํƒ์ƒ‰ํ•  ๊ฐ’์„ ์ž…๋ ฅํ•˜์‹œ์˜ค: ");
    scanf("%d", &key);
    
    printf("ํƒ์ƒ‰ ๊ฒฐ๊ณผ= %d\n", binary_search(grade, SIZE, key));
    
    return 0;
}

 

 

int binary_search(int list[], int n, int key)
{
    int low, high, middle;
    low = 0;
    high = n-1;
    while( low <= high ) {   //์•„์ง ์ˆซ์ž๋“ค์ด ๋‚จ์•„์žˆ์œผ๋ฉด
    
    	printf("[%d %d]\n", low, high) // ํ•˜ํ•œ๊ณผ ์ƒํ•œ์„ ์ถœ๋ ฅํ•œ๋‹ค
        middle = (low + high)/2;       // ์ค‘๊ฐ„ ์œ„์น˜๋ฅผ ๊ณ„์‚ฐํ•ด์„œ
        if( key == list[middle] )
        	return middle;              // ์ผ์น˜ํ•˜๋ฉด ํƒ์ƒ‰ ์„ฑ๊ณต
        else if( key > list[middle] )   // ์ค‘๊ฐ„ ์›์†Œ๋ณด๋‹ค ํฌ๋‹ค๋ฉด
        	low = middle + 1            // ์ƒˆ๋กœ์šด ๊ฐ’์œผ๋กœ low ์„ค์ •
        else
        	high = middle - 1          // ์ƒˆ๋กœ์šด ๊ฐ’์œผ๋กœ high ์„ค์ •
    }
    return - 1 ;
}

 

 

 

 

 

 

 

2์ฐจ์› ๋ฐฐ์—ด

 

int s[ROWS][COLS];

 int s[10];    //1์ฐจ์› ๋ฐฐ์—ด
 int s[3][10];    //2์ฐจ์› ๋ฐฐ์—ด
 int s[5][3][10];    //3์ฐจ์› ๋ฐฐ์—ด

 

 

 

 

 

 

2์ฐจ์› ๋ฐฐ์—ด์˜ ์ดˆ๊ธฐํ™”

 

- ํ–‰์ด 3๊ฐœ, ์—ด์ด 5๊ฐœ๋ผ๋Š” ๊ฒƒ์„ ์ปดํ“จํ„ฐ์— ์•Œ๋ ค์คŒ

int s[3][5] = {
    {0, 1, 2, 3, 4},
    {10, 11, 12, 13, 14},
    {20, 21, 22, 23, 24}
};

 

- ์—ด์ด 5๊ฐœ๋ผ๋Š” ๊ฒƒ์„ ์ปดํ“จํ„ฐ์— ์•Œ๋ ค์คŒ. ํ–‰์ด ๋ช‡๊ฐœ๋ผ๋Š” ๊ฑธ ์•Œ๋ ค์ฃผ์ง€ ์•Š์œผ๋ฉด ์ปดํ“จํ„ฐ๊ฐ€ ์•Œ์•„์„œ ํ–‰์˜ ๊ฐฏ์ˆ˜๋ฅผ ์บ์น˜ํ•จ

int s[][5] = {
    {0, 1, 2, 3, 4},
    {10, 11, 12, 13, 14},
    {20, 21, 22, 23, 24}
};

 

- ์—ด์ด 5๊ฐœ๋ผ๋Š” ๊ฒƒ์„ ์ปดํ“จํ„ฐ์— ์•Œ๋ ค์คŒ. ๋งˆ์ง€๋ง‰ ์ˆซ์ž ๋’ค์— ,๋ฅผ ์ผ์œผ๋ฏ€๋กœ ํ–‰์€ 3๊ฐœ๊ฐ€ ์ƒ๊น€

int s[][5] = {
    0, 1, 2, 3, 4,
    5, 6, 7, 8, 9,
};

 

 

 

 

 

 

ํ–‰๋ ฌ(matrix)

 

: ์ž์—ฐ๊ณผํ•™์—์„œ ๋งŽ์€ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๋Š”๋ฐ ์‚ฌ์šฉํ•œ๋‹ค.

 

- 2์ฐจ์› ๋ฐฐ์—ด์„ ํ•จ์ˆ˜๋กœ ์ „๋‹ฌํ•˜๊ธฐ

#include <stdio.h>

#define YEARS 3;
#define PRODUCTS 5;

int sum(int grade[][PRODUCTS]);

int main(void)
{
    int sales[YEARS][PRODUCTS] = {{1,2,3},{4,5,6},{7,8,9}};
    int total_sale;
    
    total_sale = sum(sales);
    printf("์ด ๋งค์ถœ์€ %d์ž…๋‹ˆ๋‹ค.\n", total_sale);
    
    return 0;
}

int sum(int grade[][PRODUCTS])
{
    int y,p;
    int total = 0;
    
    for(y = 0 ; y < YEARS ; y++)
    	for(p = 0 ; p < PRODUCTS ; p++)
        	total += grade[y][p];
    
    return total;
}