Skip to content
Snippets Groups Projects
Commit bdfd0e8e authored by Tom Almeida's avatar Tom Almeida
Browse files

Added Rust examples and fixed Quicksort.md

parent 0ca80fd2
No related merge requests found
......@@ -20,7 +20,7 @@ function quicksort(array)
quicksort(less)
quicksort(greater)
array := concatenate(less, equal, greater)
```
```
A better quicksort algorithm works in place, by swapping elements within the array, to avoid the memory allocation of more arrays.
```
function quicksort(array)
......@@ -39,7 +39,7 @@ function quicksort(array)
right := right - 1
quicksort(array from first index to right)
quicksort(array from left to last index)
```
```
Quicksort is often compared to Mergesort as they both have an average time complexity of O(nlogn). According to the perldoc:
> On average, mergesort does fewer comparisons than quicksort, so it may be better when complicated comparison routines are used. Mergesort also takes advantage of pre-existing order, so it would be favored for using sort() to merge several sorted arrays. On the other hand, quicksort is often faster for small arrays, and on arrays of a few distinct values, repeated many times.
......
// Using stdlib
use std::collections::BinaryHeap;
fn main() {
let src = vec![6,2,3,6,1,2,7,8,3,2];
let sorted= BinaryHeap::from(src).into_sorted_vec();
println!("{:?}", sorted);
}
// Full code
fn heap_sort<T,F>(array: &mut [T], order: F)
where F: Fn(&T,&T) -> bool
{
let len = array.len();
// Create heap
for start in (0..len/2).rev() {
sift_down(array,&order,start,len-1)
}
for end in (1..len).rev() {
array.swap(0,end);
sift_down(array,&order,0,end-1)
}
}
fn sift_down<T,F>(array: &mut [T], order: &F, start: usize, end: usize)
where F: Fn(&T,&T) -> bool
{
let mut root = start;
loop {
let mut child = root * 2 + 1;
if child > end { break; }
if child + 1 <= end && order(&array[child], &array[child + 1]) {
child += 1;
}
if order(&array[root], &array[child]) {
array.swap(root,child);
root = child
} else {
break;
}
}
}
// First argument is the array. Second argument is a comparison function
fn quick_sort<T,F>(v: &mut [T], f: &F)
where F: Fn(&T,&T) -> bool
{
let len = v.len();
if len >= 2 {
let pivot_index = partition(v, f);
quick_sort(&mut v[0..pivot_index], f);
quick_sort(&mut v[pivot_index + 1..len], f);
}
}
fn partition<T,F>(v: &mut [T], f: &F) -> usize
where F: Fn(&T,&T) -> bool
{
let len = v.len();
let pivot_index = len / 2;
v.swap(pivot_index, len - 1);
let mut store_index = 0;
for i in 0..len - 1 {
if f(&v[i], &v[len - 1]) {
v.swap(i, store_index);
store_index += 1;
}
}
v.swap(store_index, len - 1);
store_index
}
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment