Views
No views yet
npm i @huggingface/transformersonnx-community/Qwen2.5-Coder-1.5B-Instruct.1import { pipeline } from "@huggingface/transformers";
2
3// Create a text generation pipeline
4const generator = await pipeline(
5 "text-generation",
6 "onnx-community/Qwen2.5-Coder-1.5B-Instruct",
7 { dtype: "q4" },
8);
9
10// Define the list of messages
11const messages = [
12 { role: "system", content: "You are a helpful assistant." },
13 { role: "user", content: "Write a quick sort algorithm." },
14];
15
16// Generate a response
17const output = await generator(messages, { max_new_tokens: 512, do_sample: false });
18console.log(output[0].generated_text.at(-1).content);Sure! Below is the implementation of the QuickSort algorithm in Python:
```python
def quicksort(arr):
if len(arr) <= 1:
return arr
pivot = arr[len(arr) // 2]
left = [x for x in arr if x < pivot]
middle = [x for x in arr if x == pivot]
right = [x for x in arr if x > pivot]
return quicksort(left) + middle + quicksort(right)
# Example usage:
arr = [3, 6, 8, 10, 1, 2, 1]
print("Original array:", arr)
sorted_arr = quicksort(arr)
print("Sorted array:", sorted_arr)
```
### Explanation:
- **Base Case**: If the length of the list `arr` is less than or equal to one (`len(arr) <= 1`), it means the list is already sorted and can be returned as it is.
- **Pivot Selection**: The chosen `pivot` element can be any element from the list (e.g., `len(arr)//2`). For simplicity here we choose this way.
- **Partitioning**:
- Elements less than or equal to `pivot` are placed into a new list called `left`.
- Elements equal to `pivot` are placed into another new list called `middle`.
- Elements greater than or equal to `pivot` are placed into yet another new list called `right`.
- **Recursive Sorting**: The function recursively applies itself on these three lists (`left`, middle`, and right`) and concatenates them back together.
This implementation ensures that all elements less than or equal to any given element will appear before that element in their respective partitions.onnx).