Views
No views yet
Generate the model for more details./llama-cli -hf Intel/Qwen3-Coder-30B-A3B-Instruct-gguf-q4km-AutoRound1> Write a quick sort algorithm.
2Here's a quick sort algorithm implementation in Python:
3
4```python
5def quicksort(arr, low, high):
6 """
7 Quick sort implementation
8 arr: array to be sorted
9 low: starting index
10 high: ending index
11 """
12 if low < high:
13 # Partition the array and get pivot index
14 pivot_index = partition(arr, low, high)
15
16 # Recursively sort elements before and after partition
17 quicksort(arr, low, pivot_index - 1)
18 quicksort(arr, pivot_index + 1, high)
19
20def partition(arr, low, high):
21 """
22 Partition function using last element as pivot
23 """
24 # Choose the rightmost element as pivot
25 pivot = arr[high]
26
27 # Index of smaller element (indicates right position of pivot)
28 i = low - 1
29
30 for j in range(low, high):
31 # If current element is smaller than or equal to pivot
32 if arr[j] <= pivot:
33 i += 1
34 arr[i], arr[j] = arr[j], arr[i] # Swap elements
35
36 # Place pivot in its correct position
37 arr[i + 1], arr[high] = arr[high], arr[i + 1]
38 return i + 1
39
40# Wrapper function for easier use
41def quick_sort(arr):
42 if len(arr) <= 1:
43 return arr
44 quicksort(arr, 0, len(arr) - 1)
45 return arr
46
47# Example usage
48if __name__ == "__main__":
49 # Test the algorithm
50 test_array = [64, 34, 25, 12, 22, 11, 90]
51 print("Original array:", test_array)
52
53 sorted_array = quick_sort(test_array.copy())
54 print("Sorted array:", sorted_array)
55
56 # Test with edge cases
57 print("Empty array:", quick_sort([]))
58 print("Single element:", quick_sort([42]))
59 print("Already sorted:", quick_sort([1, 2, 3, 4, 5]))
60 print("Reverse sorted:", quick_sort([5, 4, 3, 2, 1]))
61```
62
63**How it works:**
64
651. **Choose a pivot**: Select an element from the array (here we use the last element)
662. **Partition**: Rearrange the array so that:
67 - Elements smaller than the pivot go to the left
68 - Elements greater than the pivot go to the right
693. **Recursively sort**: Apply the same process to the sub-arrays on both sides of the pivot
70
71**Time Complexity:**
72- Best/Average case: O(n log n)
73- Worst case: O(n²) - when pivot is always the smallest or largest element
74
75**Space Complexity:** O(log n) - due to recursion stack
76
77**Key Features:**
78- In-place sorting (modifies original array)
79- Not stable (doesn't preserve relative order of equal elements)
80- Efficient for large datasets
81- Good average performance
82
83The algorithm handles edge cases like empty arrays, single elements, and already sorted arrays.
84auto_round --format gguf:q4_k_m --iters 0 --nsamples 512 --dataset github-code-clean --model Qwen/Qwen3-Coder-30B-A3B-Instruct/ --output_dir tmp_autoround