C++ 基于二进制搜索错误的插入排序算法

C++ 基于二进制搜索错误的插入排序算法,c++,algorithm,insertion-sort,C++,Algorithm,Insertion Sort,我运行了下面的代码,这是一个插入排序算法,它使用二进制搜索来查找插入项的正确位置,而不是线性搜索,但是结果中有两个数字排序不正确 #include <iostream> using namespace std; void insertion_sort (int a[], int n /* the size of array */) { int i, temp,j; for (i = 1; i < n; i++) { /* Assume

我运行了下面的代码,这是一个插入排序算法,它使用二进制搜索来查找插入项的正确位置,而不是线性搜索,但是结果中有两个数字排序不正确

#include <iostream>
using namespace std;

void insertion_sort (int a[], int n /* the size of array */)
{
    int i, temp,j;
    for (i = 1; i < n; i++)
    {
        /* Assume items before a[i] are sorted. */
        /* Pick an number */
        temp = a[i];

        /* Do binary search to find out the
           point where b is to be inserted. */

        int low = 0, high = i - 1, k;

        while (high-low>1)
        {
            int mid = (high + low) / 2;

            if (temp <= a[mid]) 
                high = mid;
            else 
                low = mid;
        }

        /* Shift items between high and i by 1 */
        for (k = i; k > high; k--)
            a[k] = a[k - 1];

        a[high] = temp;
    }
}


