bosthhe1 / -

数据结构与算法(初阶)
0 stars 0 forks source link

插入排序 #15

Open bosthhe1 opened 1 year ago

bosthhe1 commented 1 year ago
void InsertSort(int *a, int i)
{
    int end = 0;
    while (end >= 0 && end<i-1)
    {
        if (a[end + 1] > a[end])
        {
            end++;
        }
        else
        {
            int tmp = a[end + 1];
            int i = end;
            while (tmp < a[i] && i >= 0)
            {
                a[i + 1] = a[i];
                i--;
            }
            i++;
            a[i] = tmp;
        }
    }
}

void TestInsert()
{
    int arr[12] = { 1,2, 5, 8, 1, 4, 7, 3, 6, 9, 0,-1 };
    int size = sizeof(arr) / sizeof(arr[0]);
    InsertSort(arr, size);
    for (int i = 0; i < size; i++)
    {
        printf("%d ", arr[i]);
    }
}

int main()
{
    TestInsert();
    return 0;
}