各种算法的改进

冒泡排序的改进

void BubbleSort(int arr[],int len)

{

    bool isSort= false;

    for ( inti = 0; i

    {

        isSort = true;

        for ( int j = 0; j < len - i -1; j++ )

        {

            if ( arr[j] > arr[j+1] )

            {

                swap(arr[j],arr[j+1]);

                isSort = false;

            }

        }

 

    }

}

直接插入排序的改进

void InsertSort(int arr[],int len )

{

    int sentinel= 0;

    for ( inti = 1; i

    {

        if (arr[i] < arr[i-1])

        {

            sentinel = arr[i];

            int j = i -1;

            do

            {

                arr[j+1] = arr[j];

                j--;

            } while (sentinel < arr[j]&&j >=0);

            arr[j+1] =sentinel;

        }

    }

}

选择排序的改进

 

void SelectSort(int ARRAY[],int LEN)

{

    for (inti = 1; i<= LEN - 1; i++)

    {

        int t= i -1;

        for (intj = i; j < LEN; j++)

        {

            if (ARRAY[j] < ARRAY[t])

            { t = j; }

        }

        if (t!= (i - 1))

        {

            int temp = ARRAY[i - 1];

            ARRAY[i - 1] = ARRAY[t];

            ARRAY[t] = temp;

        }

}

}

你可能感兴趣的:(各种算法的改进)