排序2

1.快排
i
void qsort(int array[], int l, int r){  
    if (l < r)  
    {  
        //Swap(array[l], array[(l + r) / 2]); //将中间的这个数和第一个数交换 参见注1  
        int i = l, j = r, x = array[l];  
        while (i < j)  
        {  
            while(i < j && array[j] >= x) // 从右向左找第一个小于x的数  
                j--;    
            if(i < j)   
                array[i++] = array[j];  
              
            while(i < j && array[i] < x) // 从左向右找第一个大于等于x的数  
                i++;    
            if(i < j)   
                array[j--] = array[i];  
        }  
        array[i] = x;  
        qsort(array, l, i - 1); // 递归调用   
        qsort(array, i + 1, r);  
    }  
}  
  
//int AdjustArray(int array[], int l, int r) //返回调整后基准数的位置  
//{  
//  int i = l, j = r;  
//  int x = array[l]; //array[l]即array[i]就是第一个坑  
//  while (i < j)  
//  {  
//      // 从右向左找小于x的数来填array[i]  
//      while(i < j && array[j] >= x)   
//          j--;    
//      if(i < j)   
//      {  
//          array[i] = array[j]; //将array[j]填到array[i]中,array[j]就形成了一个新的坑  
//          i++;  
//      }  
//  
//      // 从左向右找大于或等于x的数来填array[j]  
//      while(i < j && array[i] < x)  
//          i++;    
//      if(i < j)   
//      {  
//          array[j] = array[i]; //将array[i]填到array[j]中,array[i]就形成了一个新的坑  
//          j--;  
//      }  
//  }  
//  //退出时,i等于j。将x填到这个坑中。  
//  array[i] = x;  
//  
//  return i;  
//}  
//  
//void qsort1(int array[], int l, int r)  
//{  
//  if (l < r)  
//    {  
//      int i = AdjustArray(array, l, r);//先成挖坑填数法调整array[]  
//      qsort1(array, l, i - 1); // 递归调用   
//      qsort1(array, i + 1, r);  
//  }  
//}  

ii
template <class Elem, class Comp>  
int partition(Elem A[], int l, int r, Elem& pivot) {  
  do {             // Move the bounds inward until they meet  
    while (Comp::lt(A[++l], pivot));     // Move l right and  
    while ((r != 0) && Comp::gt(A[--r], pivot)); // r left  
    swap(A, l, r);              // Swap out-of-place values  
  } while (l < r);              // Stop when they cross  
  swap(A, l, r);                // Reverse last, wasted swap  
  return l;      // Return first position in right partition  
}  
  
template <class Elem, class Comp>  
void qsort(Elem array[], int i, int j) {  
  if ((j-i) <= THRESHOLD) return; // Don't sort short list  
  int pivotindex = findpivot(array, i, j);  
  swap(array, pivotindex, j); // stick pivot at end  
  int k = partition<Elem,Comp>(array, i-1, j, array[j]);  
  swap(array, k, j);          // Put pivot value in place  
  qsort<Elem,Comp>(array, i, k-1);  
  qsort<Elem,Comp>(array, k+1, j);  
}  

iii
template <class Elem, class Comp>  
void qsort(Elem array[], int i, int j) {  
  static int stack[200];  
  int top = -1;  
  Elem pivot;  
  int pivotindex, l, r;  
  
  stack[++top] = i;  
  stack[++top] = j;  
  
  while (top > 0) {  
    // Pop stack  
    j = stack[top--];  
    i = stack[top--];  
  
    // Findpivot  
    pivotindex = (i+j)/2;  
    pivot = array[pivotindex];  
    swap(array, pivotindex, j); // stick pivot at end  
  
    // Partition  
    l = i-1;  
    r = j;  
    do {  
      while (Comp::lt(array[++l], pivot));  
      while (r && Comp::gt(array[--r], pivot));  
      swap(array, l, r);  
    } while (l < r);  
    swap(array, l, r);          // Undo final swap  
    swap(array, l, j);          // Put pivot value in place  
  
    // Load up stack.  l is pivot point.  
    if ((l-1-i) > THRESHOLD) {  
      stack[++top] = i;  
      stack[++top] = l-1;  
    }  
    if ((j-l-1) > THRESHOLD) {  
      stack[++top] = l+1;  
      stack[++top] = j;  
    }  
  }  
} 

2.归并
template <class Elem, class Comp>  
void mergesort(Elem A[], Elem temp[], int left, int right) {  
  if ((right-left) <= THRESHOLD) { // Small list  
    inssort<Elem,Comp>(&A[left], right-left+1);  
    return;  
  }  
  int mid = (left+right)/2;  
  if (left == right) return;  
  mergesort<Elem,Comp>(A, temp, left, mid);  
  mergesort<Elem,Comp>(A, temp, mid+1, right);  
  
  int i, j, k;  
  //  First, copy 2 halves to temp.  
  for (i=mid; i>=left; i--) temp[i] = A[i];  
  for (j=1; j<=right-mid; j++) temp[right-j+1] = A[j+mid];  
  //Do the merge sublists back to A  
  for (i=left,j=right,k=left; k<=right; k++)  
    if (temp[i] < temp[j]) A[k] = temp[i++];  
    else A[k] = temp[j--];  
}  
  
//template <class Elem, class Comp>  
//void mergesort(Elem A[], Elem temp[], int left, int right) {  
//  int mid = (left+right)/2;  
//  if (left == right) return;        // List of one element  
//  mergesort<Elem,Comp>(A, temp, left, mid);  
//  mergesort<Elem,Comp>(A, temp, mid+1, right);  
//  
//  int i1 = left; int i2 = mid + 1;  
//  // Copy subarray to temp  
//  for (int i=left; i<=right; i++) temp[i] = A[i];  
//  // Do the merge operation back to A  
//  for (int curr=left; curr<=right; curr++) {  
//    if (i1 == mid+1)      // Left sublist exhausted  
//      A[curr] = temp[i2++];  
//    else if (i2 > right)  // Right sublist exhausted  
//      A[curr] = temp[i1++];  
//    else if (Comp::lt(temp[i1], temp[i2]))  
//      A[curr] = temp[i1++];  
//    else A[curr] = temp[i2++];  
//  }  
//}  

mergesort1

mergesort2


评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值