🌈Sorting

RoRAΒ·2023λ…„ 5μ›” 25일
0

Solidity (use_ Remix)

λͺ©λ‘ 보기
10/10

πŸͺ‚ 2023. 05. 12. Friday_ νŒ€ λͺ…단 곡지! 뚜λ‘₯! πŸͺ‚


βœ… Intro

​

μ•ˆλ…•ν•˜μ„Έμš”!😁

λͺ¨λ‘ ν•¨κ»˜ 4κ°œμ›” λ™μ•ˆ ν•˜λ‚˜μ˜ 멋진 ν”„λ‘œμ νŠΈλ₯Ό μ™„μ„±ν•˜λŠ” κ·Έλ‚ κΉŒμ§€ ν™”μ΄νŒ…ν•©μ‹œλ‹€ !πŸ‘πŸ‘πŸ‘
​
* 이 κ³³μ—μ„œλŠ” 당일에 배운 λ‚΄μš©μ„ λ³΅μŠ΅ν•˜λŠ” μš©λ„λ‘œ 글을 μž‘μ„±ν•˜λ €κ³  ν•©λ‹ˆλ‹€.
​
* μ˜€λŠ˜μ€ μ†”λ¦¬λ””ν‹°μ˜ 'Sorting' λ‚΄μš©μ„ λ³΅μŠ΅ν•΄λ³΄κ² μŠ΅λ‹ˆλ‹€.


​
​

βœ… Sorting

​

πŸ‘‰ Sorting μ‹€μŠ΅ μ½”λ“œ -(1)

    function sorting() public {
        for(uint i=0;i<numbers.length-1;i++) {
            for(uint j=i+1; j<numbers.length ;j++) {
                if(numbers[i] < numbers[j]) {
                    (numbers[i], numbers[j]) = (numbers[j], numbers[i]);
                }
            }
        }
    }

πŸ‘‰ Sorting μ‹€μŠ΅ μ½”λ“œ -(2)

    function sorting2() public {
        for(uint j=1; j<numbers.length;j++) {
            for(uint i=0; i<j; i++) {
                if(numbers[i] < numbers[j]) {
                    (numbers[i], numbers[j]) = (numbers[j], numbers[i]);
                }
            }
        }
    }

πŸ‘‰ Sorting μ‹€μŠ΅ μ½”λ“œ 전체 μ½”λ“œ

//κ°€μž₯ μƒμœ„μ— μžˆλŠ” 4λͺ…을 찾고자 sorting을 ν•˜κ²Œλœ 것이닀 == 
contract Sorting{
    
    //μˆ«μžν˜• λ³€μˆ˜ numbers
    uint[] numbers;


    //numbers에 νŠΉμ •ν•œ 값을 λ„£λŠ” ν•¨μˆ˜
    function push(uint _n) public {
        numbers.push(_n);
    }

    //전체 arrayλ₯Ό λ°˜ν™˜λ°›λŠ” ν•¨μˆ˜
    function get() public view returns(uint[] memory) {
        return numbers;
    }

    //5자리의 수λ₯Ό μ •λ ¬ν•  경우
    //01,02,03,04 - 12,13,14 - 23,24 - 34 순으둜 비ꡐ
    function sorting() public {
        for(uint i=0;i<numbers.length-1;i++) {
            for(uint j=i+1; j<numbers.length ;j++) {
                if(numbers[i] < numbers[j]) {
                    (numbers[i], numbers[j]) = (numbers[j], numbers[i]);
                }
            }
        }
    }

    //더 λ˜‘λ˜‘ν•˜κ³  효율적인 방법!!!
    //01 - 02,12 - 03,13,23 - 04,14,24,34 순으둜 λΉ„κ΅ν•˜κ²Œ 됌

    function sorting2() public {
        for(uint j=1; j<numbers.length;j++) {
            for(uint i=0; i<j; i++) {
                if(numbers[i] < numbers[j]) {
                    (numbers[i], numbers[j]) = (numbers[j], numbers[i]);
                }
            }
        }
    }
}

​

πŸ‘‰ μœ„ μ½”λ“œ λœ―μ–΄λ³΄κΈ° !_ ( 5자리 수λ₯Ό μ •λ ¬ν•  경우)

​
βœ”οΈ Sorting μ‹€μŠ΅ μ½”λ“œ -(1)

β €β €β–ͺ️ 01,02,03,04 - 12,13,14 - 23,24 - 34 순으둜 비ꡐ

β €β €β–ͺ️ address payable owner : λˆμ„ 받을 수 μžˆλŠ” address ν˜• λ³€μˆ˜ ownder μ„ μ–Έ
​

βœ”οΈ Sorting μ‹€μŠ΅ μ½”λ“œ -(1)

β €β €β–ͺ️ 01 - 02,12 - 03,13,23 - 04,14,24,34 순으둜 비ꡐ
β €β €β–ͺ️ 더 λ˜‘λ˜‘ν•˜κ³  효율적인 방법이닀 !
​


​
​

βœ… μ‹€μŠ΅ κ°€μ΄λ“œ

​

πŸ₯° κ°•μ‚¬λ‹˜ κ°μ‚¬ν•©λ‹ˆλ‹€ πŸ₯°


1. push() μ‚¬μš©ν•˜μ—¬ 2,5,7,4,3,6 넣어보기
2. get() μ‚¬μš©ν•˜μ—¬ κ²°κ³Ό 확인 -> 2,5,7,4,3,6
3. sorting() μ‚¬μš© ν›„, get()으둜 κ²°κ³Ό 확인 -> 7,6,5,4,3,2 

​

β €β €β €

πŸ“Œ23. 05. 12

벨둜그 밀리면 μ•ˆλ˜λŠ”λ°...μ΄λ―Έγ…œγ…œλ°€λ €λ²„λ €μ„œ κ³ ν†΅λ°›λŠ” 쀑 ..

0개의 λŒ“κΈ€