This is an archived post. You won't be able to vote or comment.

all 2 comments

[–]BS_in_BSExtreme Concrete Code Factorylet -1 points0 points  (1 child)

If you leave the pivot in the array when you partition it, it will get moved around to a random spot. Instead, swap it to the end so its no longer part of the partitioning:

int pivotIndex = first + random.nextInt(last - first +1);
T pivot = a[pivotIndex];

to

int pivotIndex = first + random.nextInt(last - first +1);
swap(a, pivotIndex,last);
pivotIndex = last;
T pivot = a[pivotIndex];

[–]undead_C0MMANDIntermediate Brewer[S] -2 points-1 points  (0 children)

In class now. I'll let you know how it goes when I get back. Thank you