/// 快速選擇法(從最小開始),使用迴圈來迭代。
pub fn quickselect(array: &mut [i32], index: usize) -> usize {
let mut start = 0;
let mut end = array.len() - 1;
loop {
if start == end {
return start;
}
let pivot = array[start];
let mut l = start + 1;
let mut r = end;
loop {
while r > start && array[r] >= pivot {
r -= 1;
}
while l <= r && array[l] <= pivot {
l += 1;
}
if l < r {
array.swap(l, r);
} else {
if r > start {
array.swap(start, r);
}
break;
}
}
match index.cmp(&r) {
Ordering::Equal => {
return index;
}
Ordering::Greater => {
start = r + 1;
}
Ordering::Less => {
end = r - 1;
}
}
}
}
/// 快速選擇法(從最大開始),使用迴圈來迭代。
pub fn quickselect_desc(array: &mut [i32], index: usize) -> usize {
let mut start = 0;
let mut end = array.len() - 1;
loop {
if start == end {
return start;
}
let pivot = array[end];
let mut l = start;
let mut r = end - 1;
loop {
while l < end && array[l] >= pivot {
l += 1;
}
while r >= l && array[r] <= pivot {
if r == 0 {
break;
}
r -= 1;
}
if l < r {
array.swap(l, r);
} else {
if l < end {
array.swap(l, end);
}
break;
}
}
match index.cmp(&l) {
Ordering::Equal => {
return index;
}
Ordering::Greater => {
start = l + 1;
}
Ordering::Less => {
end = l - 1;
}
}
}
}
/**
* 快速選擇法(從最小開始),使用迴圈來迭代。
*/
public static int quickselect(final int[] array, final int index) {
int start = 0;
int end = array.length - 1;
while (true) {
if (start == end) {
return start;
}
final int pivot = array[start];
int l = start + 1;
int r = end;
while (true) {
while (r > start && array[r] >= pivot) {
--r;
}
while (l <= r && array[l] <= pivot) {
++l;
}
if (l < r) {
int temp = array[l];
array[l] = array[r];
array[r] = temp;
} else {
if (r > start) {
int temp = array[start];
array[start] = array[r];
array[r] = temp;
}
break;
}
}
if (index == r) {
return index;
} else if (index > r) {
start = r + 1;
} else {
end = r - 1;
}
}
}
/**
* 快速選擇法(從最大開始),使用迴圈來迭代。
*/
public static int quickselectDesc(final int[] array, final int index) {
int start = 0;
int end = array.length - 1;
while (true) {
if (start == end) {
return start;
}
final int pivot = array[end];
int l = start;
int r = end - 1;
while (true) {
while (l < end && array[l] >= pivot) {
++l;
}
while (r >= l && array[r] <= pivot) {
--r;
}
if (l < r) {
int temp = array[l];
array[l] = array[r];
array[r] = temp;
} else {
if (l < end) {
int temp = array[l];
array[l] = array[end];
array[end] = temp;
}
break;
}
}
if (index == l) {
return index;
} else if (index > l) {
start = l + 1;
} else {
end = l - 1;
}
}
}
/**
* 快速選擇法(從最小開始),使用迴圈來迭代。
*/
function quickselect(array: number[], index: number) {
let start = 0;
let end = array.length - 1;
for (; ;) {
if (end === start) {
return start;
}
const pivot = array[start];
let l = start + 1;
let r = end;
for (; ;) {
while (r > start && array[r] >= pivot) {
r -= 1;
}
while (l <= r && array[l] <= pivot) {
l += 1;
}
if (l < r) {
const temp = array[l];
array[l] = array[r];
array[r] = temp;
} else {
if (r > start) {
const temp = array[start];
array[start] = array[r];
array[r] = temp;
}
break;
}
}
if (index === r) {
return index;
} else if (index > r) {
start = r + 1;
} else {
end = r - 1;
}
}
}
/**
* 快速選擇法(從最大開始),使用迴圈來迭代。
*/
function quickselectDesc(array: number[], index: number) {
let start = 0;
let end = array.length - 1;
for (; ;) {
if (end === start) {
return start;
}
const pivot = array[end];
let l = start;
let r = end - 1;
for (; ;) {
while (l < end && array[l] >= pivot) {
l += 1;
}
while (r >= l && array[r] <= pivot) {
r -= 1;
}
if (l < r) {
const temp = array[l];
array[l] = array[r];
array[r] = temp;
} else {
if (l < end) {
const temp = array[l];
array[l] = array[end];
array[end] = temp;
}
break;
}
}
if (index === l) {
return index;
} else if (index > l) {
start = l + 1;
} else {
end = l - 1;
}
}
}
// 快速選擇法(從最小開始),使用迴圈來迭代。
func Quickselect(array []int, index int) int {
start := 0
end := len(array) - 1
for {
if start == end {
return start
}
middle := (start + end) / 2
if start != middle {
array[start], array[middle] = array[middle], array[start]
}
pivot := array[start]
l := start + 1
r := end
for {
for r > start && array[r] >= pivot {
r -= 1
}
for l <= r && array[l] <= pivot {
l += 1
}
if l < r {
array[l], array[r] = array[r], array[l]
} else {
if r > start {
array[start], array[r] = array[r], array[start]
}
break
}
}
if index == r {
return index
} else if index > r {
start = r + 1
} else {
end = r - 1
}
}
}
// 快速選擇法(從最大開始),使用迴圈來迭代。
func QuickselectDesc(array []int, index int) int {
start := 0
end := len(array) - 1
for {
if start == end {
return start
}
pivot := array[end]
l := start
r := end - 1
for {
for l < end && array[l] >= pivot {
l += 1
}
for r >= l && array[r] <= pivot {
r -= 1
}
if l < r {
array[l], array[r] = array[r], array[l]
} else {
if l < end {
array[l], array[end] = array[end], array[l]
}
break
}
}
if index == l {
return index
} else if index > l {
start = l + 1
} else {
end = l - 1
}
}
}