Skip to content

Commit f352f87

Browse files
committed
finished quicksort
1 parent f1c4756 commit f352f87

File tree

2 files changed

+56
-20
lines changed

2 files changed

+56
-20
lines changed

src/lib.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@ mod quciksort;
44
pub use bubblesort::bubblesort;
55
pub use quciksort::quicksort;
66

7+
// pub fn generate_random_array(salt: i8, range: i32) -> &[i32]{
8+
// let mut arr = [];
9+
// &arr
10+
// }
11+
12+
713
#[cfg(test)]
814
mod tests {
915
use super::*;

src/quciksort.rs

Lines changed: 50 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,62 @@
1-
pub fn quicksort<T: Ord>(array: &mut [T]) {
2-
let low: isize = 0;
3-
let high: isize = (array.len() - 1) as isize;
1+
2+
/// # Quicksort
3+
///
4+
/// The Quicksort algoritm is a Divide and Conquer algorithm.
5+
/// Quicksort selects a `pivot` element, and takes the other elements smaller or equal on one side and bigger on the other side.
6+
///
7+
/// # Ilustration
8+
/// `arr = [ 8, 5, 9, 2, 7 ]` (pivot is the last element, in this case 7).
9+
///
10+
/// <-- `less or equal` `bigger than` -->
11+
///
12+
/// [ 8, 5, 9, 2, 7 ]
13+
/// |
14+
/// / ------------- 7 ----------------\
15+
/// [ 5, 2 ] [ 8, 9 ]
16+
/// | |
17+
/// [ ] -- 2 -- [5] [8] -- 9 -- [ ]
18+
/// | | <---- `Now we join them back together`
19+
/// \----- [ 2, 5, 7, 8, 9 ] ---- /
20+
/// (Sorted array)
21+
///
22+
pub fn quicksort<T: Ord>(array: &mut [T]) { // Takes mut array of type 'T'.
23+
let low: isize = 0; // Index '0' of array
24+
let high: isize = (array.len() - 1) as isize; // Highest index of array
425

526
sort(array, low, high);
627

7-
fn sort<T: Ord>(array: &mut [T], low: isize, high: isize){
8-
if low < high {
9-
let pi: isize = temp(array, low, high);
28+
}
1029

11-
sort(array, low, pi - 1);
12-
sort(array, (pi + 1) as isize, high);
13-
14-
}
1530

16-
fn temp<T: Ord>(array: &mut [T], low: isize, high: isize) -> isize {
17-
let mut i = low - 1;
31+
fn sort<T: Ord>(arr: &mut [T], low: isize, high: isize){
32+
if low < high {
33+
let pivot_element: isize = temp(arr, low, high); // pivot element (always the element of the highest index, in the whole or sub array)
1834

19-
for j in 0..high {
20-
if &array[j as usize] < &array[high as usize] {
35+
sort(arr, low, pivot_element - 1); // Before pi
36+
sort(arr, pivot_element + 1, high); // After pi
37+
}
38+
39+
fn temp<T: Ord>(arr: &mut [T], low: isize, high: isize) -> isize {
40+
let pivot = high as usize; // Pivot element
41+
let mut i = low - 1;
42+
let mut j = high;
2143

44+
loop {
45+
i += 1;
46+
while arr[i as usize] < arr[pivot] {
2247
i += 1;
23-
24-
array.swap(i as usize, j as usize);
25-
48+
}
49+
j -= 1;
50+
while j >= 0 && arr[j as usize] > arr[pivot] {
51+
j -= 1;
52+
}
53+
if i >= j { // If current element is smaller than pivot element.
54+
break;
55+
} else {
56+
arr.swap(i as usize, j as usize);
2657
}
2758
}
28-
array.swap((i + 1) as usize, high as usize);
29-
i + 1
30-
}
59+
arr.swap(i as usize, pivot as usize);
60+
i
3161
}
3262
}

0 commit comments

Comments
 (0)