Random pivot quicksort in Java [duplicate]

做~自己de王妃 提交于 2019-12-03 14:11:10

You can use the Random class from java:

Random rand = new Random();
int num = begin_sub_array + rand.nextInt(end_sub_array - begin_sub_array);

This will generate a value from beginning of the sub array (begin_sub_array) to end of the sub array (end_sub_array). You just have to take the variable num, and pass as the pivot in your quicksort algorithm.

int pivot = list[num];

Use int rand = (int) (st + (Math.random()*((end-st)+1))); Below is the code

private E[] tofind;
private int sameCounter=0;
private int comparisions;
public void quickSort()
{
    quickInnerSort(0, tofind.length-1);
    System.out.println("jatin");
}
    /**
 * 
 * @param st index of the starting point of the array
 * @param end index of the ending point of the array
 * @param k rank of the element to be found out
 */
private void quickInnerSort(int st, int end)
{
    if(st>end)   
        return;

    //printWithComparisions();
    int cut = partition(st,end);        
    //check k lies in which partition

    quickInnerSort(st,cut-1);
    quickInnerSort(cut+1,end);


}

/**
 * 
 * @param st index of the array from where to partition
 * @param end index of the array till where to partition
 * @return index of the random number chosen to calculate
 */
private int partition(int st, int end)
{

    int rand = (int) (st + (Math.random()*((end-st)+1)));
    //System.out.println("rand ="+tofind[rand]+" index at "+rand);
    E x = tofind[rand];
    sameCounter=0;
    swap(end,rand);
    E x = tofind[end];
    int i = st-1;
    for(int j=st;j<=end-1;j++)
    {
        if(tofind[j].compareTo(x)==0)
            sameCounter++;
        if(tofind[j].compareTo(x)<0)
        {
            i = i+1;
            swap(i,j);
        }

    }
    swap(i+1,end);        
    return i+1;
}

Try something like this:-

  int pivot = arr[left + rnd.nextInt(right - left)]; //rnd is a class Random object, which you could set as a private static final field.

  import java.util.Random;


  public class QuickSort {

/**
 * @param args
 */
public static void main(String[] args) {

      int i;
      int array[] = {10,9,1,2,3,4,100,200,300,400};  
      System.out.println(" Quick Sort\n\n");
      System.out.println("Values Before the sort:\n");
      for(i = 0; i < array.length; i++)
      System.out.print( array[i]+"  ");
      System.out.println();
      quickSort(array,0,array.length-1);
      System.out.print("Values after the sort:\n");
      for(i = 0; i <array.length; i++)
      System.out.print(array[i]+"  ");
      System.out.println(); 

}

public static int partition(int arr[], int left, int right)
{
      int i = left, j = right;
      int tmp;



      int pivot = arr[left + rnd.nextInt(right - left)];

      while (i <= j) {
            while (arr[i] < pivot)
                  i++;
            while (arr[j] > pivot)
                  j--;
            if (i <= j) {
                  tmp = arr[i];
                  arr[i] = arr[j];
                  arr[j] = tmp;
                  i++;
                  j--;
            }
      };

      return i;
}

public static void quickSort(int arr[], int left, int right) {
      int index = partition(arr, left, right);
      if (left < index - 1)
            quickSort(arr, left, index - 1);
      if (index < right)
            quickSort(arr, index, right);
}

 }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!