@@ -4,13 +4,13 @@ fn partition(list: &mut [i32], left: usize, right: usize, pivot_index: usize) ->
4
4
let pivot_value = list[ pivot_index] ;
5
5
list. swap ( pivot_index, right) ; // Move pivot to end
6
6
let mut store_index = left;
7
- for i in left..( right + 1 ) {
7
+ for i in left..right {
8
8
if list[ i] < pivot_value {
9
9
list. swap ( store_index, i) ;
10
10
store_index += 1 ;
11
11
}
12
- list. swap ( right, store_index) ; // Move pivot to its final place
13
12
}
13
+ list. swap ( right, store_index) ; // Move pivot to its final place
14
14
store_index
15
15
}
16
16
@@ -19,7 +19,7 @@ pub fn quick_select(list: &mut [i32], left: usize, right: usize, index: usize) -
19
19
// If the list contains only one element,
20
20
return list[ left] ;
21
21
} // return that element
22
- let mut pivot_index = 1 + left + ( right - left) / 2 ; // select a pivotIndex between left and right
22
+ let mut pivot_index = left + ( right - left) / 2 ; // select a pivotIndex between left and right
23
23
pivot_index = partition ( list, left, right, pivot_index) ;
24
24
// The pivot is in its final sorted position
25
25
match index {
@@ -37,7 +37,7 @@ mod tests {
37
37
let mut arr1 = [ 2 , 3 , 4 , 5 ] ;
38
38
assert_eq ! ( quick_select( & mut arr1, 0 , 3 , 1 ) , 3 ) ;
39
39
let mut arr2 = [ 2 , 5 , 9 , 12 , 16 ] ;
40
- assert_eq ! ( quick_select( & mut arr2, 1 , 3 , 2 ) , 12 ) ;
40
+ assert_eq ! ( quick_select( & mut arr2, 1 , 3 , 2 ) , 9 ) ;
41
41
let mut arr2 = [ 0 , 3 , 8 ] ;
42
42
assert_eq ! ( quick_select( & mut arr2, 0 , 0 , 0 ) , 0 ) ;
43
43
}
0 commit comments