HOME

The FORTRAN-90 Project

Add something here ...


~kc4zvw:

  

Notes

Add something here ...

Parallel sieve of Erathostenes

Add something here ...


# EOF #
  

Header 3

Add something here ...

The 'heapsort' sorting algorithm

Add something here ...

kc4zvw@www: ./heapsort

An example of the 'heapsort' sorting algorithm

The array is [12, 11, 13, 5, 6, 7]
The length of the array is: 6
The array is [5, 6, 7, 11, 12, 13]

Finished.

kc4zvw@www:
//
// $Id:$
//


pub fn heap_sort(arr: &mut [T]) {
    if arr.len() <= 1 {
        return; // already sorted
    }
 
    heapify(arr);
 
    for end in (1..arr.len()).rev() {
        arr.swap(0, end);
        move_down(&mut arr[..end], 0);
    }
}
 
/// Convert `arr` into a max heap.
fn heapify(arr: &mut [T]) {
    let last_parent = (arr.len() - 2) / 2;
    for i in (0..=last_parent).rev() {
        move_down(arr, i);
    }
}
 
/// Move the element at `root` down until `arr` is a max heap again.
///
/// This assumes that the subtrees under `root` are valid max heaps already.
fn move_down(arr: &mut [T], mut root: usize) {
    let last = arr.len() - 1;
    loop {
        let left = 2 * root + 1;
        if left > last {
            break;
        }
        let right = left + 1;
        let max = if right <= last && arr[right] > arr[left] {
            right
        } else {
            left
        };
 
        if arr[max] > arr[root] {
            arr.swap(root, max);
        }
        root = max;
    }
}
 
#[cfg(test)]
mod tests {
    use super::*;
 
    #[test]
    fn empty() {
        let mut arr: Vec = Vec::new();
        heap_sort(&mut arr);
        assert_eq!(&arr, &[]);
    }
 
    #[test]
    fn single_element() {
        let mut arr = vec![1];
        heap_sort(&mut arr);
        assert_eq!(&arr, &[1]);
    }
 
    #[test]
    fn sorted_array() {
        let mut arr = vec![1, 2, 3, 4];
        heap_sort(&mut arr);
        assert_eq!(&arr, &[1, 2, 3, 4]);
    }
 
    #[test]
    fn unsorted_array() {
        let mut arr = vec![3, 4, 2, 1];
        heap_sort(&mut arr);
        assert_eq!(&arr, &[1, 2, 3, 4]);
    }
 
    #[test]
    fn odd_number_of_elements() {
        let mut arr = vec![3, 4, 2, 1, 7];
        heap_sort(&mut arr);
        assert_eq!(&arr, &[1, 2, 3, 4, 7]);
    }
 
    #[test]
    fn repeated_elements() {
        let mut arr = vec![542, 542, 542, 542];
        heap_sort(&mut arr);
        assert_eq!(&arr, &vec![542, 542, 542, 542]);
    }
}

/// The main program starts here!

fn main() {

   let mut arr2 = [12, 11, 13, 5, 6, 7];

   println!("An example of the 'heapsort' algorithm");
   println!(" ");
   println!("The array is {:?}", arr2);

   println!("The length of the array is: {}", arr2.len());
   heap_sort(&mut arr2);
   println!("The array is {:?}", arr2);
   println!("");
   println!("Finished.");
}

/* End of File */
  

Header 5

Add something here ...





Revised: Wednesday, April 21, 2021 at 06:22:56 AM (EDT)