C#

c# 배열(array, List) 정렬 알고리즘에 대한 설명

100ksoft 2023. 12. 24. 17:44

1. Sort 방법

    1)조건이 성립되면 배열 위치를 변경시킨다.

data[i] > data[j]  // i기준으로 큰수를 뒤로 보내는 조건; 즉 오름차순 정렬이다.
int tmpC = data[i]
data[i] = data[j]
data[j] = tmpC

data[i] < data[j]  // i기준으로 작은수를 뒤로 보내는 조건; 즉 내림차순 정렬이다.
int tmpC = data[i]
data[i] = data[j]
data[j] = tmpC

2. 기준수 n에 가까운 수를 구하는 방법 1 (정렬 알고리즘을 이용)

    public int[] solution(int[] numlist, int n) {
        int[] answer = new int[numlist.Length];
        int[] copy_numlist = (int[])numlist.Clone();
        int[] nearlist = new int[numlist.Length];                        
        
        for (int i = 0; i < numlist.Length; i++){
            nearlist[i] = n-numlist[i];
        }                                
        
        for(int i = 0; i < nearlist.Length; i++){
            for(int j = i; j < nearlist.Length; j++){                                       
                if (Math.Abs(nearlist[i]) > Math.Abs(nearlist[j])){
                    int tmpC = nearlist[i];
                    nearlist[i] = nearlist[j];
                    nearlist[j] = tmpC;
                }
                else if (Math.Abs(nearlist[i]) == Math.Abs(nearlist[j])) {
                    if (nearlist[i] > nearlist[j]){
                        int tmpC = nearlist[i];
                        nearlist[i] = nearlist[j];
                        nearlist[j] = tmpC;
                    }                    
                }                
            }
        }        
        
        for (int i = 0; i < nearlist.Length; i++){
            for (int j = 0; j < answer.Length; j++){                
                if (nearlist[i] == (n-copy_numlist[j])){                    
                    answer[i] = copy_numlist[j];     
                    break;
                }
            }            
        }                
        return answer;
    }

3. 기준수 n에 가까운 수를 구하는 방법  2( Linq  사용)

    public int[] solution(int[] numlist, int n)
    {
        int[] answer = new int[numlist.Length];
        answer = numlist.OrderBy(x => Math.Abs(n-x)).ThenByDescending(x=>x).ToArray();
        return answer;
    }

4. 순위 알고리즘 방법

answer = scoreave.Select(x => scoreave.Where(ss=> ss > x).Count() + 1).ToArray();
// ToDo: 내용 수정 필요
static void Main()
{
    
    int[] scores = { 90, 87, 100, 95, 80 };             
    int[] rankings = Enumerable.Repeat(1, 5).ToArray(); 
    
    for (int i = 0; i < scores.Length; i++)
    {
        rankings[i] = 1; //1등으로 초기화, 순위 배열을 매 회전마다 1등으로 초기화
        for (int j = 0; j < scores.Length; j++)
        {
            if (scores[i] < scores[j]) //현재(i)와 나머지(j) 비교
            {
                rankings[i]++;         //RANK: 나보다 큰 점수가 나오면 순위 1증가
            }
        }
    }

    for (int i = 0; i < scores.Length; i++)
    {
        Console.WriteLine($"{scores[i],3}점 : {rankings[i]}등");
    }
}

실행 결과
 90점 : 3등
 87점 : 4등
100점 : 1등
 95점 : 2등
 80점 : 5등

5. 정수값을 숫자 내림차순으로 정렬하는 방법

public class Solution {
    public long solution(long n) {
        long answer = 0;        
        char[] arr;
        arr = n.ToString().ToCharArray();                
        for(int i = 0; i < arr.Length; i++){                        
            for(int j = i; j < arr.Length; j++){                
                if ((arr[i] - '0') < (arr[j] - '0')){
                    //swap
                    var tmp = arr[i];
                    arr[i] = arr[j];
                    arr[j] = tmp;
                }                    
            }            
        }           
        for(int i = 0; i < arr.Length; i++){
            Console.WriteLine($"arr[{i}]={arr[i]}");
        }
        
        string str = string.Join("", arr);
        Console.WriteLine(str);
        answer = Convert.ToInt64(str);
        return answer;
    }
}

// Input : 149317
// Return : 974311