Algorithm 以中间元素为中心的快速排序算法

Algorithm 以中间元素为中心的快速排序算法,algorithm,pivot,quicksort,Algorithm,Pivot,Quicksort,我不得不做这个小任务,像快速排序一样“手工”排序数组。有一个特定点(递归点),我不确定我的解决方案是否正确 正确的。如果有人能看穿这个就好了 初始顺序:7 4 6 8 9 1 3 2 解决方案(i=左索引,x=轴,j=右索引): [枢轴指数=(i+j)/2] 使用的代码: public class QuickSort { public static void sort (int[] a) { // public method quicksort(a, 0, a.lengt

我不得不做这个小任务,像快速排序一样“手工”排序数组。有一个特定点(递归点),我不确定我的解决方案是否正确 正确的。如果有人能看穿这个就好了

初始顺序:7 4 6 8 9 1 3 2

解决方案(i=左索引,x=轴,j=右索引): [枢轴指数=(i+j)/2]

使用的代码:

public class QuickSort {
  public static void sort (int[] a) {       // public method
    quicksort(a, 0, a.length-1);            // starts private method
  }
  private static void quicksort (int[] a, int left, int right) {
    int tmp;                                // temporary param
    int i = left;                           // left index
    int j = right;                          // right index
    int middle = (left + right) / 2;        // middle position
    int x = a[middle];                      // pivot element
    do {
      while (a[i] < x) i++;                 // x works as break
      while (a[j] > x) j--;                 // x works as break
        if (i <= j) {
          tmp = a[i];                       // temporary storage
          a[i] = a[j];                      // a[i] and
          a[j] = tmp;                       // a[j] get swapped
          i++;
          j--;
        }
    } while (i <= j);
                                            // all elements on the left side of the array are smaller than
                                            // all elements in the right side of the array
    if (left < j) quicksort(a, left, j);    // sort left side
    if (i < right) quicksort(a, i, right);  // sort right side
  }
}
公共类快速排序{
公共静态void排序(int[]a){//public方法
快速排序(a,0,a.length-1);//启动私有方法
}
私有静态void快速排序(int[]a,int左,int右){
int tmp;//临时参数
int i=left;//左索引
int j=right;//右索引
int middle=(左+右)/2;//中间位置
int x=a[middle];//pivot元素
做{
而(a[i]x)j--;//x用作break

如果(我觉得这个网站并不是为了让人们检查你的作业。问题出在哪里?是我自己做的,只是想寻求帮助?!这个网站是为了什么?专业人士与专业人士谈论专业内容?如果是这样,很抱歉问!代码在哪里,为什么qsort或std::sort不能做?别担心,sizzle,我没有标记你或任何东西。我只是想给你一个提示,你可能会在这个问题上收到一些回击,因为这对这个网站来说有点不寻常。除了少数例外,这个网站上广受欢迎的问题应该是关于特定代码的特定问题,并有明确的答案。尽管如此,发布这些问题并没有坏处他在这里,但是@sizzle为什么不把你的问题发布到codereview.stackexchange.com?
public class QuickSort {
  public static void sort (int[] a) {       // public method
    quicksort(a, 0, a.length-1);            // starts private method
  }
  private static void quicksort (int[] a, int left, int right) {
    int tmp;                                // temporary param
    int i = left;                           // left index
    int j = right;                          // right index
    int middle = (left + right) / 2;        // middle position
    int x = a[middle];                      // pivot element
    do {
      while (a[i] < x) i++;                 // x works as break
      while (a[j] > x) j--;                 // x works as break
        if (i <= j) {
          tmp = a[i];                       // temporary storage
          a[i] = a[j];                      // a[i] and
          a[j] = tmp;                       // a[j] get swapped
          i++;
          j--;
        }
    } while (i <= j);
                                            // all elements on the left side of the array are smaller than
                                            // all elements in the right side of the array
    if (left < j) quicksort(a, left, j);    // sort left side
    if (i < right) quicksort(a, i, right);  // sort right side
  }
}