⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 qsort3.cpp

📁 经典c++程序的实现
💻 CPP
字号:
#include <iostream.h>
#include "book.h"

typedef int ELEM;
typedef int KEY;
#include "swap.h"
  
extern long count1;
  
// Quicksort sort
struct qentry { int left, right; };

void qsort(ELEM* array, int i, int j) {
  struct qentry stack[100];
  int top = 0;
  KEY pivot;
  int pivotindex, l, r;

  stack[top].left = i;
  stack[top].right =j;

  while (top >= 0) {
    // Pop stack
    i = stack[top].left;
    j = stack[top--].right;

    // Findpivot
    pivotindex = (i+j)/2;
    pivot = array[pivotindex];
    swap(array[pivotindex], array[j]); // stick pivot at end

    // Partition
    l = i-1;
    r = j;
    do {
      while (key(array[++l]) < pivot);
      while (r && (key(array[--r]) > pivot));
      swap(array[l], array[r]);
    } while (l < r);
    swap(array[l], array[r]);  // Undo final swap
    swap(array[l], array[j]);  // Put pivot value in place

    // Load up stack
    if ((l-i) > 1) {
      stack[++top].left = i;
      stack[top].right = l-1;
    }
    if ((j-l) > 1) {
      stack[++top].left = l+1;
      stack[top].right = j;
    }
  }
}

void sort(ELEM* array, int listsize) {
  qsort(array, 0, listsize-1);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -