#include <stdio.h>
int main(void) {
int a[5]={1,2,0,5,4},i,j,c;
for(i=0;i<5;i++)
{
for(j=0;j<5-i;j++){
if(a[j]>a[j+1])
{
c=a[j];
a[j]=a[j+1];
a[j+1]=c;
}
}
}
for(i=0;i<5;i++)
{
printf("%d",a[i]);
}
return 0;
}
ideone说“超出时间限制:5个记忆:2048信号:24”
但它在turbo编译器上工作正常
答案 0 :(得分:6)
for(j=0;j<5-i;j++){
if(a[j]>a[j+1])
a[j+1]
是数组越界访问,这将导致未定义的行为
答案 1 :(得分:1)
尝试使用冒泡排序。在任何编译器中都不会妨碍机器循环。
for (i = 0; i < count -1; i++)
for (j = 0; j < ((count - 1) - i); j++) {
if (memptr[j]>memptr[j+1]) {
temp = memptr[j];
memptr[j] = memptr[j + 1];
memptr[j + 1] = temp;
}
}
答案 2 :(得分:0)
this.stop();
var mainStage = this;
var movieWidth = 640;//Choose Scaled Width of Video
var movieHeight = 360;//Choose Scaled Height of Video
var autoplayVideo = true;//Make it Autoplay
var vidya = document.createElement('video');//Creates the Video Element that is referenced later
var canvasEle = document.getElementById('canvas');//Identify the Canvas element
ctx = canvasEle.getContext('2d');//get canvas's context
canvasEle.parentNode.insertBefore(vidya, canvasEle);//insert video element before canvas element
vidya.setAttribute("src", "testing.mp4");//Place URL of the Video here (Local in this Example)
vidya.setAttribute("type","video/mp4");//What type of Video it is, Should be MP4
vidya.setAttribute("width", movieWidth);//scales the video to the width you had set above
vidya.setAttribute("controls","");//Turns on the default generic video controls
vidya.setAttribute("id","VIDEO");//gives the element an id for reference(Not Used yet)
if (autoplayVideo == true){ vidya.setAttribute("autoplay","");};
createjs.Ticker.addEventListener("tick", handleTick);
function handleTick(event){
ctx.drawImage(vidya, 30, 70, movieWidth, movieHeight);
console.log(ctx + " "+v);//here is where the uncaught reference is the "v"
}
因此,对于以下索引,您将获得以下内容:
int a[5]={1,2,0,5,4}
现在在你的外循环index ->| 0 | 1 | 2 | 3 | 4 |
content ->| 1 | 2 | 0 | 5 | 4 |
中,对于第一次迭代for(i=0;i<5;i++)
,内部循环破坏条件将是,
i = 0
,或者
for(j=0;j<5-i;j++)
因此for(j=0;j<5-0;j++)
的价值会增加j
到0
。
现在想想4
时会发生什么a[j+1]
!
您正尝试访问j = 4
或a[4+1]
,其中数组a[5]
的{{1}}定义为index
。
因此,在a
,您将获得Undefined behavior。
尝试:
4