Pure Programmer
Blue Matrix


Cluster Map

Project: Insertion Sort

The [[Insertion sort]] is almost as easy to implment as the bubble sort and has similar performance characteristics. Write a function that takes a list of integers and sorts the list without modifying it, returning a sorted list, using an insertion sort. Test the function by creating a random list of integers, print the random list, sort the list, then print out the sorted list. Accept the number of integers to put in the list and the maximum integer to randomly generate on the command line. What is the worst case performance of the Insertion sort?

Output
$ rustc InsertionSort.rs error[E0425]: cannot find value `args` in this scope --> InsertionSort.rs:37:5 | 37 | if args.len() != 3 { | ^^^^ not found in this scope | help: consider importing this function | 7 + use std::env::args; | error[E0425]: cannot find function `exit` in this scope --> InsertionSort.rs:39:3 | 39 | exit(1); | ^^^^ not found in this scope | help: consider importing this function | 7 + use std::process::exit; | error[E0425]: cannot find value `Utils` in this scope --> InsertionSort.rs:41:23 | 41 | let listSize:isize = Utils.stoiWithDefault(args[1], 10); | ^^^^^ not found in this scope error[E0425]: cannot find value `args` in this scope --> InsertionSort.rs:41:45 | 41 | let listSize:isize = Utils.stoiWithDefault(args[1], 10); | ^^^^ not found in this scope | help: consider importing this function | 7 + use std::env::args; | error[E0425]: cannot find value `Utils` in this scope --> InsertionSort.rs:42:21 | 42 | let maxInt:isize = Utils.stoiWithDefault(args[2], 100); | ^^^^^ not found in this scope error[E0425]: cannot find value `args` in this scope --> InsertionSort.rs:42:43 | 42 | let maxInt:isize = Utils.stoiWithDefault(args[2], 100); | ^^^^ not found in this scope | help: consider importing this function | 7 + use std::env::args; | error[E0308]: mismatched types --> InsertionSort.rs:11:23 | 11 | let listSize:isize = list.len(); | ----- ^^^^^^^^^^ expected `isize`, found `usize` | | | expected due to this | help: you can convert a `usize` to an `isize` and panic if the converted value doesn't fit | 11 | let listSize:isize = list.len().try_into().unwrap(); | ++++++++++++++++++++ error[E0277]: the type `[isize]` cannot be indexed by `isize` --> InsertionSort.rs:17:26 | 17 | sortedList.push(list[i]); | ^ slice indices are of type `usize` or ranges of `usize` | = help: the trait `SliceIndex<[isize]>` is not implemented for `isize` = help: the trait `SliceIndex<[T]>` is implemented for `usize` = note: required for `Vec<isize>` to implement `Index<isize>` error[E0277]: the type `[isize]` cannot be indexed by `isize` --> InsertionSort.rs:21:21 | 21 | if sortedList[j] > sortedList[j + 1] { | ^ slice indices are of type `usize` or ranges of `usize` | = help: the trait `SliceIndex<[isize]>` is not implemented for `isize` = help: the trait `SliceIndex<[T]>` is implemented for `usize` = note: required for `Vec<isize>` to implement `Index<isize>` error[E0277]: the type `[isize]` cannot be indexed by `isize` --> InsertionSort.rs:21:37 | 21 | if sortedList[j] > sortedList[j + 1] { | ^^^^^ slice indices are of type `usize` or ranges of `usize` | = help: the trait `SliceIndex<[isize]>` is not implemented for `isize` = help: the trait `SliceIndex<[T]>` is implemented for `usize` = note: required for `Vec<isize>` to implement `Index<isize>` error[E0277]: the type `[isize]` cannot be indexed by `isize` --> InsertionSort.rs:22:36 | 22 | ... let temp:isize = sortedList[j + 1]; | ^^^^^ slice indices are of type `usize` or ranges of `usize` | = help: the trait `SliceIndex<[isize]>` is not implemented for `isize` = help: the trait `SliceIndex<[T]>` is implemented for `usize` = note: required for `Vec<isize>` to implement `Index<isize>` error[E0277]: the type `[isize]` cannot be indexed by `isize` --> InsertionSort.rs:23:19 | 23 | ... sortedList[j + 1] = sortedList[j]; | ^^^^^ slice indices are of type `usize` or ranges of `usize` | = help: the trait `SliceIndex<[isize]>` is not implemented for `isize` = help: the trait `SliceIndex<[T]>` is implemented for `usize` = note: required for `Vec<isize>` to implement `Index<isize>` error[E0277]: the type `[isize]` cannot be indexed by `isize` --> InsertionSort.rs:23:39 | 23 | ... sortedList[j + 1] = sortedList[j]; | ^ slice indices are of type `usize` or ranges of `usize` | = help: the trait `SliceIndex<[isize]>` is not implemented for `isize` = help: the trait `SliceIndex<[T]>` is implemented for `usize` = note: required for `Vec<isize>` to implement `Index<isize>` error[E0277]: the type `[isize]` cannot be indexed by `isize` --> InsertionSort.rs:24:19 | 24 | ... sortedList[j] = temp; | ^ slice indices are of type `usize` or ranges of `usize` | = help: the trait `SliceIndex<[isize]>` is not implemented for `isize` = help: the trait `SliceIndex<[T]>` is implemented for `usize` = note: required for `Vec<isize>` to implement `Index<isize>` error[E0425]: cannot find function `irandom` in this scope --> InsertionSort.rs:49:20 | 49 | listToSort.push(irandom(maxInt)); | ^^^^^^^ not found in this scope error: aborting due to 15 previous errors Some errors have detailed explanations: E0277, E0308, E0425. For more information about an error, try `rustc --explain E0277`. $ rustc InsertionSort.rs error[E0425]: cannot find value `args` in this scope --> InsertionSort.rs:37:5 | 37 | if args.len() != 3 { | ^^^^ not found in this scope | help: consider importing this function | 7 + use std::env::args; | error[E0425]: cannot find function `exit` in this scope --> InsertionSort.rs:39:3 | 39 | exit(1); | ^^^^ not found in this scope | help: consider importing this function | 7 + use std::process::exit; | error[E0425]: cannot find value `Utils` in this scope --> InsertionSort.rs:41:23 | 41 | let listSize:isize = Utils.stoiWithDefault(args[1], 10); | ^^^^^ not found in this scope error[E0425]: cannot find value `args` in this scope --> InsertionSort.rs:41:45 | 41 | let listSize:isize = Utils.stoiWithDefault(args[1], 10); | ^^^^ not found in this scope | help: consider importing this function | 7 + use std::env::args; | error[E0425]: cannot find value `Utils` in this scope --> InsertionSort.rs:42:21 | 42 | let maxInt:isize = Utils.stoiWithDefault(args[2], 100); | ^^^^^ not found in this scope error[E0425]: cannot find value `args` in this scope --> InsertionSort.rs:42:43 | 42 | let maxInt:isize = Utils.stoiWithDefault(args[2], 100); | ^^^^ not found in this scope | help: consider importing this function | 7 + use std::env::args; | error[E0308]: mismatched types --> InsertionSort.rs:11:23 | 11 | let listSize:isize = list.len(); | ----- ^^^^^^^^^^ expected `isize`, found `usize` | | | expected due to this | help: you can convert a `usize` to an `isize` and panic if the converted value doesn't fit | 11 | let listSize:isize = list.len().try_into().unwrap(); | ++++++++++++++++++++ error[E0277]: the type `[isize]` cannot be indexed by `isize` --> InsertionSort.rs:17:26 | 17 | sortedList.push(list[i]); | ^ slice indices are of type `usize` or ranges of `usize` | = help: the trait `SliceIndex<[isize]>` is not implemented for `isize` = help: the trait `SliceIndex<[T]>` is implemented for `usize` = note: required for `Vec<isize>` to implement `Index<isize>` error[E0277]: the type `[isize]` cannot be indexed by `isize` --> InsertionSort.rs:21:21 | 21 | if sortedList[j] > sortedList[j + 1] { | ^ slice indices are of type `usize` or ranges of `usize` | = help: the trait `SliceIndex<[isize]>` is not implemented for `isize` = help: the trait `SliceIndex<[T]>` is implemented for `usize` = note: required for `Vec<isize>` to implement `Index<isize>` error[E0277]: the type `[isize]` cannot be indexed by `isize` --> InsertionSort.rs:21:37 | 21 | if sortedList[j] > sortedList[j + 1] { | ^^^^^ slice indices are of type `usize` or ranges of `usize` | = help: the trait `SliceIndex<[isize]>` is not implemented for `isize` = help: the trait `SliceIndex<[T]>` is implemented for `usize` = note: required for `Vec<isize>` to implement `Index<isize>` error[E0277]: the type `[isize]` cannot be indexed by `isize` --> InsertionSort.rs:22:36 | 22 | ... let temp:isize = sortedList[j + 1]; | ^^^^^ slice indices are of type `usize` or ranges of `usize` | = help: the trait `SliceIndex<[isize]>` is not implemented for `isize` = help: the trait `SliceIndex<[T]>` is implemented for `usize` = note: required for `Vec<isize>` to implement `Index<isize>` error[E0277]: the type `[isize]` cannot be indexed by `isize` --> InsertionSort.rs:23:19 | 23 | ... sortedList[j + 1] = sortedList[j]; | ^^^^^ slice indices are of type `usize` or ranges of `usize` | = help: the trait `SliceIndex<[isize]>` is not implemented for `isize` = help: the trait `SliceIndex<[T]>` is implemented for `usize` = note: required for `Vec<isize>` to implement `Index<isize>` error[E0277]: the type `[isize]` cannot be indexed by `isize` --> InsertionSort.rs:23:39 | 23 | ... sortedList[j + 1] = sortedList[j]; | ^ slice indices are of type `usize` or ranges of `usize` | = help: the trait `SliceIndex<[isize]>` is not implemented for `isize` = help: the trait `SliceIndex<[T]>` is implemented for `usize` = note: required for `Vec<isize>` to implement `Index<isize>` error[E0277]: the type `[isize]` cannot be indexed by `isize` --> InsertionSort.rs:24:19 | 24 | ... sortedList[j] = temp; | ^ slice indices are of type `usize` or ranges of `usize` | = help: the trait `SliceIndex<[isize]>` is not implemented for `isize` = help: the trait `SliceIndex<[T]>` is implemented for `usize` = note: required for `Vec<isize>` to implement `Index<isize>` error[E0425]: cannot find function `irandom` in this scope --> InsertionSort.rs:49:20 | 49 | listToSort.push(irandom(maxInt)); | ^^^^^^^ not found in this scope error: aborting due to 15 previous errors Some errors have detailed explanations: E0277, E0308, E0425. For more information about an error, try `rustc --explain E0277`.

Solution