java排序方法中的比较在哪里? [英] Where are comparisons in java sorting methods?

查看:65
本文介绍了java排序方法中的比较在哪里?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题:每种单独的排序方法在哪里进行比较?
另外,如果您知道,请告诉我哪些方法计数数字错误以及将计数器放置在哪里.尝试了解排序方法在何处和多少次进行比较.

Question: Where are comparisons being made in each separate sorting method?
Also if you know please tell me which method count numbers are wrong and where to place my counters instead.trying to understand where and how many times sorting methods make comparisons.

Method        A        B   
Selection    4950     4950 
Bubble        99      9900 
Insertion     99      5049
Merge         712     1028
Shell         413      649
Quick        543      1041

好的,为了解释某些部分,基本上,数组A是从1到100的升序排列的数组.因此,这应该是最少的比较次数.
数组B降序为100-1.因此,我认为这应该是最大的比较次数.数组C只是随机生成的数字,因此每次都会更改.

Okay so to explain some parts, basically Array A is an array from 1-100 in ascending order. So this should be the minimum number of comparisons.
Array B is 100-1 in descending order. So I believe it should be the maximum number of comparisons. Array C is just randomly generated numbers, so it changes every time.

我觉得自己的选择和气泡排序得到了正确的计数.随时让我知道没有计算过的比较地点,或者如果我计算的比较有误.

I feel like my selection and bubble sorts were counted correctly. Feel free to let me know where comparisons are being made that I haven't counted, or if I'm counting wrong comparisons.

侧面说明:做了一些全局变量来计算在多个部分中递归的方法.

Side note: Made some global variable to count the methods that were recursive in multiple sections.

class Sorting
   {
      static int[] X = new int[100];
      static int mergecount = 0;
      static int quickcount = 0;
      public static void selectionSort(int list[])
      {
         int count = 0;
         int position = 0, n = list.length;
         for(int j = 0; j < n-1; j++)
         {
            position = j;
            for(int k = j+1; k < n; k++)
            {
               count++;
               if(list[k] < list[position])
                  position = k;
            }
            Swap(list, j, position);
         }
         System.out.println("counter" + count);
      }

  public static void Swap(int list[], int j, int k)
  {
     int temp = list[j];
     list[j] = list[k];
     list[k] = temp;
  }

  public static void bubbleSort(int list[])
  {
     int count = 0;
     boolean changed = false;
     do
     {
        changed = false;
        for(int j = 0; j < list.length - 1; j++)
        {
           count++;
           if(list[j] > list[j + 1])
           {
              Swap(list, j, j+1);
              changed = true;
           }
        }
     } while(changed);
     System.out.println("counter" + count);
  }

  public static void insertionSort(int list[])
  {
     int count = 0;
     for(int p = 1; p < list.length; p++)
     {
        int temp = list[p];
        int j = p;
        count++;
        for( ; j > 0 && temp < list[j - 1]; j = j-1)
        {
           list[j] = list[j - 1];
           count++;
        }
        list[j] = temp;
     }
     System.out.println("counter" + count);
  }

  public static void mergeSort(int list[])
  {
     mergeSort(list, 0, list.length - 1);
     System.out.println("counter" + mergecount);
  }

  public static void mergeSort(int list[], int first, int last)
  {
     if(first < last)
     {
        int mid = (first + last) / 2;
        mergeSort(list, first, mid);
        mergeSort(list, mid + 1, last);
        Merge(list, first, mid, last);
     }

  }

  public static void Merge(int list[], int first, int mid, int last)
  {
     int count = 0;
     int first1 = first, last1 = mid;
     int first2 = mid + 1, last2 = last;
     int temp[] = new int[list.length];
     int index = first1;

     while(first1 <= last1 && first2 <= last2)
     {
        if(list[first1] < list[first2])
        {
           temp[index] = list[first1++];
           mergecount++;
        }
        else
           temp[index] = list[first2++];
        index++;
        mergecount++;
     }

     while(first1 <= last1)
        temp[index++] = list[first1++];

     while(first2 <= last2)
        temp[index++] = list[first2++];

     for(index = first; index <= last; index++)
        list[index] = temp[index];


  }

  public static void shellSort(int list[])
  {
     int count = 0;
     int n = list.length;
     for(int gap = n / 2; gap > 0; gap = gap == 2 ? 1: (int) (gap/2.2))
        for(int i = gap; i < n; i++)
        {
           int temp = list[i];
           int j = i;
           count++;
           for( ; j >= gap && (temp < (list[j - gap])); j -= gap)
           {
              list[j] = list[j - gap];
              count++;
           }

           list[j] = temp;
        }
     System.out.println("counter" + count);
  }

  public static void quickSort(int start, int finish, int list[])
  {
     int count = 0;
     int left = start, right = finish, pivot, temp;
     pivot = list[(start + finish) / 2];
     do
     {
        while(list[left] < pivot)
        {
           left++;
           quickcount++;
        }

        while(pivot < list[right])
        {
           right--;
           quickcount++;
        }

        if(left <= right)
        {
           temp = list[left];
           list[left++] = list[right];
           list[right--] = temp;
           quickcount++;
        }
     }  while(left < right);

     if(start < right)
        quickSort(start, right, list);


     if(left < finish)
        quickSort(left, finish, list);

  }

  public static void copy(int list[])
  {
     for(int i = 0; i < list.length; i++)
        X[i] = list[i];
  }

  public static void restore(int list[])
  {
     for(int i = 0; i < list.length; i++)
        list[i] = X[i];
  }

  public static void displayArray(int list[])
  {
     for(int k = 0; k < list.length; k++)
        System.out.print(list[k] + " ");
     System.out.println();
  }

  public static void main(String args[])
  {
     int[] A = new int[100];
     for(int i = 0; i < A.length; i++)
        A[i] = i + 1;

     int[] B = new int[100];
     int q = 100;
     for(int i = 0; i < B.length; i++)
        B[i] = q--;

     int[] C = new int[100];
     for(int i = 0; i < C.length; i++)
        C[i] = (int)(Math.random() * 100 + 1); 

     displayArray(A);
     copy(A);
     selectionSort(A);
     displayArray(A);
     restore(A);
}

推荐答案

请注意,QuickSort性能在很大程度上取决于您对数据透视表的选择.对两个测试数组都进行排序(升/降),并且由于您选择的数据透视表为array [length/2],因此实际上总是在选择最佳的数据透视表.因此,您的测试用例B将不会为quicksort生成最大数量的比较.如果您选择array [0]作为枢轴,则将获得测试用例A和B的最大比较数.

Note that QuickSort performance is greatly influenced by your choice of the pivot. With both of your test arrays sorted (ascending / descending) and because you are picking pivot as array[length/2] you are actually always picking the best pivot. So your test case B won't generate maximum number of comparisons for quicksort. If you were picking array[0] as pivot you'd get maximum number of comparisons for test case A and B.

计数比较的最简单方法是使用比较功能并在其中进行.

The easiest way to count comparisons is to use a compare function and do it in there.

static int compareCount = 0;
int compareInt(int a, int b) {
    compareCount++;
    return a - b; // if 0 they are equal, if negative a is smaller, if positive b is smaller
}

现在,只需在所有算法中使用compareInt,您就会获得准确的计数.不过,您必须在每次运行之间重置compareCount.

Now just use compareInt in all your algorithms and you'll get an accurate count. You'll have to reset compareCount between each run though.

这篇关于java排序方法中的比较在哪里?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