小编Rac*_*hid的帖子

确定插入排序中执行的移位次数?

我正在尝试解决这个问题http://www.mycodeschool.com/work-outs/sorting/7 问题是在插入排序中找不到任何变化。

我已经编写了代码,但无法弄清楚我在逻辑上哪里出错了

http://ideone.com/GGjZjw

#include<iostream>
#include<cstdio>
#include<cmath>
// Include headers as needed

using namespace std;

int main()
{
// Write your code here
int T,count,n,*a;
// int imin;
cin >> T;
int value,hole;

while(T--)
{
    cin >> n;
    count=0;
    a=new int[n];
    //reading the input array
    for(int i=0;i<n;i++)
    {
        cin >> a[i];
    }

    // considering the 0th element to be already sorted and
    // remaining list unsorted
    for(int i=1;i<n;i++)
    {
        value=a[i];
        hole=i;
        // shifting 
        while(hole>0&&a[hole-1]>value)
        {
            // …
Run Code Online (Sandbox Code Playgroud)

c++ arrays sorting insertion-sort

2
推荐指数
1
解决办法
1911
查看次数

标签 统计

arrays ×1

c++ ×1

insertion-sort ×1

sorting ×1