동작 방식

- 반씩 쪼개서 합쳐 나가는 형태로 정렬을 한다.
- 모두 쪼개서 한개의 요소로 구성된 그룹으로 만든다.
- 그런다음 두개를 비교 할때 작은것을 앞으로 넣어 두개씩 그룹을 만든다.
- 그런다음 두그룹에서 맨앞에 있는 값중에 작은 값을 먼저 넣으면서 4개짜리 그룹을 만든다.
- 해당 작업을 반복을 하면 데이터의 갯수(N) 전체 반복작업 횟수 log8 = 3으로 NlogN을 보장한다.

장점

- O(NlogN)을 보장한다.

단점

 - 기존 데이터를 추가로 담을 메모리 공간이 필요해서 메모리 활용에 비효율 적이다.

시간 복잡도

O(NlogN)
    - 8개의 데이터가 있다 가정하면 데이터의 갯수(N) 전체 반복작업 횟수 log 2에 8 = 3으로 NlogN을 보장한다.

python 구현

    '''
    @ Author : bsh0817
    @ Created : 2020. 01. 26
    @ Update : 2020. 01. 26
    @ Description : Merge sort (병합 정렬)
                    - 전역 변수로 사용 하면 메모리 사용 측면에서 효율 적이지만 그렇게 하지 않았다.
    '''

    def fn_merge_sort(list_input):
        '''
            @Description: Merge sort (병합 정렬)

            @Arguments Example
                list_input = [10, 5, 1, 8, 4, 3, 2, 9, 6, 7]
        '''
        int_len_list_input = len(list_input)
        list_init = []
        int_merge_group_index_count = 1

        while int_len_list_input > int_merge_group_index_count:
            int_end_index = 0
            while int_end_index < int_len_list_input:
                int_index_b = int_end_index + int_merge_group_index_count*1
                int_index_c = int_index_b + int_merge_group_index_count*1
                list_init += fn_merge_list(list_input[int_end_index:int_index_b], list_input[int_index_b:int_index_c])
                int_end_index = int_index_c
            list_input = list_init
            list_init = []
            int_merge_group_index_count *= 2
        return list_input


    def fn_merge_list(list_a, list_b):
        '''
            @Description: Merge list

            @Arguments Example
                list_a = [1, 3]
                list_b = [2, 4]
        '''
        int_len_a = len(list_a)
        int_len_b = len(list_b)
        int_index_a = 0
        int_index_b = 0
        responce = []
        while int_index_a < int_len_a or int_index_b < int_len_b != 0:
            if int_index_b == int_len_b or (int_index_a < int_len_a and list_a[int_index_a] <= list_b[int_index_b]):
                responce += [list_a[int_index_a]]
                int_index_a += 1
            else:
                responce += [list_b[int_index_b]]
                int_index_b += 1
        print("fn_merge_list : " + str(responce))
        return responce


    if __name__ == "__main__":
        list_fn_merge_sort_param = [10, 1, 5, 8, 4, 3, 2, 9, 6, 7]
        print(fn_merge_sort(list_fn_merge_sort_param))

'알고리즘 > 정렬' 카테고리의 다른 글

7.계수 정렬 (Counting sort)  (0) 2020.01.28
6. 힙 정렬 (Heap sort)  (0) 2020.01.27
4. 퀵 정렬 (Quick sort)  (0) 2020.01.26
3. 삽입 정렬 (Insertion sort)  (0) 2020.01.24
2. 버블정렬 (Bubble sort)  (0) 2020.01.24

동작 방식

 - 맨 처음 값 기준으로 왼쪽에서 오른쪽으로 큰게 있는지 확인
 - 그런다음 오른쪽에서 왼쪽으로는 기준값보다 작은값이 있는지 확인
 - 그런다음 그 두 값의 자리를 바꾼다.
 - 그러다 작은값의 index 보다 큰 값의 index 값이 더 크게 되었을때
 - 기준 값과 작은 값을 자리를 바꾼다.
 - 그러면 기준값은 재자리를 찾았다.
 - 그런다음 기준값을 기준으로 왼쪽과 오른쪽 배열에 대해 위 작업을 반복 수행한다.

특징

