본문 바로가기
프로그래밍/알고리즘

알고리즘 정렬(sort) - 선택정렬(Selection sort)

by -현's- 2012. 4. 1.
반응형

 

●선택정렬(Selection sort)

- 데이터에서 가장 큰 값을 찾아서 마지막 위치에 있는 데이터와 교환하고, 그 다음 두번째로 큰 값을 갖은 데이터를 찾아서 마지막 전 위치에 있는 데이터와 교환한다. 이런식으로 모든 데이터를 정렬한다.

  

●선택정렬 코드

 #include<stdio.h>

int i, n=10, data[10] = {32, 1, 15, 2, 7, 3, 9, 7, 92, 12};

void select(int array[], int n)
{
 int i, j, temp;
 for(i=0; i<n-1; i++)
 {
  for(j=i+1; j<n; j++)
  {
   if(array[i]>array[j])
   {
    temp=array[i];
    array[i]=array[j];
    array[j]=temp;
   }
  }
 }
}

void output(){
 for(i=0; i<n; i++)
 {
  printf("%d ", data[i]);
 }
 printf("\n");
}

int main()
{
 output();
 select(data, n);
 output();
 return 0;
}

 

 

 

 

 

 

 

 

 

반응형

댓글