算法 排序 冒泡排序

void BubbleSort(SeqList R)

    
// R(l..n)是待排序的文件,采用自下向上扫描,对R做冒泡排序
    int i,j;
    
bool exchangeFlag;    // 交换标志
    for(i = 1; i < n; i++)
    

        
// 最多做n-1趟排序
        exchangeFlag = false;    // 本趟排序开始前
        for(j = n; j > i; j--)    // 对当前无序区R[i..n]自下向上扫描
        {
            
if(R[j].key < R[j - 1].key)
            
{
                
// 交换
                R[temp] = R[j];    // R[temp]仅做暂存单元
                R[j] = R[j - 1];
                R[j] 
= R[temp];
                exchangeFlag 
= true// 发生交换
            }

        }

        
if(!exchange)    // 本趟排序未发生交换,提前终止算法
        {
            
return;
        }

    }
 // endfor(外循环)
}
 // BubbleSort  

void BubbleSort(int[] x)
{  
    
for(int i = 0; i < x.Length - 1; i++)
    
{
        
for(int j = x.Length - 1; j > i; j--)
        
{
            
if(x[j] < x[j - 1])
            
{
                
int temp;
                temp 
= x[j];
                x[j] 
= x[j - 1];
                x[j 
- 1= temp;
            }

        }

    }

}
原文地址:https://www.cnblogs.com/xiaodi/p/296437.html