평균적으로 속도가 가장 빨라라이브러리 등 많은 곳에서 쓰인다.

장점

- O(N×N) 정렬보다 압도적으로 빠르다.

단점

 - 최악의 경우에는 O(N×N)의 속도를 낸다 (ex > 배열이 이미 정렬이 되어 있는경우)
    분할 정복의 장점을 사용하지 못하기 때문.

시간 복잡도

O(NlogN)
    - 추론 1 : 원소가 10개의 배열 1개를 O(N*N) 작업을 한다고 가정 == 100
    - 추론 2 : 원소가 5개의 배열 2개를 O(N*N) 작업을 한다고 가정 == 50
    - 해당 원리를 '분할정복'이라 한다.
    - 따라서 기준값을 기준으로 반씩 쪼개기 때문에 logN  x 원소의 수 N
    - 결론 : 시간 복잡도는 NlogN

python 구현

    '''
    @ Author : bsh0817
    @ Created : 2020. 01. 25
    @ Update : 2020. 01. 25
    @ Description : Quick sort (퀵 정렬)
    '''

    def fn_swapping(list_target, int_index_a, int_index_b):
        '''
            @Description: List index swapping

            @Arguments Example
                list_target = [10, 5, 1, 8, 4, 3, 2, 9, 6, 7]
                int_index_a = 0
                int_index_b = 2
        '''
        value_tmp = list_target[int_index_a]
        list_target[int_index_a] = list_target[int_index_b]
        list_target[int_index_b] = value_tmp
        return list_target


    def fn_quick_sort(list_input):
        '''
            @Description: quick sort (퀵 정렬)

            @Arguments Example
                list_input = [10, 5, 1, 8, 4, 3, 2, 9, 6, 7]
        '''
        int_len_list_input = len(list_input)
        value_standard = list_input[0]
        print("start : " + str(list_input))
        for _ in range(int_len_list_input):
            if int_len_list_input > 1:
                int_left_index = 0
                int_right_index = 0
                for int_index in range(1, int_len_list_input):
                    if value_standard < list_input[int_index]:
                        int_left_index = int_index
                        break
                for int_index in range(1, int_len_list_input):
                    if value_standard > list_input[int_len_list_input - int_index]:
                        int_right_index = int_len_list_input - int_index
                        break
                if int_left_index > int_right_index or int_left_index == 0:
                    list_input = fn_swapping(list_input, 0, int_right_index)
                    list_left_responce = []
                    list_right_responce = []

                    if int_right_index > 0:
                        list_left_responce = fn_quick_sort(list_input[0:int_right_index])

                    if len(list_input[int_right_index + 1:]) > 0:
                        list_right_responce = fn_quick_sort(list_input[int_right_index +1:])

                    if len(list_left_responce + list_right_responce) > 0:
                        list_input = list_left_responce + [value_standard] + list_right_responce
                    break
                else:
                    list_input = fn_swapping(list_input, int_right_index, int_left_index)
            else:
                return list_input
        return list_input


    if __name__ == "__main__":
        list_fn_quick_sort_param = [10, 1, 5, 8, 4, 3, 2, 9, 6, 7]
        print(fn_quick_sort(list_fn_quick_sort_param))

'알고리즘 > 정렬' 카테고리의 다른 글

6. 힙 정렬 (Heap sort)  (0) 2020.01.27
5. 병합 정렬(Merge sort)  (0) 2020.01.26
3. 삽입 정렬 (Insertion sort)  (0) 2020.01.24
2. 버블정렬 (Bubble sort)  (0) 2020.01.24
1. 선택정렬(Selection sort)  (0) 2020.01.24

동작 방식

배열에서 앞에 값들 중 적절한 곳에 위치 시킨다.
    ex> [10, 5, 1] 의 배열이라 가정
        - 10은 맨 앞의 값이라 건너 뛴다.
        - 5는 앞의 값 10의 앞 또는 뒤 두 곳중 한 곳에 갈 수 있다.
        - 해당 값은 10보다 작으므로 10을 뒤로 밀고 [5, 10, 1]로 만든다.
        - 1은 맨 앞, 5와 10 사이 그리고 10 뒤에 세 곳중 한 곳에 갈 수있다.
        - 1은 가장 작으니 맨앞으로 가고 나머지를 뒤로 밀어준다. [1, 5, 10]

