Test/C

배열을 활용한 프로그래밍 기법 - 선택정렬

kiostory 2018. 12. 12. 21:57

배열값을 오름차순으로.


#include <stdio.h>
int main(void)

{
 int aList[5] = { 30,40,10,50,20 };
 int i = 0, j = 0, nTmp = 0;
 int nIndexMin = 0;


 for (i = 0; i < 4; ++i)
 {
  nIndexMin = i;
  for (j = i + 1; j < 5; ++j)
  {
   if (aList[nIndexMin] > aList[j])
    nIndexMin = j;
  }
  if (i != nIndexMin)
  {
   nTmp = aList[i];
   aList[i] = aList[nIndexMin];
   aList[nIndexMin] = nTmp;
  }

 }
 
 for (i = 0; i < 5; ++i)
  printf("%d\t", aList[i]);
  
 return 0;
}


------------------------------------------

10      20      30      40      50