Pure Programmer
Blue Matrix


Cluster Map

Project: Bubble Sort

The [[Bubble sort]] is perhaps the easiest sort to implement. Write a function that takes a list of integers and sorts the list, in place, using a bubble 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 Bubble sort?

Output
$ rustc BubbleSort.rs error[E0425]: cannot find value `args` in this scope --> BubbleSort.rs:35:5 | 35 | 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 --> BubbleSort.rs:37:3 | 37 | 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 --> BubbleSort.rs:39:23 | 39 | let listSize:isize = Utils.stoiWithDefault(args[1], 10); | ^^^^^ not found in this scope error[E0425]: cannot find value `args` in this scope --> BubbleSort.rs:39:45 | 39 | 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 --> BubbleSort.rs:40:21 | 40 | let maxInt:isize = Utils.stoiWithDefault(args[2], 100); | ^^^^^ not found in this scope error[E0425]: cannot find value `args` in this scope --> BubbleSort.rs:40:43 | 40 | 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 --> BubbleSort.rs:11:21 | 11 | let mut i:isize = list.len() - 2; | ----- ^^^^^^^^^^^^^^ 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 mut i:isize = (list.len() - 2).try_into().unwrap(); | + +++++++++++++++++++++ error[E0277]: the type `[isize]` cannot be indexed by `isize` --> BubbleSort.rs:17:14 | 17 | if list[j] > list[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` --> BubbleSort.rs:17:24 | 17 | if list[j] > list[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` --> BubbleSort.rs:18:29 | 18 | let temp:isize = list[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` --> BubbleSort.rs:19:12 | 19 | list[j + 1] = list[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` --> BubbleSort.rs:19:26 | 19 | list[j + 1] = list[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` --> BubbleSort.rs:20:12 | 20 | list[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 --> BubbleSort.rs:47:20 | 47 | listToSort.push(irandom(maxInt)); | ^^^^^^^ not found in this scope error: aborting due to 14 previous errors Some errors have detailed explanations: E0277, E0308, E0425. For more information about an error, try `rustc --explain E0277`. $ rustc BubbleSort.rs error[E0425]: cannot find value `args` in this scope --> BubbleSort.rs:35:5 | 35 | 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 --> BubbleSort.rs:37:3 | 37 | 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 --> BubbleSort.rs:39:23 | 39 | let listSize:isize = Utils.stoiWithDefault(args[1], 10); | ^^^^^ not found in this scope error[E0425]: cannot find value `args` in this scope --> BubbleSort.rs:39:45 | 39 | 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 --> BubbleSort.rs:40:21 | 40 | let maxInt:isize = Utils.stoiWithDefault(args[2], 100); | ^^^^^ not found in this scope error[E0425]: cannot find value `args` in this scope --> BubbleSort.rs:40:43 | 40 | 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 --> BubbleSort.rs:11:21 | 11 | let mut i:isize = list.len() - 2; | ----- ^^^^^^^^^^^^^^ 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 mut i:isize = (list.len() - 2).try_into().unwrap(); | + +++++++++++++++++++++ error[E0277]: the type `[isize]` cannot be indexed by `isize` --> BubbleSort.rs:17:14 | 17 | if list[j] > list[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` --> BubbleSort.rs:17:24 | 17 | if list[j] > list[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` --> BubbleSort.rs:18:29 | 18 | let temp:isize = list[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` --> BubbleSort.rs:19:12 | 19 | list[j + 1] = list[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` --> BubbleSort.rs:19:26 | 19 | list[j + 1] = list[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` --> BubbleSort.rs:20:12 | 20 | list[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 --> BubbleSort.rs:47:20 | 47 | listToSort.push(irandom(maxInt)); | ^^^^^^^ not found in this scope error: aborting due to 14 previous errors Some errors have detailed explanations: E0277, E0308, E0425. For more information about an error, try `rustc --explain E0277`.

Solution