为什么我的插入排序代码不起作用 [英] Why my code for insertion sort not working

查看:119
本文介绍了为什么我的插入排序代码不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经尝试了很长时间但仍然无法弄清楚出了什么问题



没有编译错误..

问题是我无法获得分拣阵列



我尝试过:



I have been trying for a long time and still could not figure out what is going wrong

There is no compiler error..
The Problem is that I CANT GET THE SORTED ARRAY

What I have tried:

#include <iostream>
#include <string>
using namespace std;

int my_array(int _array[],int size )
{
    for(int i=0; i < size; i++)
    {
        return _array[i];
    }
}

swap(int i, int j, int k)
{
    i = j;
    i = k;
    k = j;
}

void sort(int _array[], int hole, int size)
{
    int i;
    hole = i;
    for (int hole = 1; hole < size; hole++)
    {
        if (hole > 0 && _array[hole-1] > _array[hole])
        {
            swap(_array[hole], i, _array[hole-1]);
            hole = hole - 1;
        }
    }
}

void sorted_array(int _array[], int hole, int size)
{
    sort(_array, hole, size);

}

int main()
{
    int _array[5];
    for (int i=0; i < 5; i++)
    {
        cout << "first element" << " " << i << " ";
        cin >> _array[i];
    }
    
    int hole;
    sorted_array(_array, hole, 5);
    cout << my_array(_array,5) << endl;
}

推荐答案

嗯......那里有一些奇怪的代码!

开始使用您的第一种方法:

Um...that's some odd code you have there!
Start with your first method:
int my_array(int _array[],int size )
    {
    for(int i=0;i<size;i++)>
        {
        return _array[i];
        }
    }

它总是返回数组中的第一个元素,除非大小是负数,在这种情况下它根本不返回任何内容。我很惊讶编译 - 它不应该在现代编译器上,因为有一个路径使得该方法不返回值,这是不允许的。

第二种方法:

It always returns the first element in the array, unless the size is negative, in which case it returns nothing at all. I'm surprised that compiles at all - it shouldn't on a modern compiler as there is a path such that the method does not return a value, and that is not allowed.
The second method:

swap(int i,int j,int k)
    {
    i=j;
    i=k;
    k=j;
    }

什么都不做。说真的,什么都没有。 C和C ++中的值按值传递,而不是通过引用传递,这意味着函数不会影响外部世界 - 函数内部的值是外部值的副本,并且不会复制任何更改。如果不是,那么你可以这样称呼:

Does nothing. Seriously, nothing at all. Values in C and C++ are passed by value, not by reference, which means that the function doesn't affect the world outside - the values inside the function are copies of the values outside, and any changes are not copied back. If they weren't, then you could call it like this:

swap(1, 2, 3)

然后会发生什么?

此外,这不是一个交换功能,所以你期望它做的很多我不确定...

首先看看这两个并找出你的实际意思要做,并尝试让它工作:使用调试器来帮助你。这是一个很棒的工具,可以让您在代码运行时准确地查看和控制正在发生的事情。了解它 - 它会为你节省一些严重的头部刮伤!直到你让它们工作,其余的代码是没用的!当它们工作时,您可以使用调试器来解决其余问题。

And then what would happen?
In addition that isn't a "swap" function, so quite what you expect it to do I'm not sure...
Start by looking at those two and working out what you actually meant to do, and try to get it working: use the debugger to help you. It's a wonderful tool that lets you see and control exactly what is happening while your code is running. Get to know it - it'll save you some serious head scratching later! And until you get them working, the rest of the code is pretty useless! When they work, you can use the debugger to work out what is wrong with the rest.


这篇关于为什么我的插入排序代码不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