排序1

1.冒泡排序
倒序,正序优化,正序
template <class Elem, class Comp>  
void bubsort(Elem A[], int n) { // Bubble Sort  
  for (int i=0; i<n-1; i++)     // Bubble up i'th record  
    for (int j=n-1; j>i; j--)  
      if (Comp::lt(A[j], A[j-1]))  
        swap(A, j, j-1);  
}  
//  int lowindex;  
//  for (int i = 0; i<n - 1; i++) {   // Select i'th record    
//      lowindex = 0;  
//      for (int j = 1; j<n - i ; j++) {   // Find the least value    
//          if (Comp::gt(A[j], A[lowindex])) {  
//              lowindex = j;           // Put it in place    
//          }  
//      }  
//      swap(A, n-i-1, lowindex);//不能用didSwap  
//  }  
//}  
template <class Elem, class Comp>  
void bubsort2(Elem A[], int n) { // Bubble Sort    
    for (int i = 0; i<n - 1; i++) {  
        for (int j = 0; j<n - i - 1; j++) {  
            if (A[j] > A[j + 1]) swap(A, j, j + 1);  
        }  
    }  
} 

2.选择排序(冒泡排序改善)
template <class Elem, class Comp>  
void selsort(Elem A[], int n) { // Selection Sort  
    bool didSwap;int lowindex;  
    for (int i = 0; i<n - 1; i++) {   // Select i'th record  
        didSwap = false;  
        lowindex = i;           // Remember its index  
        for (int j = n - 1; j>i; j--)   // Find the least value  
            if (Comp::lt(A[j], A[lowindex])) {  
                lowindex = j;           // Put it in place  
                didSwap = true;  
            }  
        if (didSwap == false)continue;  
        swap(A, i, lowindex);  
    }  
}  
//template <class Elem, class Comp>  
//void selsort3(Elem A[], int n) { // Selection Sort    
//  bool didSwap;int lowindex;  
//  for (int i = 0; i<n - 1; i++) {   // select i'th record    
//      didSwap = false;  
//      lowindex = i;           // remember its index    
//      for (int j = i + 1; j<n; j++) {   // find the least value    
//          if (Comp::lt(A[j], A[lowindex])) {  
//              lowindex = j;           // put it in place    
//              didSwap = true;  
//          }  
//      }  
//      if (didSwap == false)continue;  
//      swap(A, i, lowindex);  
//  }  
//}  
static void bubble_sort3(int unsorted[], int n)  
{  
    for (int i = 0; i < n - 1; i++)  
    {  
        for (int j = i + 1; j < n; j++)  
        {  
            if (unsorted[i] > unsorted[j])  //只能用i  
            {  
                int temp = unsorted[i];  
                unsorted[i] = unsorted[j];  
                unsorted[j] = temp;  
            }  
        }  
    }  
} 

3.插入排序
template <class elem, class comp>  
void inssort(elem a[], int n) { // insertion sort  
  for (int i=0; i<n-1; i++)       // insert i'th record  
    for (int j=i+1; (j>0) && (comp::lt(a[j], a[j-1])); j--)  
      swap(a, j,j-1);  
}  
  
template <class Elem, class Comp>  
void inssort(Elem A[], int n) { // Inserttion Sort  
    for (int i = 1; i < n; i++)    //从第2个数据开始插入    
    {  
        int j = 0;  
        while (j < i && A[j] <= A[i])    //寻找插入的位置    
            j++;  
  
        if (j < i)    //i位置之前,有比pDataArray[i]大的数,则进行挪动和插入    
        {  
            int k = i;  
            int temp = A[i];  
            while (k > j)    //挪动位置    
            {  
                A[k] = A[k - 1];  
                k--;  
            }  
            A[k] = temp;    //插入    
        }  
    }  
}

4.希尔排序
a)将序列分成子序列,分别对子序列排序
b)再用插入排序完成最后排序工作
// Modified version of Insertion Sort  
template <class Elem, class Comp>  
void inssort2(Elem A[], int n, int from, int incr)   
{  
  for (int i= from + incr; i<n; i+=incr)  
    for (int j=i; j> from && Comp::lt(A[j],A[j-incr]); j-=incr)  
        swap(A, j, j-incr);  
}  
  
template <class Elem, class Comp>  
void shellsort(Elem A[], int n) { // Shellsort  
  for (int i=n/2; i>=1; i/=2)  // For each incr  
    for (int j=0; j<i; j++)   // Sort sublists  
      inssort2<Elem,Comp>(A, n, j, i);     
}  
  
// Modified version of Insertion Sort for varying increments  
//template <class Elem, class Comp>  
//void inssort2(Elem A[], int n, int incr) {  
//  for (int i=incr; i<n; i+=incr)  
//    for (int j=i;  
//         (j>=incr) && (Comp::lt(A[j], A[j-incr])); j-=incr)  
//      swap(A, j, j-incr);  
//}  
//  
//template <class Elem, class Comp>  
//void shellsort(Elem A[], int n) { // Shellsort  
//  for (int i=n/2; i>2; i/=2)      // For each increment  
//    for (int j=0; j<i; j++)       // Sort each sublist  
//      inssort2<Elem,Comp>(&A[j], n-j, i);  
//  inssort2<Elem,Comp>(A, n, 1);  
//}  

a)
innsort(A,16,0,4)

innsort(A,16,1,4)

innsort(A,16,2,4)

innsort(A,16,3,4)


b)
innsort(A,16,0,4)执行过程




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值