+fn sort<T: PartialOrd>(data: &mut [T]) {
+ if data.len() < 2 { return; }
+
+ let mut lpos = 1;
+ let mut rpos = data.len();
+ // Invariant: pivot is data[0]; (0,lpos) is <= pivot; [rpos,len) is >= pivot; lpos < rpos
+ loop {
+ while lpos < rpos && data[lpos] <= data[0] {
+ lpos += 1;
+ }
+ while rpos > lpos && data[rpos-1] >= data[0] {
+ rpos -= 1;
+ }
+ if rpos == lpos {
+ break;
+ }
+
+ data.swap(lpos, rpos-1);
+ }
+
+ data.swap(0, lpos-1); // put pivot in the right place
+
+ let (part1, part2) = data.split_at_mut(lpos);
+ sort(&mut part1[..lpos-1]);
+ sort(part2);
+}
+