algorithm--General Array element selection problem (11)

Source: Internet
Author: User
Tags sort
algorithm--General array element selection problems


Introduction to the algorithm introduces a general problem of array element selection: In the case of an array a of different elements, select the element of which I is small (that is, if i=1, the smallest element is selected).

The pseudo-code of the algorithm is as follows, which uses the random Subarray partitioning method previously described in fast ordering:

Randomized-select (A, p, R, i)
    if p = = R
        return A [p]
    q = randomized-partition (A, p, r)
    k = q-p + 1
    I f i = = K
        return A[q]
    else if I < K
        return Randomized-select (A, p, q-1, i)
    else
        return randomized -select (A, q + 1, R, I-k)
The Java implementation of this algorithm is as follows:

	/** * * In the range of the given array subscript interval, according to the selected main element of the array * * @param A * To be sorted by the main array * @param p * Sub-array subscript left bounds * @param R * Sub-array subscript right boundary * @return The resulting sub-array demarcation subscript value */private int partition (int[] A, int p, int r) {int P
		Ivot = a[r];//pivot element int i = p-1; for (int j = p; J < R; J + +) {/////When pivot >=//a[j], the element labeled J is the element that needs to be exchanged with the element labeled I; the element below is the subscript for the elements that are larger than pivot;
			When an element is more than a pivot hour, this element is swapped with the previous element that is larger than the pivot element, so that elements smaller than pivot always appear to the left of the array and appear consecutively.
			if (A[j] <= pivot) {i++;//subscript is added 1, and after each 1, the current subscript I points to an element that is larger than pivot Exchange (A, I, J);
		}}//For loop at the end of this time the state of the array A[P...R] is://For any subscript k, if k in the [p,i] interval, there is always a[k] <= pivot;
		For any subscript k, if k is within the [i+1,j] interval, there is always a[k] > pivot;
		If subscript k = R, then a[r] = pivot;
	Exchange (A, i + 1, R),//Will a[i+1] with A[r], at which point the value of the element with a small far (i+1) subscript is less than or equal to pivot, and the element value greater than (i+1) subscript is greater than pivot return i + 1;

	}//For random sampling private random util = new Random (); /** * * Adding random sampling array Partitioning algorithm * * @param a * array to be sorted a * @param p * to be sortedSub-array subscript left boundary * @param R * Sub-array subscript right boundary with sort * @return get sub-array demarcation subscript value */public int randomizedpartition (int[] A, int p, int r) {int i = Util.nextint (r-p + 1)/* interval [0,r-p+1) */+ p;//randomly generate a subscript value between interval [P,r] Exchange (A, I, r);//Guaranteed Child The pivot element required for group partitioning is a return partition (A, p, R) that is randomly selected from A[P...R];//Call General sub-array partitioning function for sub-array}/** * A sort of algorithm to solve the selection problem: return array A (not Contains the same element) in the subscript range [P...R] within the small element of the I; I count from 1, I=1 is the smallest element in select A[P...R] * */public int randomizedselect (int[] A, int p, int r, int i) {if (P = = r) Return a[p];//if there is only one element in the specified subscript range, then the current element is the required small i element int q = randomizedpartition (A, p, r);//Use random number of children Group partitioning method, with the aid of a random main element to the array of molecules (may be empty); the subscript of the returned Q is the subscript of the current main element//after dividing the sub-array in the previous step, A[P...R] can actually be considered to be divided into three sub-array intervals: A[p...q-1] =< A[q] < a[q +1...R]; At this time the main element a[q] in the entire sub-array A[P...R] is already well-ordered int k = q-p + 1;//to calculate the number of elements in the sub-array a[p...q] if (i = = k)//If i==k, then the main element A[q] is to choose
		The small element of I, which is returned directly at this time, otherwise the next two steps determine whether the element to be selected is in the Subarray a[p...q-1] or A[Q+1...R] [return a[q]; else if (I < K)//i < K, indicating that the element to be selected is in the sub-array a[p...q-1], and then continue to recursively find the return Randomizedselect (A, p, q-1, i);
			else//i > K, stating that the element to be selected is in the sub-array A[Q+1...R], and then continues to search recursively; Return Randomizedselect (A, q + 1, R, i-k);//Also note that at this point we know that the elements in A[P...Q] (total k) are smaller than the element to be selected, then in the Subarray A[Q+1...R], the element to be selected should be the first (i-k) Small.}
The complete test code is:

public class Sortalgor {public static void main (string[] args) {int[] A = {2, 5, 10, 8, 9, 12, 15, 4};
		Sortalgor algorithm = new Sortalgor ();  
	 System.out.println (Algorithm.randomizedselect (A, 0, A.length-1, 3));//returns 5}/** * * Within the range of the given array subscript interval, based on the selected primary element of the numerator array * * @param A * The main array to be sorted * @param the left boundary of the subscript of the p * sub-array * @param the right boundary of the subscript of the R * Sub-array * @ret Urn gets sub-array demarcation subscript value */private int partition (int[] A, int p, int r) {int pivot = a[r];//pivot element int i = P-1
		; for (int j = p; J < R; J + +) {/////When pivot >=//a[j], the element labeled J is the element that needs to be exchanged with the element labeled I; the element below is the subscript for the elements that are larger than pivot;
			When an element is more than a pivot hour, this element is swapped with the previous element that is larger than the pivot element, so that elements smaller than pivot always appear to the left of the array and appear consecutively.
			if (A[j] <= pivot) {i++;//subscript is added 1, and after each 1, the current subscript I points to an element that is larger than pivot Exchange (A, I, J);
		}}//For loop at the end of this time the state of the array A[P...R] is://For any subscript k, if k in the [p,i] interval, there is always a[k] <= pivot;
		For any subscript k, if k is within the [i+1,j] interval, there is always a[k] > pivot;
		If subscript k = R, then a[r] = pivot; Exchange (A, i + 1, r);//Will a[I+1] with A[r], at which point the value of the element with a small far (i+1) subscript is less than or equal to pivot, and the element value greater than (i+1) subscript is greater than pivot return i + 1;

	}//For random sampling private random util = new Random ();
	 /** * * Adding an array partitioning algorithm for random sampling * * @param a * array to be sorted a * @param p * Sub-array subscript left boundary to be sorted * @param r * with sorted sub-array subscript right boundary * @return get sub-array demarcation subscript value */public int randomizedpartition (int[] A, int p, int r) {int i = util.nextint (r-p + 1)/* interval [0,r-p+1) */+ p;//randomly generates a subscript value between interval [P,r] Exchange (A, I, r);//The pivot element required to ensure sub-array partitioning is return partition (A, p, r) selected from A[P...R];//Call regular sub-array partitioning functions for sub-arrays}/** * A divide-and-conquer algorithm to solve the selection problem: Returns the array a (does not contain the same element) in the subscript range [P...R] 
			I small element; I count from 1, i=1 the smallest element in select A[P...R] * */public int randomizedselect (int[] A, int p, int r, int i) {if (P = = r) Return a[p];//If there is only one element within the specified subscript range, then the current element is the required small i element int q = randomizedpartition (A, p, r);//using a random subarray partitioning method, a random principal element is used to draw a molecular array (which can be can be empty); The index of the returned Q is the subscript of the current principal//after the sub-array is divided in the previous step, A[P...R] can actually be considered as being divided into three sub-array intervals: A[p...q-1] =< a[q] < A[Q+1...R]; At this time the main element a[q]
In the entire Subarray A[P...R] is already in order.		int k = q-p + 1;//computes the number of elements in the sub-array a[p...q] if (i = = k)//If the i==k, then the main element of the a[q] is to select the small elements of I, this time directly return; otherwise the following two steps will determine the element to be selected in the sub-array a[p.
		. q-1] or in A[Q+1...R] [return a[q];
		else if (I < K)//i < K, stating that the element to be selected is in the sub-array a[p...q-1], and then proceeds to recursively find the return Randomizedselect (A, p, q-1, i);
			else//i > K, stating that the element to be selected is in the sub-array A[Q+1...R], and then continues to search recursively; Return Randomizedselect (A, q + 1, R, i-k);//Also note that at this point we know that the elements in A[P...Q] (total k) are smaller than the element to be selected, then in the Subarray A[Q+1...R], the element to be selected should be the first
 (i-k) Small}}









Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.