选择排序的输出错误 [英] Wrong output from selection sort

查看:118
本文介绍了选择排序的输出错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在传递'EXAMPLE'作为输入之后,显示的输出是'AELMPXE',我不知道为什么'E'中的一个没有被排序,在跟踪程序之后我得到的是它在第二次迭代后没有比较元素,请帮忙。



我尝试过:



< pre lang =c ++>< pre> #include < stdio.h >
#include < stdlib.h >
#include < string.h >

void selection_sort( char a [], int n)
{
int pos,i,j;
char temp;
for (i = 0 ; i< n- 2 ; i ++)
{
pos = i;
for (j = i + 1 ; j< n- 1 ; j ++)
{
if (a [pos]> a [j])
{
pos = j;
}
}
temp = a [pos];
a [pos] = a [i];
a [i] = temp;
}
printf( %s,a);
}
int main()
{
char a [ 30 ];
int n;
printf( 输入字符串:);
scanf( %s,a);
n = strlen(a);
selection_sort(a,n);
return 0 ;
}

解决方案

您需要非常小心地为 c>声明。如果你使用<比较运算符,然后

  for (i =  0 ; i< n; ++ i){



对长度为n的数组的每个元素执行,直到最后一个位置n-1。它不执行i == n



它与< =比较运算符

<的for语句完全相同pre lang =C ++> for (i = 0 ; i< = n- 1 ; ++ i){


引用:

在传递'EXAMPLE'作为输入后,显示的输出为'AELMPXE'



用户输入的最后一个字母未排序。这可能只意味着一件事:您的排序例程过早停止,您需要在排序例程中为结束值添加1。

您应该学习使用调试器,它允许您查看代码一步一步执行。



有一个工具可以让你看到你的代码在做什么,它的名字是调试器。它也是一个很好的学习工具,因为它向你展示了现实,你可以看到哪种期望与现实相符。

当你不明白你的代码在做什么或为什么它做它做的时候,答案就是答案是调试器

使用调试器查看代码正在执行的操作。只需设置断点并查看代码执行情况,调试器允许您逐行执行第1行并在执行时检查变量。



调试器 - 维基百科,免费的百科全书 [ ^ ]



掌握Visual Studio 2010中的调试 - 初学者指南 [ ^ ]

使用Visual Studio 2010进行基本调试 - YouTube [ ^ ]

调试器在这里向您展示您的代码正在做什么,您的任务是与什么进行比较应该这样做。

调试器中没有魔法,它没有找到错误,它只是帮助你。当代码没有达到预期的效果时,你就会接近一个错误。


After passing 'EXAMPLE' as input the output shown is 'AELMPXE', i am not getting why one of the 'E' is not being sorted, after tracing the program i got that its not comparing after 2nd iteration with the last element, please help.

What I have tried:

<pre>#include <stdio.h>
#include <stdlib.h>
#include<string.h>

void selection_sort(char a[] , int n)
{
    int pos,i,j;
    char temp;
    for(i = 0;i < n-2;i++)
    {
        pos = i;
        for(j = i + 1;j < n-1;j++)
        {
            if(a[pos] > a[j])
            {
                pos = j;
            }
        }
        temp = a[pos];
        a[pos] = a[i];
        a[i] = temp;
    }
    printf("%s",a);
}
int main()
{
    char a[30];
    int n;
    printf("Enter the string :");
    scanf("%s",a);
    n = strlen(a);
    selection_sort(a , n);
    return 0;
}

解决方案

You need to be very careful in writing the for statement. If you use the < comparison operator, then

for (i=0; i<n; ++i) {


executes for each element of an array of length n, up to the last position n-1. It does not execute for i == n

It does the same thing as a for statement with the <= comparison operator

for (i=0; i<=n-1; ++i) {


Quote:

After passing 'EXAMPLE' as input the output shown is 'AELMPXE'


The last letter of user input is not sorted. This can mean only 1 thing: your sort routine stops too early and you need to add 1 to the end value in sorting routine.
You should learn to use the debugger, it allow you to see your code execute step by step.

There is a tool that allow you to see what your code is doing, its name is debugger. It is also a great learning tool because it show you reality and you can see which expectation match reality.
When you don't understand what your code is doing or why it does what it does, the answer is debugger.
Use the debugger to see what your code is doing. Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.


这篇关于选择排序的输出错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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