인생마린
어떤 공부 블로거의 금서목록
인생마린
전체 방문자
오늘
어제
  • 전체 (155)
    • C언어 (19)
    • Python (14)
      • Flask (0)
    • Coding Challenge (11)
      • Code Clone & Review (0)
      • Toy Project (0)
      • 오늘의 코드 (5)
    • Algorithm (6)
    • JAVA (8)
    • 웹 (8)
      • Javascript (3)
    • 정보보안 (19)
    • 기타 (21)
    • 일기는일기장에 (2)
    • 리눅스 (4)
    • 철학 (1)
    • 주식 (14)
    • AI (2)
    • 독후감 (13)
    • 프로그래밍 (4)
    • 게임 (1)
    • Devops (2)
      • CI_CD (2)
      • AWS (0)
    • Flutter (3)

블로그 메뉴

  • 홈
  • 태그
  • 미디어로그
  • 위치로그
  • 방명록

공지사항

인기 글

태그

  • 정규표현식
  • 가상화폐
  • Java
  • 폭락
  • 주식 #ETF
  • Bitcoin
  • smtplib
  • 퀴즈봇
  • 주린이 #주식
  • 불편한사회
  • Regular Expression
  • 비트코인
  • Sphinx
  • 우영우 #패러디논란
  • Flutter
  • python #eval #dictionary
  • 주식 #배당주
  • turtle
  • flask
  • 카카오톡봇
  • best of best
  • vpn
  • Python
  • 코인
  • 해커톤
  • TFT
  • 백테스팅
  • 테라
  • c언어
  • Regex

최근 댓글

최근 글

티스토리

hELLO · Designed By 정상우.
인생마린

어떤 공부 블로거의 금서목록

Algorithm

[정렬 알고리즘] Seletion Sort (선택 정렬)

2018. 3. 14. 20:45
반응형

§선택 정렬

정렬 되지 않은 부분에서 최소 요소(오름차순 일때)를 반복적으로 찾아 시작 부분에 배치하여 배열을 정렬한다.

 

주어지는 배열은 두가지 경우를 생각 해볼 수 있다.

  1. 부분 배열이 이미 정렬 되었을때
  2. 부분 배열이 정렬 되지 않은 상태일때

 

Following example explains the above steps:

 

arr[] = 64 25 12 22 11

// Find the minimum element in arr[0...4]
// and place it at beginning
11 25 12 22 64

// Find the minimum element in arr[1...4]
// and place it at beginning of arr[1...4]
11
12 25 22 64

// Find the minimum element in arr[2...4]
// and place it at beginning of arr[2...4]
11 12
22 25 64

// Find the minimum element in arr[3...4]
// and place it at beginning of arr[3...4]
11 12 22
25 64

Time Complexity: O(n2) as there are two nested loops.

Auxiliary Space: O(1)

 

구현 방법

  1. 가장 작은 원소를 남은 배열에서 고른다.
  2. 해당 원소와 시작지점의 자리를 바꾼다.
  3. 배열 시작지점을 앞으로 옮긴다.

 

 

구현언어(C)

 

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

////////////////////////////////

//    Selection Sort

//    선택 정렬

////////////////////////////////

 

#include <stdio.h>

 

void swap(int *xp, int *yp) {

    int tmp = *xp;

    *xp = *yp;

    *yp = tmp;

}

 

void selectionSort(int arr[], int n) {

    int i, j, min_idx;

 

    

    for (i = 0; i < n - 1; i++) {

        min_idx = i;    //최소 원소를 찾는다.

        for (j = i + 1; j < n; j++)

            if (arr[j] < arr[min_idx])

                min_idx = j;

 

        //최소 원소와 i번째 원소 자리를 바꾼다.

        swap(&arr[min_idx], &arr[i]);

    }

}

 

void printArray(int arr[], int size) {

    int i;

    for (i = 0; i < size; i++)

        printf("%d ", arr[i]);

    printf("\n");

}

 

int main() {

    int arr[] = { 64, 25, 12, 22, 11 };

    int n = sizeof(arr) / sizeof(arr[0]);

    printf("Before array: ");

    printArray(arr, n);

    selectionSort(arr, n);

    printf("Sorted array: ");

    printArray(arr, n);

    return 0;

}

Colored by Color Scripter

cs


반응형
저작자표시 비영리 (새창열림)

'Algorithm' 카테고리의 다른 글

[정렬 알고리즘] Merge Sort(병합 정렬)  (1) 2018.03.14
[정렬 알고리즘] Recursive Insertion Sort(재귀 삽입 정렬)  (0) 2018.03.14
[정렬 알고리즘] Insertion Sort(삽입정렬)  (0) 2018.03.14
[정렬 알고리즘] Recursive Bubble Sort(재귀 버블 정렬)  (0) 2018.03.14
[정렬 알고리즘] Bubble Sort(버블정렬, 거품정렬)  (0) 2018.03.14
    'Algorithm' 카테고리의 다른 글
    • [정렬 알고리즘] Recursive Insertion Sort(재귀 삽입 정렬)
    • [정렬 알고리즘] Insertion Sort(삽입정렬)
    • [정렬 알고리즘] Recursive Bubble Sort(재귀 버블 정렬)
    • [정렬 알고리즘] Bubble Sort(버블정렬, 거품정렬)
    인생마린
    인생마린
    즐거운 프로그래밍~♬

    티스토리툴바