簡體   English   中英

我不確定我的快速選擇方法有什么問題

[英]I am not sure what is wrong with my quick select method

所以我嘗試編寫一個代碼來使用quick sort在給定的輸入integer數組中選擇第 k 個最小的元素,但由於某些原因,您可以在下面的代碼中看到,

public static int partition(int[] input, int first, int end) {
    int pivot = input[(first + end)/2];
    int i = first - 1;
    int j = end + 1;

    while (true) {

      do {
        i++;
      } while (input[i] < pivot);

      do {
        j--;
      } while (input[j] > pivot);

      if (i < j)
        swap(input, i, j);
      else
        return j;
    }
  }

  public static void swap(int[] input, int i, int j){
    int temp = input[i];
    input[i] = input[j];
    input[j] = temp;
  }

  public static int select(int[] input, int k){
    return mySelect(input, 0, input.length-1, k);
  }

  public static int mySelect(int[] input, int left, int right, int k){ 
    // If k is smaller than number of elements in array 
    if (k > 0 && k <= right - left + 1) { 

      int pos = partition(input, left, right); 

      if (pos - left == k - 1) 
        return input[pos]; 

      // If position is larger, recursive call on the left subarray 
      if (pos - left > k - 1)  
        return mySelect(input, left, pos-1, k); 

      // if smaller, recursive call on the right subarray 
      return mySelect(input, pos+1, right, k-pos+left-1); 
    } 

    System.out.println("Invalid k value");
    return Integer.MAX_VALUE; 
  }

public static void main(String[] args){
    test2 = new int[]{99, 44, 77, 22, 55, 66, 11, 88, 33};
    int[] test2 = new int[]{99, 44, 77, 22, 55, 66, 11, 88, 33};
    //testing for selecting kth min 
    System.out.println("The 1st smallest : " + select(test2, 1));
    System.out.println("The 2nd smallest : " + select(test2, 2));
    System.out.println("The 3rd smallest : " + select(test2, 3));
    System.out.println("The 4th smallest : " + select(test2, 4));
    System.out.println("The 6th smallest : " + select(test2, 6));
    System.out.println("The 9th smallest : " + select(test2, 9)); 
    }

但是我的第一個最小元素出現 22,第二個最小元素返回 11,而其他值是正常的。 有人可以幫我找出我犯了什么錯誤嗎?

您的代碼中的問題出在分區中。 do while是這里的罪魁禍首。 您在檢查條件之前更新頭寸,這導致了last交換操作的問題。

將您的方法更新為此,您會很高興

public static int partition(int[] input, int first, int end) {
    int pivot = input[(first + end)/2];
    int i = first;
    int j = end;

    while (true) {

      while (input[i] < pivot) {
        i++;
      }

      while (input[j] > pivot) {
        j--;
      }

      if (i < j)
        swap(input, i, j);
      else
        return j;
    }
}

我在學習快速排序時編寫了這段代碼。 我犯的錯誤是沒有意識到

'left' 和 'right' 是數組中的索引,而,

'pivot' 是存儲在數組中的值之一

你可以研究我下面的代碼,確定你對算法的理解有什么問題!!

public class QuickSort
{   
    public static void main(String[] args)
    {
        int[] nums = {1,5,9,2,7,8,4,2,5,8,9,12,35,21,34,22,1,45}; 

        quickSort(0,nums.length-1,nums);
        //print nums to see if sort is working as expected,omit printing in your case
        print(nums);
        //now you can write code to select kth smallest number from sorted nums here
    }

    /**
     * left and right are indices in array whereas
     * pivot is one of the values stored in array
     */
    static int partition(int left, int right , int pivot, int[] nums)
    {
        int leftIndex = left -1;
        int rightIndex = right;
        int temp = 0;
        while(true)
        {
            while( nums[++leftIndex] < pivot );
            while( rightIndex>0 && nums[--rightIndex] > pivot );

            if( leftIndex >= rightIndex ) break;
            else//swap value at leftIndex and rightIndex
            {
                temp = nums[leftIndex];
                nums[leftIndex]= nums[rightIndex];
                nums[rightIndex] = temp;
            }
        }

        //swap value at leftIndex and initial right index
        temp = nums[leftIndex];
        nums[leftIndex]= nums[right];
        nums[right] = temp;

        return leftIndex;
    }

    static void quickSort( int leftIndex , int rightIndex ,int[] nums)
    {
        if( rightIndex-leftIndex <= 0 ) return;
        else
        {
            int pivot = nums[rightIndex];
            int partitionIndex = partition(leftIndex, rightIndex , pivot,nums);
            quickSort(leftIndex,partitionIndex-1,nums);
            quickSort(partitionIndex+1,rightIndex,nums);
        }
    }

    static void print(int[] nums)
    {
        for( int i = 0 ; i < nums.length ; i++ )
        {
            System.out.print(nums[i]+", ");
        }
    }

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM