Skip to content

stdlib/sortView source ↗

sort

15 items · generated by scribe fromstdlib/sort.fd· synced 2026-11-15

stdlib/sort15 items

Contents

sort_quick

Sort a Vec in ascending order using quicksort. In-place, average O(n log n).

Example: var v: Vec= Vec::new(); v.push(3); v.push(1); v.push(4); v.push(1); v.push(5); sort_quick(&mut v); assert_eq(v.get(0), 1); assert_eq(v.get(4), 5); assert(sort_is_sorted(v));

pub fn sort_quick(v: & mut Vec<i64>) -> void {

Parameters:

  • v: & mut Vec<i64>

Returns: void

Defined at line 28


sort_quick_desc

Sort a Vec in descending order using quicksort.

Example: var v: Vec= Vec::new(); v.push(3); v.push(1); v.push(4); sort_quick_desc(&mut v); assert_eq(v.get(0), 4); assert(sort_is_sorted_desc(v));

pub fn sort_quick_desc(v: & mut Vec<i64>) -> void {

Parameters:

  • v: & mut Vec<i64>

Returns: void

Defined at line 43


sort_insertion

Sort a Vec using insertion sort. O(n²) but fast for small arrays.

Example: var v: Vec= Vec::new(); v.push(2); v.push(-1); v.push(0); sort_insertion(&mut v); assert_eq(v.get(0), -1);

pub fn sort_insertion(v: & mut Vec<i64>) -> void {

Parameters:

  • v: & mut Vec<i64>

Returns: void

Defined at line 57


sort_bubble

Sort a Vec using bubble sort. O(n²), simple but slow.

Example: var v: Vec= Vec::new(); v.push(9); v.push(8); v.push(7); sort_bubble(&mut v); assert_eq(v.get(0), 7);

pub fn sort_bubble(v: & mut Vec<i64>) -> void {

Parameters:

  • v: & mut Vec<i64>

Returns: void

Defined at line 84


sort_selection

Sort a Vec using selection sort. O(n²), minimal swaps.

Example: var v: Vec= Vec::new(); v.push(5); v.push(2); v.push(8); sort_selection(&mut v); assert_eq(v.get(1), 5);

pub fn sort_selection(v: & mut Vec<i64>) -> void {

Parameters:

  • v: & mut Vec<i64>

Returns: void

Defined at line 109


sort_by

Sort a Vec using a custom comparator function. Comparator returns: negative if a < b, 0 if equal, positive if a > b.

Example: var v: Vec= Vec::new(); v.push(-3); v.push(1); v.push(-2); sort_by(&mut v, fn(a: i64, b: i64) -> i64 { a * a - b * b }); assert_eq(v.get(0), 1); assert_eq(v.get(2), -3);

pub fn sort_by(v: & mut Vec<i64>, cmp: fn(i64, i64) -> i64) -> void {

Parameters:

  • v: & mut Vec<i64>
  • cmp: fn(i64

Returns: void

Defined at line 141


sort_is_sorted

Check if a Vec is sorted in ascending order.

Example: var v: Vec= Vec::new(); v.push(1); v.push(2); v.push(2); assert(sort_is_sorted(v)); v.push(0); assert(!sort_is_sorted(v));

pub fn sort_is_sorted(v: Vec<i64>) -> bool {

Parameters:

  • v: Vec<i64>

Returns: bool

Defined at line 158


sort_is_sorted_desc

Check if a Vec is sorted in descending order.

Example: var v: Vec= Vec::new(); v.push(3); v.push(3); v.push(1); assert(sort_is_sorted_desc(v));

pub fn sort_is_sorted_desc(v: Vec<i64>) -> bool {

Parameters:

  • v: Vec<i64>

Returns: bool

Defined at line 176


sort_copy

Return a sorted copy of a Vec (ascending).

Example: var v: Vec= Vec::new(); v.push(2); v.push(1); var s = sort_copy(v); assert_eq(s.get(0), 1); assert_eq(v.get(0), 2);

pub fn sort_copy(v: Vec<i64>) -> Vec<i64> {

Parameters:

  • v: Vec<i64>

Returns: Vec<i64>

Defined at line 196


sort_reverse

Reverse a Vec in place.

Example: var v: Vec= Vec::new(); v.push(1); v.push(2); v.push(3); sort_reverse(&mut v); assert_eq(v.get(0), 3); assert_eq(v.get(2), 1);

pub fn sort_reverse(v: & mut Vec<i64>) -> void {

Parameters:

  • v: & mut Vec<i64>

Returns: void

Defined at line 215


Find the index of a value in a sorted Vec using binary search. Returns the index if found, -1 if not found.

Example: var v: Vec= Vec::new(); v.push(1); v.push(3); v.push(5); v.push(7); assert_eq(sort_binary_search(v, 5), 2); assert_eq(sort_binary_search(v, 4), -1);

pub fn sort_binary_search(v: Vec<i64>, target: i64) -> i64 {

Parameters:

  • v: Vec<i64>
  • target: i64

Returns: i64

Defined at line 234


sort_lower_bound

Find the insertion point for a value in a sorted Vec. Returns the index where the value should be inserted to maintain order.

Example: var v: Vec= Vec::new(); v.push(1); v.push(3); v.push(5); assert_eq(sort_lower_bound(v, 4), 2); assert_eq(sort_lower_bound(v, 0), 0); assert_eq(sort_lower_bound(v, 9), 3);

pub fn sort_lower_bound(v: Vec<i64>, target: i64) -> i64 {

Parameters:

  • v: Vec<i64>
  • target: i64

Returns: i64

Defined at line 261


sort_quick_range

Quicksort the elements at indexes lo..=hi in place, ascending.

Example: var v: Vec= Vec::new(); v.push(9); v.push(3); v.push(2); v.push(1); sort_quick_range(&mut v, 1, 3); assert_eq(v.get(0), 9); assert_eq(v.get(1), 1);

pub fn sort_quick_range(v: & mut Vec<i64>, lo: i64, hi: i64) -> void {

Parameters:

  • v: & mut Vec<i64>
  • lo: i64
  • hi: i64

Returns: void

Defined at line 285


sort_quick_desc_range

Quicksort the elements at indexes lo..=hi in place, descending.

Example: var v: Vec= Vec::new(); v.push(1); v.push(2); v.push(3); sort_quick_desc_range(&mut v, 0, 2); assert_eq(v.get(0), 3);

pub fn sort_quick_desc_range(v: & mut Vec<i64>, lo: i64, hi: i64) -> void {

Parameters:

  • v: & mut Vec<i64>
  • lo: i64
  • hi: i64

Returns: void

Defined at line 316


sort_by_range

Sort the elements at indexes lo..=hi in place with a comparison function (negative when a comes before b).

Example: var v: Vec= Vec::new(); v.push(3); v.push(2); v.push(1); sort_by_range(&mut v, 0, 1, fn(a: i64, b: i64) -> i64 { a - b }); assert_eq(v.get(0), 2); assert_eq(v.get(2), 1);

pub fn sort_by_range(v: & mut Vec<i64>, lo: i64, hi: i64, cmp: fn(i64, i64) -> i64) -> void {

Parameters:

  • v: & mut Vec<i64>
  • lo: i64
  • hi: i64
  • cmp: fn(i64

Returns: void

Defined at line 349


Generated by scribe, the Fides documentation generator