Divided
Dividing data is a group of data that divides data into two groups, so that all keywords are larger than a specific value, and that all keywords are less than a specific value in the other group.
package com.gaojipaixu.partition;public class arraypar{ private long[] thearray; private int nelems; public arraypar (Int max) { thearray = new long[max]; nElems = 0; } public void insert (Long value) { theArray[nElems] = value; nElems++; } Public int size () { return nelems; } public void display () { System.out.print ("a="); for (int i = 0; i < nelems; i++) { system.out.print (thearray[i]+ " "); } system.out.println (""); } public int partitionit (Int left,int right,long pivot) { int leftPtr = left-1; int rightPtr = right+1; while (true) { while (leftPtr<right && thearray[++leftptr]&lT;pivot) ; while (rightPtr>left && thearray [--rightptr]>pivot) ; if (leftPtr >= &NBSP;RIGHTPTR) break; else{ long temp ; temp = Thearray[leftptr]; thearray[leftptr] = thearray[rightptr]; theArray[rightPtr] = temp; } } return leftptr; }}
Public static void main (String[] args) {int maxSize = 16; Arraypar arr ;arr = new arraypar (maxSize);for (int i = 0; i < maxsize; i++) {long n = (int) (Java.lang.Math.random () *199); Arr.insert (n);} Arr.display ();long pivot = 99; System.out.println ("pivot is " +pivot); Int size = arr.size (); int partdex = arr.partitionit (0, size-1, pivot); System.out.println (",partition is at index " +partdex); Arr.display ();} Output:a=170 142 81 128 131 39 16 186 84 35 46 195 174 114 144 103 pivot is 99,partition is at index 6a=46 35 81 84 16 39 131 186 128 142 170 195 174 114 144 103
It can be seen that the division is successful: the first 6 numbers are smaller than the pivot 99, and the last 8 numbers are larger than the hubs.
Note that the partitioning process does not necessarily divide the array into two halves of the same size as this example, depending on the value of the pivot and data key. It is possible that the number of data items in a group is greater than the number of items in another group.
Partitioning algorithm
The partitioning algorithm starts with two pointers, and two pointers point to both ends of the array. (The word "pointer" is used here to indicate the array data item, not the pointer in C + +.) The pointer on the left, Leftptr, moves to the right, while the pointer on the right, rightptr, moves to the left.
In fact, leftptr initialization is at the left of the first data item, and Rightptr is on the right-hand side of the last data item, because before they work, they have to add one and minus one separately.
Stop and Exchange
When Leftptr encounters a data item that is smaller than the hub, it continues to move right because the data item's position is already on the right side of the array. However, when it comes to data items that are larger than the pivot, it stops, similarly, when RIGHTPRT encounters a data item that is larger than the pivot, it continues to move to the left but when it finds a data item that is smaller than the pivot, it stops. When the two inner layers of the while loop, the first applies leftptr, the second applies to RIGHTPRT, which controls the scanning process. Because the pointer exits the while loop, it stops moving. Here is a simplified code for a scan of data items that are not in place:
while (Thearray[++leftptr] < pivot)//find bigger item; (NOP) while (thearray[--right]>pivot)//find smaller item; (NOP) swap (LEFGPTR,RIGHTPTR); Swap elements
The first while loop exits when a data item larger than the pivot is found, and the second loop exits when a small data item is found. When both loops are exited, both Leftptr and Rightprt point to the data item on the wrong side of the array, so two data items are exchanged.
256
Java Data structures and algorithms (seventh advanced Sort 2)