1.基本版:
<span style="font-family:Microsoft YaHei;font-size:12px;">i</span><span style="font-family:Microsoft YaHei;font-size:14px;">nt tempBubble(int *aiv, int Hi){
int sorted = true;
for (int i = 1; i != Hi; i++){
if (aiv[i - 1]>aiv[i]){
sorted = false;
swap(aiv[i - 1], aiv[i]);
}
}
return sorted;
}
void bubbleSort(int *aiv, int N)
{
while (!tempBubble(aiv, N--));
}</span>
2.改进版:
就是若中间有重复的,跳过而不是步进.
<span style="font-family:Microsoft YaHei;"><span style="font-size:14px;">template<typename T>
int tempBubbleEvolved(T *aiv, int Hi){//----进化版冒泡
int Last = 1;
for (int i = 1; i != Hi; i++){
if (aiv[i - 1]>aiv[i]){
Last = i;
swap(aiv[i - 1], aiv[i]);
}
}
return Last;
}
template<typename T>
void bubbleSortEvolved(T *aiv, int N)
{
/*int Last=1;
int Hi = N;*/</span>
//do
//{
// Last = tempBubbleEvolved(aiv, Hi);
// Hi = Last;
//} while (Last != 1);
while (1 !=(N = tempBubbleEvolved(aiv, N)));
}</span>