|
| 1 | +# `gemma` |
| 2 | + |
| 3 | +## Get Model |
| 4 | + |
| 5 | +```console |
| 6 | +wget https://huggingface.co/second-state/Gemma-2b-it-GGUF/resolve/main/gemma-2b-it-Q4_K_M.gguf |
| 7 | +``` |
| 8 | + |
| 9 | +## Execute |
| 10 | + |
| 11 | +````console |
| 12 | +$ wasmedge --dir .:. \ |
| 13 | + --nn-preload default:GGML:AUTO:gemma-2b-it-Q4_K_M.gguf \ |
| 14 | + ./wasmedge-ggml-gemma.wasm default |
| 15 | + |
| 16 | +USER: |
| 17 | +Write a merge sort in Rust for me |
| 18 | +ASSISTANT: |
| 19 | +```rust |
| 20 | +fn merge_sort(arr: &mut [i32]) { |
| 21 | + if arr.len() <= 1 { |
| 22 | + return; |
| 23 | + } |
| 24 | + let mid = arr.len() / 2; |
| 25 | + let (left, right) = arr.split_at_mut(mid); |
| 26 | + |
| 27 | + merge_sort(left); |
| 28 | + merge_sort(right); |
| 29 | + |
| 30 | + merge(arr, left, right); |
| 31 | +} |
| 32 | + |
| 33 | +fn merge(arr: &mut [i32], left: &mut [i32], right: &mut [i32]) { |
| 34 | + let mut merged = vec![]; |
| 35 | + |
| 36 | + for i in 0..left.len() { |
| 37 | + merged.push(*left[i]); |
| 38 | + } |
| 39 | + |
| 40 | + for j in 0..right.len() { |
| 41 | + merged.push(*right[j]); |
| 42 | + } |
| 43 | + |
| 44 | + arr.clear(); |
| 45 | + for item in merged { |
| 46 | + arr.push(item); |
| 47 | + } |
| 48 | +} |
| 49 | +``` |
| 50 | + |
| 51 | +**Usage:** |
| 52 | + |
| 53 | +```rust |
| 54 | +let mut numbers = vec![5, 2, 8, 3, 1, 9, 4]; |
| 55 | +merge_sort(&mut numbers); |
| 56 | +println!("{:?}", numbers); // Output: [1, 2, 3, 4, 5, 8, 9] |
| 57 | +``` |
| 58 | + |
| 59 | +**Explanation:** |
| 60 | + |
| 61 | +The `merge` function takes three slices as input and merges them into a single sorted slice. It works by iterating over the two input slices and adding each element to the merged slice until one of the slices is empty. |
| 62 | + |
| 63 | +* `left` contains the first half of the elements. |
| 64 | +* `right` contains the second half of the elements. |
| 65 | +* The merged slice is stored in `arr`. |
| 66 | + |
| 67 | +**Time complexity:** |
| 68 | + |
| 69 | +Merge sort has a time complexity of O(n log n), where n is the length of the input vector. This is because the function divides the problem into smaller subproblems until each subproblem contains only one element. |
| 70 | +USER: |
| 71 | +How about in C++? |
| 72 | +ASSISTANT: |
| 73 | +```c++ |
| 74 | +#include <iostream> |
| 75 | +#include <vector> |
| 76 | + |
| 77 | +using namespace std; |
| 78 | + |
| 79 | +void merge(int arr[], int left[], int right[]) { |
| 80 | + int n = sizeof(arr) / 2; |
| 81 | + int i = 0, j = 0, k = 0; |
| 82 | + |
| 83 | + while (i < n && j < left[n]) { |
| 84 | + if (left[i] <= right[j]) { |
| 85 | + arr[k++] = left[i++]; |
| 86 | + } else { |
| 87 | + arr[k++] = right[j++]; |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + while (i < n) { |
| 92 | + arr[k++] = left[i++]; |
| 93 | + } |
| 94 | + |
| 95 | + while (j < left[n]) { |
| 96 | + arr[k++] = right[j++]; |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +void merge_sort(vector<int>& arr) { |
| 101 | + if (arr.size() <= 1) { |
| 102 | + return; |
| 103 | + } |
| 104 | + int mid = arr.size() / 2; |
| 105 | + merge_sort(arr.data() + mid); |
| 106 | + merge_sort(arr.data() + mid * 2); |
| 107 | + merge(arr.data(), arr.data() + mid, arr.data() + 2 * mid); |
| 108 | +} |
| 109 | + |
| 110 | +int main() { |
| 111 | + vector<int> numbers = {5, 2, 8, 3, 1, 9, 4}; |
| 112 | + merge_sort(numbers); |
| 113 | + cout << "{:?}", numbers) << endl; |
| 114 | + return 0; |
| 115 | +} |
| 116 | +``` |
| 117 | + |
| 118 | +**Output:** |
| 119 | + |
| 120 | +``` |
| 121 | +{:?}", [1, 2, 3, 4, 5, 8, 9] |
| 122 | +``` |
| 123 | +```` |
0 commit comments