java - Writing a Quicksort with one loop -
My brother wants me to only optimize my code by a loop. I do not think how only a loop can work in a quaortet (they asked me to remove the internal loop)
public class QuickSort {public static zero instant (int [] Target, int lo, int hi) {if (lo> = en) {return; } Random Number Generator = New Random (); Int Pivot = Number generator. Take NXITINTI (High-Low) +; Int pivotValue = target [pivot]; Target [axle] = target [hi]; Targeted to [Hi] = pivotValue; Axis = hi; Int counter; {Counter = lo; Counter & lieutenance; Pivot; Counter ++) {While (target [counter] & gt; target [pivot]) {if (smoke-counter == 1} {int temp = target] [counter]; target [counter] = target [pivot]; target [Axis] = floating; // return; // possibly problem} and {int temp1 = target [pivot-1]; int temp2 = target [pivot]; target [axle] = target [counter]; target [axle 1] Public target zero main (string [] args) {} = temp2; target [counter] = temp1; axis = axis1;}}} instant (target, lo, counter 1) Int sizeOfArray = 10; int number o For test = 1000; for int numfailed = 0; (int i = 0; i
}
Quikroort is conceptually two loops: An internal partitioning loop, which runs on the entire array, and an external partition loop is usually expressed through recursion. Here, you have the division part in classical way, but your division is a bit complicated.
For one of your external A common method of writing partitions is to use a loop with two counter instead of one. A counter that you have used: All the things on the left are less than the axis. The second counter plays a symmetrical role: everything on its right is more than the axis. The loop body does the following: If both < P> When the counter is crossed, the loop ends up. This will eventually end because at least one counter runs on each walk. ,
takes one step left to the loop, write your array from left to right). Transforms a notch to the inner for your inner
(except for a special case where the counter has almost reached the axis and you swap the last). There is nothing like that which takes the counter back in the right direction or the axis goes back to the back, so you are not doing extra work due to the two loops, it is a matter of clarity rather than efficiency.
targets [left_counter]
and target [right_counter]
are both outside, place Swap them; After this, both targets [left_counter]
and are in the desired side of the target [right_counter]
array, so it increases left_counter
and subtract otherwise : If
target [left_counter]
is in the desired side, increase left_counter
; If is the target [right_counter]
in the desired side, then right_counter
Comments
Post a Comment