특징

동작 하는 기준 값의 앞에 있는 배열은 이미 정렬되어 있다는 가정하에 작업한다.

장점

- 특징 때문에 일반적으로 버블정렬이나 선택 정렬 보다 빠르다.
- 배열이 거의 정렬이 되어 있는경우 O(NlongN)의 복잡도를 가지는 배열(퀵, 병합, 힙)보다도 빠르다.

시간 복잡도

O(N*N)
    - 추론 1 : 10 + 9 + 8 ... +1 번 비교 작업
    - 추론 2 : N(N+1)/2
    - 추론 3 : '/2'는 N이 엄청 큰 수라고 가정 했을 경우 의미 없는 값으로 처리한다
    - 결론 : 시간 복잡도는 N*N

동작 순서 예시

[10, 5, 1, 8, 4, 3, 2, 9, 6, 7]
[5, 10, 1, 8, 4, 3, 2, 9, 6, 7]
[1, 5, 10, 8, 4, 3, 2, 9, 6, 7]
[1, 5, 8, 10, 4, 3, 2, 9, 6, 7]
[1, 4, 5, 8, 10, 3, 2, 9, 6, 7]
[1, 3, 4, 5, 8, 10, 2, 9, 6, 7]
[1, 2, 3, 4, 5, 8, 10, 9, 6, 7]
[1, 2, 3, 4, 5, 8, 9, 10, 6, 7]
[1, 2, 3, 4, 5, 6, 8, 9, 10, 7]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

python 구현

    '''
    @ Author : bsh0817
    @ Created : 2020. 01. 24
    @ Update : 2020. 01. 24
    @ Description : Insertion sort (삽입 정렬)
    '''

    def fn_swapping(list_target, int_index_a, int_index_b):
        '''
            @Description: List index swapping

            @Arguments Example
                list_target = [10, 5, 1, 8, 4, 3, 2, 9, 6, 7]
                int_index_a = 0
                int_index_b = 2
        '''
        value_tmp = list_target[int_index_a]
        list_target[int_index_a] = list_target[int_index_b]
        list_target[int_index_b] = value_tmp
        return list_target

    def fn_insertion_sort(list_input):
        '''
            @Description: Insertion sort (삽입 정렬)

            @Arguments Example
                list_input = [10, 5, 1, 8, 4, 3, 2, 9, 6, 7]
        '''

        int_len_list_input = len(list_input)
        for int_list_input_index in range(int_len_list_input):
            for int_index in range(0, int_list_input_index):
                if list_input[int_index] > list_input[int_list_input_index]:
                    value_tmp = list_input[int_list_input_index]
                    for int_move_index in range(int_list_input_index - int_index):
                        list_input = fn_swapping(list_input, int_list_input_index - int_move_index, int_list_input_index - int_move_index - 1)
                    list_input[int_index] = value_tmp
                    break
            print(list_input)

    if __name__ == "__main__":
        list_fn_insertion_sort_param = [10, 5, 1, 8, 4, 3, 2, 9, 6, 7]
        fn_insertion_sort(list_fn_insertion_sort_param)

'알고리즘 > 정렬' 카테고리의 다른 글

6. 힙 정렬 (Heap sort)  (0) 2020.01.27
5. 병합 정렬(Merge sort)  (0) 2020.01.26
4. 퀵 정렬 (Quick sort)  (0) 2020.01.26
2. 버블정렬 (Bubble sort)  (0) 2020.01.24
1. 선택정렬(Selection sort)  (0) 2020.01.24

동작 방식

 - 배열의 바로 다음값과 비교해서 큰값을 뒤로 자리를 바꿔준다
 - 해당 작업을 처음부터 끝까지 한번 진행하면 맨뒤에 가장 큰 값이 간다.
 - 그러므로 그다음은 위 작업을 마지막 배열 전까지 작업을 한다.
 - 해당 작업을 반복하면 정렬이 이루어 진다.

단점

 - 선택정렬보다 효율이 엄청 떨어짐 (자리를 바꾸는 작업이 훨씬 더 많기 때문.)

시간 복잡도

