冒泡排序法实现中的问题

u013163178 2014-01-03 01:18:44
每次编写一些下程序总是遇到一些奇怪的问题,不知道的时候我就上CSDN论坛求答案,每次都能得到满意的回答,都学到了很多书上没有的知识。这次又遇到了问题
#include "stdafx.h"
#include <stdio.h>
int main()
{
int i,j,temp;
int a[10];
for(i=0;i<=9;i++)
{
printf("please enter a number\n");
scanf_s("%d",&a[i]);
}
for(j=0;j<=9;j++)
{
for(i=0;i<=9;i++)
{
if(a[i]>a[i+1])
{
temp=a[i];
a[i]=a[i+1];
a[i+1]=temp;
}
}
}
printf("排序后:\n");
for(i=0;i<=9;i++)
printf("%d",a[i]);
return 0;
这个程序在运行时提示数组a[i]被破坏

运行结果
...全文
216 19 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
19 条回复
切换为时间正序
请发表友善的回复…
发表回复
gogiqp_jyh 2014-01-03
  • 打赏
  • 举报
回复
引用 14 楼 u013163178 的回复:
[quote=引用 13 楼 u010800064 的回复:] [quote=引用 10 楼 u013163178 的回复:] [quote=引用 5 楼 derekrose 的回复:] [quote=引用 4 楼 u013163178 的回复:] [quote=引用 2 楼 u010800064 的回复:]
a[i]>a[i+1]
这个条件调用了越界的小标,当i=9时
我改了后还是这样
Run-Time Check Failure #2 - Stack around the variable 'a' was corrupted.
[/quote]上改后的代码[/quote]
#include "stdafx.h"
#include <stdio.h>
int main()
{
	int i,j,temp;
	int a[10];
	for(i=0;i<=9;i++)
	{
		printf("please enter a number\n");
		scanf_s("%d",&a[i]);
	}
	for(j=0;j<=9;j++)
	{
		for(i=0;i<=9;i++)
		{
			if(a[i]>a[i-1])
			{
				temp=a[i];
				a[i]=a[i-1];
				a[i-1]=temp;
			}
		}
	}
	printf("排序后:\n");
	for(i=0;i<=9;i++)
		printf("%d",a[i]);
	return 0;
}


[/quote]
#include <stdio.h>

int main(int argc, char **argv)
{
    int i,j,temp;
    int a[10];
    for(i=0; i<=9; i++)
    {
        printf("please enter a number\n");
        scanf("%d",&a[i]);
    }

    for(j=0; j<=9; j++)
    {
        for(i=0; i<9-j; i++)
        {
            if(a[i] > a[i+1])
            {
                temp = a[i];
                a[i] = a[i+1];
                a[i+1] = temp;
            }
        }
    }

    printf("排序后:\n");
    for(i=0;i<=9;i++)
    {
        printf("%d",a[i]);
    }

    return 0;
}
[/quote]请问我的错在哪里,我的目的是要搞清楚哪里错了,避免下次发生类似错误[/quote]
for(i=0;i<=9;i++)
        {
            if(a[i]>a[i-1])
            {
                temp=a[i];
                a[i]=a[i-1];
                a[i-1]=temp;
            }
        }
在使用数组时第一要注意的就是你做循环有没有越界产生。 a[i-1] 当i=0 的也是越界了
「已注销」 2014-01-03
  • 打赏
  • 举报
回复
引用 16 楼 cait_sith 的回复:
[quote=引用 7 楼 u013163178 的回复:] [quote=引用 1 楼 lpcads 的回复:] line 14 for(i=0;i<=9;i++) => for(i=0;i<9;i++)
#include "stdafx.h"
#include <stdio.h>
int main()
{
	int i,j,temp;
	int a[10];
	for(i=0;i<=9;i++)
	{
		printf("please enter a number\n");
		scanf_s("%d",&a[i]);
	}
	for(j=0;j<=9;j++)
	{
		for(i=0;i<=9;i++)
		{
			if(a[i]>a[i-1])
			{
				temp=a[i];
				a[i]=a[i-1];
				a[i-1]=temp;
			}
		}
	}
	printf("排序后:\n");
	for(i=0;i<=9;i++)
		printf("%d",a[i]);
	return 0;
}

I correct it,but it also can run.
Run-Time Check Failure #2 - Stack around the variable 'a' was corrupted
[/quote]

for(i=0;i<=9;i++)
		{
			if(a[i]>a[i-1])
			{
				temp=a[i];
				a[i]=a[i-1];
				a[i-1]=temp;
			}
		}
i=0时,a[0]>a[-1][/quote]解决了,谢谢你
「已注销」 2014-01-03
  • 打赏
  • 举报
回复
引用 15 楼 Sharing_Li 的回复:
[quote=引用 8 楼 u013163178 的回复:] [quote=引用 6 楼 Sharing_Li 的回复:] 嵌套循环中的if语句中,数组下标越界了。当i = 9时,a[i+1]= a[10]越界
if(a[i]>a[i-1])
改成这样了,还是有点问题
“冒泡排序.exe”(Win32): 已加载“C:\Users\I love u\Desktop\新建文件夹\冒泡排序\Debug\冒泡排序.exe”。已加载符号。
“冒泡排序.exe”(Win32): 已加载“C:\Windows\SysWOW64\ntdll.dll”。已加载符号。
“冒泡排序.exe”(Win32): 已加载“C:\Windows\SysWOW64\kernel32.dll”。已加载符号。
“冒泡排序.exe”(Win32): 已加载“C:\Windows\SysWOW64\KernelBase.dll”。已加载符号。
“冒泡排序.exe”(Win32): 已加载“C:\Windows\SysWOW64\msvcr110d.dll”。已加载符号。
Run-Time Check Failure #2 - Stack around the variable 'a' was corrupted.
程序“[7592] 冒泡排序.exe”已退出,返回值为 0 (0x0)。
[/quote] ...因为等于0时,a[i-1] = a[-1],下标错误,细心点哟[/quote]改好了,哎,我太粗行了
Cai菜 2014-01-03
  • 打赏
  • 举报
回复
引用 7 楼 u013163178 的回复:
[quote=引用 1 楼 lpcads 的回复:] line 14 for(i=0;i<=9;i++) => for(i=0;i<9;i++)
#include "stdafx.h"
#include <stdio.h>
int main()
{
	int i,j,temp;
	int a[10];
	for(i=0;i<=9;i++)
	{
		printf("please enter a number\n");
		scanf_s("%d",&a[i]);
	}
	for(j=0;j<=9;j++)
	{
		for(i=0;i<=9;i++)
		{
			if(a[i]>a[i-1])
			{
				temp=a[i];
				a[i]=a[i-1];
				a[i-1]=temp;
			}
		}
	}
	printf("排序后:\n");
	for(i=0;i<=9;i++)
		printf("%d",a[i]);
	return 0;
}

I correct it,but it also can run.
Run-Time Check Failure #2 - Stack around the variable 'a' was corrupted
[/quote]

for(i=0;i<=9;i++)
		{
			if(a[i]>a[i-1])
			{
				temp=a[i];
				a[i]=a[i-1];
				a[i-1]=temp;
			}
		}
i=0时,a[0]>a[-1]
Sharing_Li 2014-01-03
  • 打赏
  • 举报
回复
引用 8 楼 u013163178 的回复:
[quote=引用 6 楼 Sharing_Li 的回复:] 嵌套循环中的if语句中,数组下标越界了。当i = 9时,a[i+1]= a[10]越界
if(a[i]>a[i-1])
改成这样了,还是有点问题
“冒泡排序.exe”(Win32): 已加载“C:\Users\I love u\Desktop\新建文件夹\冒泡排序\Debug\冒泡排序.exe”。已加载符号。
“冒泡排序.exe”(Win32): 已加载“C:\Windows\SysWOW64\ntdll.dll”。已加载符号。
“冒泡排序.exe”(Win32): 已加载“C:\Windows\SysWOW64\kernel32.dll”。已加载符号。
“冒泡排序.exe”(Win32): 已加载“C:\Windows\SysWOW64\KernelBase.dll”。已加载符号。
“冒泡排序.exe”(Win32): 已加载“C:\Windows\SysWOW64\msvcr110d.dll”。已加载符号。
Run-Time Check Failure #2 - Stack around the variable 'a' was corrupted.
程序“[7592] 冒泡排序.exe”已退出,返回值为 0 (0x0)。
[/quote] ...因为等于0时,a[i-1] = a[-1],下标错误,细心点哟
「已注销」 2014-01-03
  • 打赏
  • 举报
回复
引用 13 楼 u010800064 的回复:
[quote=引用 10 楼 u013163178 的回复:] [quote=引用 5 楼 derekrose 的回复:] [quote=引用 4 楼 u013163178 的回复:] [quote=引用 2 楼 u010800064 的回复:]
a[i]>a[i+1]
这个条件调用了越界的小标,当i=9时
我改了后还是这样
Run-Time Check Failure #2 - Stack around the variable 'a' was corrupted.
[/quote]上改后的代码[/quote]
#include "stdafx.h"
#include <stdio.h>
int main()
{
	int i,j,temp;
	int a[10];
	for(i=0;i<=9;i++)
	{
		printf("please enter a number\n");
		scanf_s("%d",&a[i]);
	}
	for(j=0;j<=9;j++)
	{
		for(i=0;i<=9;i++)
		{
			if(a[i]>a[i-1])
			{
				temp=a[i];
				a[i]=a[i-1];
				a[i-1]=temp;
			}
		}
	}
	printf("排序后:\n");
	for(i=0;i<=9;i++)
		printf("%d",a[i]);
	return 0;
}


[/quote]
#include <stdio.h>

int main(int argc, char **argv)
{
    int i,j,temp;
    int a[10];
    for(i=0; i<=9; i++)
    {
        printf("please enter a number\n");
        scanf("%d",&a[i]);
    }

    for(j=0; j<=9; j++)
    {
        for(i=0; i<9-j; i++)
        {
            if(a[i] > a[i+1])
            {
                temp = a[i];
                a[i] = a[i+1];
                a[i+1] = temp;
            }
        }
    }

    printf("排序后:\n");
    for(i=0;i<=9;i++)
    {
        printf("%d",a[i]);
    }

    return 0;
}
[/quote]请问我的错在哪里,我的目的是要搞清楚哪里错了,避免下次发生类似错误
gogiqp_jyh 2014-01-03
  • 打赏
  • 举报
回复
引用 10 楼 u013163178 的回复:
[quote=引用 5 楼 derekrose 的回复:] [quote=引用 4 楼 u013163178 的回复:] [quote=引用 2 楼 u010800064 的回复:]
a[i]>a[i+1]
这个条件调用了越界的小标,当i=9时
我改了后还是这样
Run-Time Check Failure #2 - Stack around the variable 'a' was corrupted.
[/quote]上改后的代码[/quote]
#include "stdafx.h"
#include <stdio.h>
int main()
{
	int i,j,temp;
	int a[10];
	for(i=0;i<=9;i++)
	{
		printf("please enter a number\n");
		scanf_s("%d",&a[i]);
	}
	for(j=0;j<=9;j++)
	{
		for(i=0;i<=9;i++)
		{
			if(a[i]>a[i-1])
			{
				temp=a[i];
				a[i]=a[i-1];
				a[i-1]=temp;
			}
		}
	}
	printf("排序后:\n");
	for(i=0;i<=9;i++)
		printf("%d",a[i]);
	return 0;
}


[/quote]
#include <stdio.h>

int main(int argc, char **argv)
{
    int i,j,temp;
    int a[10];
    for(i=0; i<=9; i++)
    {
        printf("please enter a number\n");
        scanf("%d",&a[i]);
    }

    for(j=0; j<=9; j++)
    {
        for(i=0; i<9-j; i++)
        {
            if(a[i] > a[i+1])
            {
                temp = a[i];
                a[i] = a[i+1];
                a[i+1] = temp;
            }
        }
    }

    printf("排序后:\n");
    for(i=0;i<=9;i++)
    {
        printf("%d",a[i]);
    }

    return 0;
}
gogiqp_jyh 2014-01-03
  • 打赏
  • 举报
回复
引用 7 楼 u013163178 的回复:
[quote=引用 1 楼 lpcads 的回复:] line 14 for(i=0;i<=9;i++) => for(i=0;i<9;i++)
#include "stdafx.h"
#include <stdio.h>
int main()
{
	int i,j,temp;
	int a[10];
	for(i=0;i<=9;i++)
	{
		printf("please enter a number\n");
		scanf_s("%d",&a[i]);
	}
	for(j=0;j<=9;j++)
	{
		for(i=0;i<=9;i++)
		{
			if(a[i]>a[i-1])
			{
				temp=a[i];
				a[i]=a[i-1];
				a[i-1]=temp;
			}
		}
	}
	printf("排序后:\n");
	for(i=0;i<=9;i++)
		printf("%d",a[i]);
	return 0;
}

I correct it,but it also can run.
Run-Time Check Failure #2 - Stack around the variable 'a' was corrupted
[/quote]
#include <stdio.h>

int main(int argc, char **argv)
{
    int i,j,temp;
    int a[10];
    for(i=0; i<=9; i++)
    {
        printf("please enter a number\n");
        scanf("%d",&a[i]);
    }

    for(j=0; j<=9; j++)
    {
        for(i=0; i<9-j; i++)
        {
            if(a[i] > a[i+1])
            {
                temp = a[i];
                a[i] = a[i+1];
                a[i+1] = temp;
            }
        }
    }

    printf("排序后:\n");
    for(i=0;i<=9;i++)
    {
        printf("%d",a[i]);
    }

    return 0;
}
「已注销」 2014-01-03
  • 打赏
  • 举报
回复
引用 9 楼 netbooting 的回复:

#include <stdio.h>

int main(void)
{
    int   i, j, temp;
    int   a[10];
    int   swapped = 1;

    for (i = 0; i <= 9; i++) {
        printf("please enter a number\n");
        scanf("%d", &a[i]);
    }

	/* 冒泡排序 */
    for (i = 0; swapped; i++) {
    	/* 如果一轮检查下来没有一次交换,则说明数据已经排序排好,不用继续比较 */
        swapped = 0;
        /* j < 9 - i即可,因为9-i后面的数都是已经排好序的较大的数,不用再次比较 */
        for (j = 0; j < 9 - i; j++) {
            if (a[j] > a[j + 1]) {
                temp = a[j];
                a[j] = a[j + 1];
                a[j + 1] = temp;
                swapped = 1;
            }
        }
    }

    printf("排序后:\n");

    for (i = 0; i <= 9; i++)
        printf("%d ", a[i]);

    return 0;
}
 /* j < 9 - i即可,因为9-i后面的数都是已经排好序的较大的数,不用再次比较 */
有道理
「已注销」 2014-01-03
  • 打赏
  • 举报
回复
引用 5 楼 derekrose 的回复:
[quote=引用 4 楼 u013163178 的回复:] [quote=引用 2 楼 u010800064 的回复:]
a[i]>a[i+1]
这个条件调用了越界的小标,当i=9时
我改了后还是这样
Run-Time Check Failure #2 - Stack around the variable 'a' was corrupted.
[/quote]上改后的代码[/quote]
#include "stdafx.h"
#include <stdio.h>
int main()
{
	int i,j,temp;
	int a[10];
	for(i=0;i<=9;i++)
	{
		printf("please enter a number\n");
		scanf_s("%d",&a[i]);
	}
	for(j=0;j<=9;j++)
	{
		for(i=0;i<=9;i++)
		{
			if(a[i]>a[i-1])
			{
				temp=a[i];
				a[i]=a[i-1];
				a[i-1]=temp;
			}
		}
	}
	printf("排序后:\n");
	for(i=0;i<=9;i++)
		printf("%d",a[i]);
	return 0;
}


netbooting 2014-01-03
  • 打赏
  • 举报
回复

#include <stdio.h>

int main(void)
{
    int   i, j, temp;
    int   a[10];
    int   swapped = 1;

    for (i = 0; i <= 9; i++) {
        printf("please enter a number\n");
        scanf("%d", &a[i]);
    }

	/* 冒泡排序 */
    for (i = 0; swapped; i++) {
    	/* 如果一轮检查下来没有一次交换,则说明数据已经排序排好,不用继续比较 */
        swapped = 0;
        /* j < 9 - i即可,因为9-i后面的数都是已经排好序的较大的数,不用再次比较 */
        for (j = 0; j < 9 - i; j++) {
            if (a[j] > a[j + 1]) {
                temp = a[j];
                a[j] = a[j + 1];
                a[j + 1] = temp;
                swapped = 1;
            }
        }
    }

    printf("排序后:\n");

    for (i = 0; i <= 9; i++)
        printf("%d ", a[i]);

    return 0;
}
「已注销」 2014-01-03
  • 打赏
  • 举报
回复
引用 6 楼 Sharing_Li 的回复:
嵌套循环中的if语句中,数组下标越界了。当i = 9时,a[i+1]= a[10]越界
if(a[i]>a[i-1])
改成这样了,还是有点问题
“冒泡排序.exe”(Win32): 已加载“C:\Users\I love u\Desktop\新建文件夹\冒泡排序\Debug\冒泡排序.exe”。已加载符号。
“冒泡排序.exe”(Win32): 已加载“C:\Windows\SysWOW64\ntdll.dll”。已加载符号。
“冒泡排序.exe”(Win32): 已加载“C:\Windows\SysWOW64\kernel32.dll”。已加载符号。
“冒泡排序.exe”(Win32): 已加载“C:\Windows\SysWOW64\KernelBase.dll”。已加载符号。
“冒泡排序.exe”(Win32): 已加载“C:\Windows\SysWOW64\msvcr110d.dll”。已加载符号。
Run-Time Check Failure #2 - Stack around the variable 'a' was corrupted.
程序“[7592] 冒泡排序.exe”已退出,返回值为 0 (0x0)。
「已注销」 2014-01-03
  • 打赏
  • 举报
回复
引用 1 楼 lpcads 的回复:
line 14 for(i=0;i<=9;i++) => for(i=0;i<9;i++)
#include "stdafx.h"
#include <stdio.h>
int main()
{
	int i,j,temp;
	int a[10];
	for(i=0;i<=9;i++)
	{
		printf("please enter a number\n");
		scanf_s("%d",&a[i]);
	}
	for(j=0;j<=9;j++)
	{
		for(i=0;i<=9;i++)
		{
			if(a[i]>a[i-1])
			{
				temp=a[i];
				a[i]=a[i-1];
				a[i-1]=temp;
			}
		}
	}
	printf("排序后:\n");
	for(i=0;i<=9;i++)
		printf("%d",a[i]);
	return 0;
}

I correct it,but it also can run.
Run-Time Check Failure #2 - Stack around the variable 'a' was corrupted
Sharing_Li 2014-01-03
  • 打赏
  • 举报
回复
嵌套循环中的if语句中,数组下标越界了。当i = 9时,a[i+1]= a[10]越界
derekrose 2014-01-03
  • 打赏
  • 举报
回复
引用 4 楼 u013163178 的回复:
[quote=引用 2 楼 u010800064 的回复:]
a[i]>a[i+1]
这个条件调用了越界的小标,当i=9时
我改了后还是这样
Run-Time Check Failure #2 - Stack around the variable 'a' was corrupted.
[/quote]上改后的代码
「已注销」 2014-01-03
  • 打赏
  • 举报
回复
引用 2 楼 u010800064 的回复:
a[i]>a[i+1]
这个条件调用了越界的小标,当i=9时
我改了后还是这样
Run-Time Check Failure #2 - Stack around the variable 'a' was corrupted.
buyong 2014-01-03
  • 打赏
  • 举报
回复
for(i=0;i<=9;i++) { if(a[i]>a[i+1]) when i=9, a[i+1] is a[10], beyond scope
gogiqp_jyh 2014-01-03
  • 打赏
  • 举报
回复
a[i]>a[i+1]
这个条件调用了越界的小标,当i=9时
lpcads 2014-01-03
  • 打赏
  • 举报
回复
line 14 for(i=0;i<=9;i++) => for(i=0;i<9;i++)

70,023

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