while语句中的运算符
大家最熟悉的应该有 || &&吧。
但还有一种运算符,是 ‘ , ’。
笔者用快排验证这三者之间的区别。
#include<bits/stdc++.h>
#define FOR(i,n) for(int i=0;i<n;++i)
void Quicksort(int r[], int low, int high) {
int temp;
int i = low; int j = high;
if (low < high) {
temp = r[low];
while (i < j) {
while (j > i&&r[j] >= temp)--j;
if (i < j) {
r[i] = r[j];
++i;
}
while (i < j&&r[i] < temp)++i;
if (i < j) {
r[j] = r[i];
--j;
}
}
r[i] = temp;
Quicksort(r, low, i - 1);
Quicksort(r, i + 1, high);
}
}
int main(void) {
cout << "快速排序:\n";
int e[8] = { 9,7,8,5,3,2,6,11 };
Quicksort(e, 0,7);
FOR(i, 8)
cout << e[i] << ' ';
return 0;
}
以上为正确的快排代码
运行结果如下:
但当将while语句中的 &&运算符 换成‘ ,’运算符
即
while (j > i&&r[j] >= temp)–j;
while (i < j&&r[i] < temp)++i;
更改为
while (j > i , r[j] >= temp)–j;
while (i < j , r[i] < temp)++i;
时
运行结果如下:
可见 逗号运算符与&&运算符在while语句中不等价,也不与||等价(笔者已验证过),但逗号运算符究竟起的什么作用,欢迎各位朋友在下面留言!感谢!