O(N*N)
    - 추론 1 : 10 + 9 + 8 ... +1 번 비교 작업
    - 추론 2 : N(N+1)/2
    - 추론 3 : '/2'는 N이 엄청 큰 수라고 가정 했을 경우 의미 없는 값으로 처리한다
    - 결론 : 시간 복잡도는 N*N

동작 순서 예시

[5, 1, 8, 4, 3, 2, 9, 6, 7, 10]
[1, 5, 4, 3, 2, 8, 6, 7, 9, 10]
[1, 4, 3, 2, 5, 6, 7, 8, 9, 10]
[1, 3, 2, 4, 5, 6, 7, 8, 9, 10]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

python 구현

    '''
    @ Author : bsh0817
    @ Created : 2020. 01. 24
    @ Update : 2020. 01. 24
    @ Description : Bubble sort (버블 정렬)
    '''

    def fn_swapping(list_target, int_index_a, int_index_b):
        '''
            @Description: List index swapping

            @Arguments Example
                list_target = [10, 5, 1, 8, 4, 3, 2, 9, 6, 7]
                int_index_a = 0
                int_index_b = 2
        '''
        value_tmp = list_target[int_index_a]
        list_target[int_index_a] = list_target[int_index_b]
        list_target[int_index_b] = value_tmp
        return list_target

    def fn_bubble_sort(list_input):
        '''
            @Description: Bubble sort (버블 정렬)

            @Arguments Example
                list_input = [10, 5, 1, 8, 4, 3, 2, 9, 6, 7]
        '''

        int_len_list_input = len(list_input)
        for int_list_input_index in range(int_len_list_input):
            for int_index in range(0, int_len_list_input - int_list_input_index -1):
                if list_input[int_index + 1] < list_input[int_index]:
                    list_input = fn_swapping(list_input, int_index, int_index + 1)
            print(list_input)

    if __name__ == "__main__":
        list_fn_bubble_sort_param = [10, 5, 1, 8, 4, 3, 2, 9, 6, 7]
        fn_bubble_sort(list_fn_bubble_sort_param)

'알고리즘 > 정렬' 카테고리의 다른 글

6. 힙 정렬 (Heap sort)  (0) 2020.01.27
5. 병합 정렬(Merge sort)  (0) 2020.01.26
4. 퀵 정렬 (Quick sort)  (0) 2020.01.26
3. 삽입 정렬 (Insertion sort)  (0) 2020.01.24
1. 선택정렬(Selection sort)  (0) 2020.01.24

동작 방식

배열 요소를 하나씩 다 비교해서 가장 작은걸 앞으로 보내는 방식
    ex> index 0-9 비교 후 가장 작은것 맨앞으로 바꿈
        다음 1-9 비교후 가장 작은것 두번째
        이 작업을 배열의 수 만큼 반복

단점

효율이 엄청 떨어짐

시간 복잡도

O(N*N)
    - 추론 1 : 10 + 9 + 8 ... +1 번 비교 작업
    - 추론 2 : N(N+1)/2
    - 추론 3 : '/2'는 N이 엄청 큰 수라고 가정 했을 경우 의미 없는 값으로 처리한다
    - 결론 : 시간 복잡도는 N*N

동작 순서 예시

[1, 5, 10, 8, 4, 3, 2, 9, 6, 7]
[1, 2, 10, 8, 4, 3, 5, 9, 6, 7]
[1, 2, 3, 8, 4, 10, 5, 9, 6, 7]
[1, 2, 3, 4, 8, 10, 5, 9, 6, 7]
[1, 2, 3, 4, 5, 10, 8, 9, 6, 7]
[1, 2, 3, 4, 5, 6, 8, 9, 10, 7]
[1, 2, 3, 4, 5, 6, 7, 9, 10, 8]
[1, 2, 3, 4, 5, 6, 7, 8, 10, 9]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]  

