#include "stdafx.h"

#include <iostream>

 

void DirectSelectSort(int* a, int iLen);

void Swap(int* a, int index1, int index2);

void PrintArray(int a[], int iLen);

 

int _tmain(int argc, _TCHAR* argv[])

{

    int arrayToSort[] = {20, 7, 3, 4, 25, 15, 29, 12, 4, 1};

    int n = sizeof(arrayToSort)/sizeof(int);

    PrintArray(arrayToSort, n);

    DirectSelectSort(arrayToSort, n);

    PrintArray(arrayToSort, n);

    return 0;

}

 

void DirectSelectSort(int* a, int iLen)

{

    for(int i=0; i<=iLen-2; i++)

    {

       int min = a[i];

       int imin = i;

       for(int j=i+1;j<iLen;j++)

       {

           if(a[j]<min)

           {

              min=a[j];

              imin=j;

           }

       }

       if(i!=imin)

       {

           Swap(a,i,imin);

       }

    }

}

 

void Swap(int* a, int index1, int index2)

{

    if(index1!=index2&&a!=NULL)

    {

       int temp = a[index1];

       a[index1] = a[index2];

       a[index2] = temp;

    }

}

 

void PrintArray(int a[], int iLen)

{

    for(int i=0; i< iLen; i++)

    {

       std::cout<<a[i]<<' ';

    }

    std::cout<<std::endl;

}

相关文章:

  • 2022-12-23
  • 2021-09-17
  • 2021-08-29
  • 2022-02-27
  • 2021-12-27
  • 2021-07-03
猜你喜欢
  • 2022-12-23
  • 2021-11-12
  • 2021-05-24
  • 2021-07-05
  • 2021-12-03
  • 2022-02-13
相关资源
相似解决方案