본문 바로가기

B1:기초 Basement

선택 정렬 (selection sort)

반응형
주제(Subject)
--------------------------------------------------------
한글(약어) : 선택 정렬()
영어(약어) : selection sort()


관련개념(Related Concepts)
--------------------------------------------------------
정렬
알고리즘


개요(Summary)
--------------------------------------------------------
- 별도의 메모리 공간이 필요하지 않음(제자리 정렬)
- 안정적/불안정적 모두 구현 가능


본문(Body)
--------------------------------------------------------
1. n개의 자료에 대한 키 비교회수
   n(n-1) / 2 회

2. 시뮬레이션 링크
  
http://www.cs.queensu.ca/home/malamb/SortingDemos/SelectionSortDemo.html

3. 알고리즘
   1) C (from
Wikipedia : selection sort)
void selectionSort(int a[], int size)
{
  int i, j, min;

  for (i = 0; i < size - 1; i++)
  {
    min = i;
    for (j = i+1; j < size; j++)
      if (a[j] < a[min])
        min = j;

    swap(a[i], a[min]);
  }
}

4. Source
   1) C++(from
24bytes.com : selection sort)

#include <iostream.h>

void selectionSort(int *array,int length)//selection sort function
{
 int i,j,min,minat;
 for(i=0;i<(length-1);i++)
 {
  minat=i;
  min=array[i];
      for(j=i+1;j<(length);j++)      //select the min of the rest of array
   {
    if(min>array[j])                      //ascending order for descending reverse
    {
     minat=j;                               //the position of the min element
     min=array[j];
    }
   }
   int temp=array[i] ;
   array[i]=array[minat];            //swap
   array[minat]=temp;  
 }
}

void printElements(int *array,int length) //print array elements
{
 int i=0;
 for(i=0;i<10;i++)
    cout<<array[i]<<endl;
}

void main()
{
    int a[]={9,6,5,23,2,6,2,7,1,8};    // array to sort
    selectionSort(a,10);                    //call to selection sort 
    printElements(a,10);                     // print elements
}


모임,단체(Commutities)
--------------------------------------------------------


블로그,개인 홈페이지 등(Humanities)
--------------------------------------------------------


참고문서(References)
--------------------------------------------------------
* 한국어(Korean)
저자. 역자. "제목". 출판사. 출판년도. (ISBN:)

* 영어(English)
저자. 제목, 판, 출판사. 출판년도. (ISBN:)
1. Wikipedia : Selection Sort
  
http://en.wikipedia.org/wiki/Selection_sort

2. Wanginator : Selection Sort
   http://www.wanginator.de/studium/applets/selectionsort_en.html

반응형