冒泡排序C语言
BubbleSorting C language
我们正在学习数组,我刚接触冒泡排序。
我写了下面的代码来按升序对数组进行排序但是有问题。
我找不到,但我知道有问题。
我找到了正确的代码,但我仍然不明白为什么这不起作用。
我的代码
int a;
int i;
int temp;
int value[5] = { 5, 4, 3, 2, 1 };
for (i = 0; i < 5; i++) {
if (value[i] > value[i + 1]) {
temp = value[i];
value[i] = value[i + 1];
value[i + 1] = temp;
}
}
for (a = 0; a < 5; a++) {
printf(" %d ", value[i]);
}
当 i == 4
您使用 value[i+1]
访问位置 5 而它不是您的。
您正在访问未保留的内存。
您的代码有多个问题,
First And Foremost 在使用 value[i]
进行排序后,您正在打印数组元素,并且您正在使用变量 a
进行 运行ning 循环。
for(a=0;a<5;a++){ //You Are Incrementing a
printf(" %d ",value[i]); //But Using i here , change it to a.
//As It will just print the value[i] member
}
您正在访问 value[5]
,这不是您的。
for(i=0;i<5;i++){ //Here In The Last Loop value[4] is compared with value[5],
// value[5] is not defined
//for(i=0;i<4;i++)
//Instead Run Loop Till i<4 , I guess this is what you
//wanted but accidently made a mistake.
if(value[i]>value[i+1])
最大的问题,是你还没有完全理解Bubblesort。
在冒泡排序中,循环 运行 多次,直到循环中没有成员可以交换,即停止循环。
这就是维基百科Says
Bubble sort, sometimes referred to as sinking sort, is a simple sorting algorithm that repeatedly steps through the list to be sorted, compares each pair of adjacent items and swaps them if they are in the wrong order. The pass through the list is repeated until no swaps are needed, which indicates that the list is sorted. The algorithm, which is a comparison sort, is named for the way smaller or larger elements "bubble" to the top of the list. Although the algorithm is simple, it is too slow and impractical for most problems even when compared to insertion sort.It can be practical if the input is usually in sorted order but may occasionally have some out-of-order elements nearly in position.
请参阅下面的演示文稿以了解冒泡排序如何 Works.See 循环 运行 一次又一次,直到没有成员可以交换。
Step-by-step example
Let us take the array of numbers "5 1 4 2 8", and sort the array from lowest number to greatest number using bubble sort. In each step, elements written in bold are being compared. Three passes will be required.
First Pass
( 5 1 4 2 8 ) to ( 1 5 4 2 8 ), Here algorithm compares the first 2 elements,and swap since 5 > 1.
( 1 5 4 2 8 ) to ( 1 4 5 2 8 ), Swap since 5 > 4
( 1 4 5 2 8 ) to ( 1 4 2 5 8 ), Swap since 5 > 2
( 1 4 2 5 8 ) to ( 1 4 2 5 8 ), Now, since these elements are already in order (8 > 5), algorithm does not swap them.
Second Pass
( 1 4 2 5 8 ) to ( 1 4 2 5 8 )
( 1 4 2 5 8 ) to ( 1 2 4 5 8 ), Swap since 4 > 2
( 1 2 4 5 8 ) to ( 1 2 4 5 8 )
( 1 2 4 5 8 ) to ( 1 2 4 5 8 )
Now, the array is already sorted, but the algorithm does not know if it is completed. The algorithm needs one whole pass without any swap to know it is sorted.
Third Pass
( 1 2 4 5 8 ) to ( 1 2 4 5 8 )
( 1 2 4 5 8 ) to ( 1 2 4 5 8 )
( 1 2 4 5 8 ) to ( 1 2 4 5 8 )
( 1 2 4 5 8 ) to ( 1 2 4 5 8 )
所以你需要 运行 循环多次直到没有成员可以交换,你可以通过使用一个新变量 count
来做到这一点,最初初始化为 1
开始,在每个循环开始之前,它被初始化为0
,如果在循环中,Swap语句执行然后,它变为1
,再次执行循环,因为计数是[=16] =],如果在最后一次循环中,它的值不会更改为 1
,因为现在所有成员都已排序,所以循环不会再次 运行。
我们正在学习数组,我刚接触冒泡排序。
我写了下面的代码来按升序对数组进行排序但是有问题。
我找不到,但我知道有问题。
我找到了正确的代码,但我仍然不明白为什么这不起作用。
我的代码
int a;
int i;
int temp;
int value[5] = { 5, 4, 3, 2, 1 };
for (i = 0; i < 5; i++) {
if (value[i] > value[i + 1]) {
temp = value[i];
value[i] = value[i + 1];
value[i + 1] = temp;
}
}
for (a = 0; a < 5; a++) {
printf(" %d ", value[i]);
}
当 i == 4
您使用 value[i+1]
访问位置 5 而它不是您的。
您正在访问未保留的内存。
您的代码有多个问题,
First And Foremost 在使用
value[i]
进行排序后,您正在打印数组元素,并且您正在使用变量a
进行 运行ning 循环。for(a=0;a<5;a++){ //You Are Incrementing a printf(" %d ",value[i]); //But Using i here , change it to a. //As It will just print the value[i] member }
您正在访问
value[5]
,这不是您的。for(i=0;i<5;i++){ //Here In The Last Loop value[4] is compared with value[5], // value[5] is not defined //for(i=0;i<4;i++) //Instead Run Loop Till i<4 , I guess this is what you //wanted but accidently made a mistake. if(value[i]>value[i+1])
最大的问题,是你还没有完全理解Bubblesort。 在冒泡排序中,循环 运行 多次,直到循环中没有成员可以交换,即停止循环。
这就是维基百科Says
Bubble sort, sometimes referred to as sinking sort, is a simple sorting algorithm that repeatedly steps through the list to be sorted, compares each pair of adjacent items and swaps them if they are in the wrong order. The pass through the list is repeated until no swaps are needed, which indicates that the list is sorted. The algorithm, which is a comparison sort, is named for the way smaller or larger elements "bubble" to the top of the list. Although the algorithm is simple, it is too slow and impractical for most problems even when compared to insertion sort.It can be practical if the input is usually in sorted order but may occasionally have some out-of-order elements nearly in position.
请参阅下面的演示文稿以了解冒泡排序如何 Works.See 循环 运行 一次又一次,直到没有成员可以交换。
Step-by-step example
Let us take the array of numbers "5 1 4 2 8", and sort the array from lowest number to greatest number using bubble sort. In each step, elements written in bold are being compared. Three passes will be required.
First Pass
( 5 1 4 2 8 ) to ( 1 5 4 2 8 ), Here algorithm compares the first 2 elements,and swap since 5 > 1.
( 1 5 4 2 8 ) to ( 1 4 5 2 8 ), Swap since 5 > 4
( 1 4 5 2 8 ) to ( 1 4 2 5 8 ), Swap since 5 > 2
( 1 4 2 5 8 ) to ( 1 4 2 5 8 ), Now, since these elements are already in order (8 > 5), algorithm does not swap them.Second Pass
( 1 4 2 5 8 ) to ( 1 4 2 5 8 )
( 1 4 2 5 8 ) to ( 1 2 4 5 8 ), Swap since 4 > 2
( 1 2 4 5 8 ) to ( 1 2 4 5 8 )
( 1 2 4 5 8 ) to ( 1 2 4 5 8 )Now, the array is already sorted, but the algorithm does not know if it is completed. The algorithm needs one whole pass without any swap to know it is sorted.
Third Pass
( 1 2 4 5 8 ) to ( 1 2 4 5 8 )
( 1 2 4 5 8 ) to ( 1 2 4 5 8 )
( 1 2 4 5 8 ) to ( 1 2 4 5 8 )
( 1 2 4 5 8 ) to ( 1 2 4 5 8 )
所以你需要 运行 循环多次直到没有成员可以交换,你可以通过使用一个新变量 count
来做到这一点,最初初始化为 1
开始,在每个循环开始之前,它被初始化为0
,如果在循环中,Swap语句执行然后,它变为1
,再次执行循环,因为计数是[=16] =],如果在最后一次循环中,它的值不会更改为 1
,因为现在所有成员都已排序,所以循环不会再次 运行。