File tree Expand file tree Collapse file tree 1 file changed +23
-3
lines changed Expand file tree Collapse file tree 1 file changed +23
-3
lines changed Original file line number Diff line number Diff line change 3
3
This problem requires you to implement a sorting algorithm
4
4
you can use bubble sorting, insertion sorting, heap sorting, etc.
5
5
*/
6
- // I AM NOT DONE
7
6
8
- fn sort < T > ( array : & mut [ T ] ) {
9
- //TODO
7
+ fn partition < T : Ord > ( array : & mut [ T ] , low : isize , high : isize ) -> isize {
8
+ let piovt = high as usize ;
9
+ let mut i = low - 1 ;
10
+ for j in low..high {
11
+ if array[ j as usize ] <= array[ piovt] {
12
+ i += 1 ;
13
+ array. swap ( i as usize , j as usize ) ;
14
+ }
15
+ }
16
+ array. swap ( ( i + 1 ) as usize , piovt) ;
17
+ i + 1
18
+ }
19
+
20
+ fn quick_sort < T : Ord > ( array : & mut [ T ] , low : isize , high : isize ) {
21
+ if low < high {
22
+ let piovt = partition ( array, low, high) ;
23
+ quick_sort ( array, low, piovt - 1 ) ;
24
+ quick_sort ( array, piovt + 1 , high) ;
25
+ }
26
+ }
27
+
28
+ fn sort < T : Ord > ( array : & mut [ T ] ) {
29
+ quick_sort ( array, 0 , array. len ( ) as isize - 1 ) ;
10
30
}
11
31
#[ cfg( test) ]
12
32
mod tests {
You can’t perform that action at this time.
0 commit comments