python3 구현

  '''
  @ Author : bsh0817
  @ Created : 2020. 01. 23
  @ Update : 2020. 01. 23
  @ Description : Selection sort (선택 정렬)
  '''

  def fn_swapping(list_target, int_index_a, int_index_b):
      '''
          @Description: List index swapping

          @Arguments Example
              list_target = [10, 5, 1, 8, 4, 3, 2, 9, 6, 7]
              int_index_a = 0
              int_index_b = 2
      '''
      value_tmp = list_target[int_index_a]
      list_target[int_index_a] = list_target[int_index_b]
      list_target[int_index_b] = value_tmp
      return list_target

  def fn_selection_sort(list_input):
      '''
          @Description: Selection sort (선택 정렬)

          @Arguments Example
              list_input = [10, 5, 1, 8, 4, 3, 2, 9, 6, 7]
      '''
      int_len_list_input = len(list_input)
      for int_list_input_index in range(int_len_list_input):
          int_min_index = int_list_input_index
          int_min = list_input[int_list_input_index]
          for int_index in range(int_list_input_index + 1, int_len_list_input):
              if int_min > list_input[int_index]:
                  int_min_index = int_index
                  int_min = list_input[int_min_index]
          list_input = fn_swapping(list_input, int_list_input_index, int_min_index)
          print(list_input)

  if __name__ == "__main__":
      list_fn_selection_sort_param = [10, 5, 1, 8, 4, 3, 2, 9, 6, 7]
      fn_selection_sort(list_fn_selection_sort_param)

'알고리즘 > 정렬' 카테고리의 다른 글

6. 힙 정렬 (Heap sort)  (0) 2020.01.27
5. 병합 정렬(Merge sort)  (0) 2020.01.26
4. 퀵 정렬 (Quick sort)  (0) 2020.01.26
3. 삽입 정렬 (Insertion sort)  (0) 2020.01.24
2. 버블정렬 (Bubble sort)  (0) 2020.01.24
안녕하세요
저는 어제 AWS에서 진행하는
Moving to AWS Serverless 워크샵
을 다녀왔습니다.

행사 일정과 장소는
일시: 2019년 3월 12일(화) 9:30 ~ 17:30
장소 : 역삼역 GS 타워 14층 305, 306호
에서 진행됬습니다.

상세 커리큘럼 페이지 링크

우선 워크샵은 기본적인 AWS 서비스를 다룰줄 아는 분들을 대상으로
진행했습니다.
사전에  EC2, S3, VPC, lambda, auto scaling
정도에 서비스들을 잘 다루지는 못해도 각각 어떤 서비스 인지 정도 알고 가시면 좋을것 같습니다.

행사장에 도책해보니 커비도 준비되어 있었고
강의실 안에는 과자랑 물들이 준비되어 있었습니다 ㅎㅎ

그리고 작게 IoT 서비스를 체험 할 수있는 곳도 있었는데
어떻게하는지 몰라서... 못해봤네요 ㅎㅎ

그리고 왼쪽 벽에는 아이콘들을 가지고 도식화한 내용이 있었습니다 ㅎㅎ
저희도 칠판에 저렇게 해도 좋을것 같네요 ㅎㅎ


아무튼 사설은 여기까지

저는 이번 교육을 듣기 전 lambda, cognito dynamodb, ec2, s3,api gatway등 1년 가까이
서비스를 사용했었습니다.
그래서 교육 내용을 듣는데 큰 어려움이 없었고
아는 내용이라고 하더라도
 
cloud9또는 chalice같은 새로운 개발환경에 대한 내용을 배울수 있어 좋았습니다.

또한 실습중 저는 DB의 종류가 너무 많아
어느정도 규모 또는 어떤 특성에 어떤 DB 를
사용해야 하는지 평소 고민었는데
이런 내용들을 문의하면서
가이드를 받을 수 있어 수업 내용 이상에 가치를 얻었다고 생각합니다.

이런 실습 형태의 교육은 처음으로 진행 되었으며
앞으로 새로운 주제들로 지속적으로 진행한다고하니

그때마다 꼭 참석 해야할것 같습니다 ㅎㅎ


해당 수업은 스타트업 처럼 규모가 작고 적은인원으로 많은 다양한 일을 처리하면서 자문을 구하거나 가이드를 받기 어려운 분들은 정말 많은 내용들을 받아 갈 수있을 것으로 생각 됩니다.

그럼 여기까지 Moving to AWS Serverless
후기였습니다 ㅎㅎ