int main()
{
    int A[15]={9,5,98,2,5,4,66,12,8,54,0,11,99,55,13};
    insertion_sort(A,15);
    for (int i=0; i<15; i++)
        cout<<A[i]<<endl;
    system("pause");
    return 0;
}
#包括
使用名称空间std;
void insertion_sort(int a[],int n/*数组大小*/)
{
内部温度i,温度j;
对于(i=1;i1)
{
int mid=(高+低)/2;
如果(温度高;k--)
a[k]=a[k-1];
a[高]=温度;
}
}
int main()
{
inta[15]={9,5,98,2,5,4,66,12,8,54,0,11,99,55,13};
插入排序(A,15);

对于(int i=0;i这里需要注意的一些事情:

  • 二进制搜索不会给你任何东西,因为你需要移动所有元素来产生空间。因此它实际上增加了算法的总体成本(虽然不是渐进的)

  • >p>因为这是C++,所以在使用它的for循环之前不需要声明k(只使用<代码>(int k;…)< />代码>)< < /p>
  • 分析算法的开头:i=0->low=high=0。因此,while循环不会执行。然后,无论元素是否应该移动,for(k)循环都会交换元素0和1。这是错误号1

  • i的第二次迭代:while循环不会再次执行,因为low=0和high=1,并且无论您交换了什么元素,至少会交换元素1和2。错误号2

  • 现在请注意,无论发生什么,下一次迭代都会将最初位于索引0(在测试代码中为=9)的元素越来越远地移动到最后一个索引


  • 因此,在检查for(i)循环的两次迭代后,您可能会发现[i]之前的元素被排序的假设是错误的,因此算法也是错误的。

    这里需要注意的几点:

    #include <iostream>
    using namespace std;
    
    void insertion_sort (int a[], int n /* the size of array */)
    {
        int i, temp,j;
        for (i = 1; i < n; i++)
        {
            /* Assume items before a[i] are sorted. */
            /* Pick an number */
            temp = a[i];
    
            /* Do binary search to find out the
               point where b is to be inserted. */
    
  • 二进制搜索不会给你任何东西,因为你需要移动所有元素来产生空间。因此它实际上增加了算法的总体成本(虽然不是渐进的)

  • >p>因为这是C++,所以在使用它的for循环之前不需要声明k(只使用<代码>(int k;…)< />代码>)< < /p>
  • 分析算法的开头:i=0->low=high=0。因此,while循环不会执行。然后,无论元素是否应该移动,for(k)循环都会交换元素0和1。这是错误号1

  • i的第二次迭代:while循环不会再次执行,因为low=0和high=1,并且无论您交换了什么元素,至少会交换元素1和2。错误号2

  • 现在请注意,无论发生什么,下一次迭代都会将最初位于索引0(在测试代码中为=9)的元素越来越远地移动到最后一个索引


  • 因此,在检查for(i)循环的两次迭代后,您可能会发现[i]之前的元素被排序的假设是错误的,因此算法也是错误的。

    这里需要注意的几点:

    #include <iostream>
    using namespace std;
    
    void insertion_sort (int a[], int n /* the size of array */)
    {
        int i, temp,j;
        for (i = 1; i < n; i++)
        {
            /* Assume items before a[i] are sorted. */
            /* Pick an number */
            temp = a[i];
    
            /* Do binary search to find out the
               point where b is to be inserted. */
    
  • 二进制搜索不会给你任何东西,因为你需要移动所有元素来产生空间。因此它实际上增加了算法的总体成本(虽然不是渐进的)

  • >p>因为这是C++,所以在使用它的for循环之前不需要声明k(只使用<代码>(int k;…)< />代码>)< < /p>
  • 分析算法的开头:i=0->low=high=0。因此,while循环不会执行。然后,无论元素是否应该移动,for(k)循环都会交换元素0和1。这是错误号1

  • i的第二次迭代:while循环不会再次执行,因为low=0和high=1,并且无论您交换了什么元素,至少会交换元素1和2。错误号2

  • 现在请注意,无论发生什么,下一次迭代都会将最初位于索引0(在测试代码中为=9)的元素越来越远地移动到最后一个索引


  • 因此,在检查for(i)循环的两次迭代后,您可能会发现[i]之前的元素被排序的假设是错误的,因此算法也是错误的。

    这里需要注意的几点:

    #include <iostream>
    using namespace std;
    
    void insertion_sort (int a[], int n /* the size of array */)
    {
        int i, temp,j;
        for (i = 1; i < n; i++)
        {
            /* Assume items before a[i] are sorted. */
            /* Pick an number */
            temp = a[i];
    
            /* Do binary search to find out the
               point where b is to be inserted. */
    
  • 二进制搜索不会给你任何东西,因为你需要移动所有元素来产生空间。因此它实际上增加了算法的总体成本(虽然不是渐进的)

  • >p>因为这是C++,所以在使用它的for循环之前不需要声明k(只使用<代码>(int k;…)< />代码>)< < /p>
  • 分析算法的开头:i=0->low=high=0。因此,while循环不会执行。然后,无论元素是否应该移动,for(k)循环都会交换元素0和1。这是错误号1

  • i的第二次迭代:while循环不会再次执行,因为low=0和high=1,并且无论您交换了什么元素,至少会交换元素1和2。错误号2

  • 现在请注意,无论发生什么,下一次迭代都会将最初位于索引0(在测试代码中为=9)的元素越来越远地移动到最后一个索引

  • 因此,在检查for(i)循环的两次迭代后,您可能会发现,对[i]之前的元素进行排序的假设是错误的,因此算法也是错误的

    #include <iostream>
    using namespace std;
    
    void insertion_sort (int a[], int n /* the size of array */)
    {
        int i, temp,j;
        for (i = 1; i < n; i++)
        {
            /* Assume items before a[i] are sorted. */
            /* Pick an number */
            temp = a[i];
    
            /* Do binary search to find out the
               point where b is to be inserted. */
    
    这里的条件应该是
    low
    ,而不是
    low+1

            // while (high-low>1)
            while (low < high)
            {
                int mid = (high + low) / 2;
    
                if (temp <= a[mid]) 
                    high = mid;
                else 
    
    这里的条件应该是
    low
    ,而不是
    low+1

            // while (high-low>1)
            while (low < high)
            {
                int mid = (high + low) / 2;
    
                if (temp <= a[mid]) 
                    high = mid;
                else 
    
    这里的条件应该是
    low
    ,而不是