This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| fn size_base10(x: usize) -> usize { | |
| let x_string: String = x.to_string(); | |
| x_string.chars().count() | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| fn split_string(x: usize, m: usize) -> (usize, usize) { | |
| let x_string: String = x.to_string(); | |
| let x_string_len: usize = x_string.chars().count(); | |
| let x0: usize = x_string[0..(x_string_len - m)].parse().unwrap(); | |
| let x1: usize = x_string[(x_string_len - m)..x_string_len].parse().unwrap(); | |
| (x0, x1) | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| fn karatsuba(x: usize, y: usize) -> u32 { | |
| if x < 10 || y < 10 { | |
| (x * y) as u32 | |
| } else { | |
| let m: usize = std::cmp::min(size_base10(x), size_base10(y)) / 2; | |
| let tuple = split_string(x, m); | |
| let high1 = tuple.0; | |
| let low1 = tuple.1; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| fn count_inversion(arr: &[usize]) -> usize { | |
| let a_len = arr.len(); | |
| let mut count = 0; | |
| for i in 0..a_len { | |
| for j in i..a_len { | |
| if arr[j] < arr[i] { | |
| count += 1; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| fn merge_sort(vec: Vec<usize>) -> Vec<usize> { | |
| let a_vec = vec.len(); | |
| if a_vec > 1 { | |
| let mut vector = Vec::new(); | |
| let middle = a_vec/2; | |
| let left = &vec[..middle]; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| fn partition(l: usize, r: usize, arr: &mut [usize]) -> usize { | |
| let pivot = arr[r]; | |
| let mut ptr = l; | |
| for i in l..r { | |
| if arr[i] <= pivot { | |
| arr.swap(i, ptr); | |
| ptr += 1; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| fn quick_sort(l: usize, r: usize, arr: &mut [usize]) -> usize{ | |
| if arr.len() == 1 { | |
| return 0; | |
| } | |
| if l < r { | |
| let pi = partition(l, r, arr); | |
| quick_sort(l, pi - 1, arr); | |
| quick_sort(pi + 1, r, arr); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| fn bubble_sort(arr: &mut [usize]) { | |
| let a_len = arr.len(); | |
| for i in 0..a_len{ | |
| for j in 0..(a_len - i - 1) { | |
| if arr[j] > arr[j + 1] { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| fn selection_sort(arr: &mut [usize]) { | |
| let a_len = arr.len(); | |
| for i in 0..a_len | |
| { | |
| let mut minimum: usize = usize::pow(2, 64 - 1); | |
| let mut min_pos: usize = 0; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| fn first_word(s: &String) -> &str { | |
| let bytes = s.as_bytes(); | |
| for (i, &item) in bytes.iter().enumerate() { | |
| if item == b' ' { | |
| return &s[0..i]; | |
| } | |
| } | |
| &s[..] |
NewerOlder