끝까지 읽어주셔서 감사합니다.


AWS CloudFront에서 default root object 모든 Path 적용 이슈 우회 방법



- 작업 개요

Cloud Front의 경우 Default Root Object 설정이 최상위 Directory 에서 밖에 적용이 되지 않는다.

S3에서 웹사이트 호스팅(Static website hosting)을 할때 S3에서 [Index document] 를 index.html로 설정한다면 

아래 표와 같기 Redirect를 해준다.

접속 URL 

Redirect URL 

 www.example.com/

 www.example.com/index.html

 www.example.com/test/ 

 www.example.com/test/index.html


그러나 Cloud Front 에서는 아무리  Default Root Object를 설정해도 최상위 Path를 제외하고는 오류가 발생한다.

접속 URL 

Redirect URL 

 www.example.com/

 www.example.com/index.html

 www.example.com/test/ 

ERROR(403)


따라서 Cloud Front를 적용 할 떄 최상위 path를 제외하고는 url을 전체 다 입력 해줘야한다.

해당 이슈에대한 우회 방법으로 Path와 같은 파일 명으로 html 파일을 생성고,

그 파일 S3에 업로드 할때 Header에  Content-Type을 text/html로 설정하면 해당 파일을 html로인식해서 

www.example.co.kr/test/ 로 입력해도 CloudFont 서비스가 가능해진다.



- 작업 절차

웹 서비스를 할 S3에 html 파일을 업로드하고, html 파일 명을 확장자를 제외한 Path 명으로 변경한다.

그런 다음 html 파일의 Meta data에서 Content-Type을 text/html로 변경한다. 

그리고 Cloud Front의 캐시를 초기화 시킨다.



1. S3에서 파일을 선택한다음 [Properties]-[Metadata]에서 

[Content-Type]을 [text/html]로 변경해준다.


2. 그런 다음 파일 명에서 확장자를 지운다.

3. CloudFont 에서 Invalidations 탭에 아래와 같이 edge location에 cache를 초기화 시킨다.

(이미 edge Location에 올라가있는 파일은 Metadata 값이 TTL 시간이 지나더라도 초기화 되지 않으므로 꼭 이 작업을 해주어야한다.)



해당 작업을 완료하면 CloudFont와 S3를 사용해 https 서비스를 사용하면서 모든 Root에 index 파일을 설정한것 처럼

millionairedeveloper.example.co.kr/test/

test 가 html이기 때문에 서비스가 가능해진다.




그럼 오늘도 행복한 하루 되세요.

'AWS > S3' 카테고리의 다른 글

AWS S3를 EC2 인스턴스에 s3fs를 이용한 mount 예제  (2) 2019.01.28

linux grep 명령어 문자열 검색


명령어 : grep 

기본 사용법  : grep [옵션] "검색 할 문자열"  [파일명]



옵션 

-i : 대소문자를 구별 안함 


-w : 전체 단어와 일치하는 행만 출력 


-n : 일치하는 행의 번호와 행을 같이 출력 


-c : 일치하는 행의 수를 출력 


-l : 문자열이 포함된 파일명을 출력 


-v : 일치하지 않는 행만 출력 



응용방법


grep '^a' [파일명] 

a로 시작하는 행을 출력한다.


grep [a-c] [파일명]

a,b,c 로 시작하는 모든 단어를 찾아 출력한다.


grep '^[ab]' [파일명]

 a나 b로 시작되는 행을 출력한다.


grep [aA]bcd [파일명]

파일에서 abcd 또는 Abcd로 시작하는 행을 출력한다.


grep 'a$' [파일명]

a로 끝나는 행을 출력한다.


grep 'a.....b' [파일명]

a로 시작하고b로 끝나는 총 7자리 단어가 포함된 행을 출력한다.


grep 'a*' [파일명]

a로 시작하는 단어가 포함된 행을 출력한다.


grep 'abcd' d*

 d로 시작하는 모든 파일에서 apple 를 포함하는 모든 행을 출력한다.


grep '^a' [파일명] |  grep 'a$'

a로 시작하는 행을 찾은 결과에서 a로 끝나는 데이터를 찾는다.





이상 입니다.ㅎㅎ 


+ Recent posts