n1ck-guo commited on
Commit
e6ef2d7
·
verified ·
1 Parent(s): fd349f7

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +133 -1
README.md CHANGED
@@ -14,11 +14,143 @@ Please follow the license of the original model.
14
  Llamacpp inference
15
 
16
  ~~~bash
17
- /llama-cli -hf Intel/gpt-oss-20b-gguf-q4ks-AutoRound
18
  ~~~
19
 
20
  ~~~bash
21
  > Write a quick sort algorithm.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
22
  // put pivot into final place
23
  T tmp = a[left]; a[left] = a[j]; a[j] = tmp;
24
  return j;
 
14
  Llamacpp inference
15
 
16
  ~~~bash
17
+ llama-cli -hf Intel/gpt-oss-20b-gguf-q4ks-AutoRound
18
  ~~~
19
 
20
  ~~~bash
21
  > Write a quick sort algorithm.
22
+ <|channel|>analysis<|message|>We need to write quick sort algorithm. The user wants a quick sort algorithm. Provide code? They didn't specify language. I can provide a general quick sort algorithm, maybe in Python, C++, Java, etc. Provide explanation. Provide typical implementation. I'll respond with explanation and code.<|start|>assistant<|channel|>final<|message|>Below is a minimal, self‑contained quick‑sort implementation in a few common languages.
23
+ Feel free to copy‑paste the snippet that matches your stack, or use it as a template and adapt it to your needs.
24
+
25
+ ---
26
+
27
+ ## Quick‑Sort – Concept
28
+
29
+ 1. **Choose a pivot** – usually the first element (or median of three, etc.).
30
+ 2. **Partition** the array into two sub‑arrays:
31
+ * elements `< pivot`
32
+ * elements `>= pivot` (or `> pivot` if you want to avoid duplicates)
33
+ 3. **Recursively sort** the two sub‑arrays.
34
+ 4. **Concatenate** the sorted left part, the pivot, and the sorted right part.
35
+
36
+ The algorithm is *in‑place* (no extra array for the whole list) and runs in average \(O(n \log n)\), worst‑case \(O(n^2)\) if the pivot is poorly chosen.
37
+
38
+ ---
39
+
40
+ ## 1. Python (in‑place)
41
+
42
+ ```python
43
+ def quick_sort(arr, left=0, right=None):
44
+ """
45
+ Sorts `arr` in place using the Quick‑Sort algorithm.
46
+ """
47
+ if right is None:
48
+ right = len(arr) - 1
49
+
50
+ def _partition(lo, hi):
51
+ # choose the first element as pivot
52
+ pivot = arr[lo]
53
+ i, j = lo + 1, hi
54
+ while True:
55
+ while i <= hi and arr[i] < pivot:
56
+ i += 1
57
+ while j >= lo + 1 and arr[j] > pivot:
58
+ j -= 1
59
+ if i >= j:
60
+ break
61
+ arr[i], arr[j] = arr[j], arr[i]
62
+ # place pivot in its final position
63
+ arr[lo], arr[j] = arr[j], arr[lo]
64
+ return j
65
+
66
+ if left < right:
67
+ p = _partition(left, right)
68
+ quick_sort(arr, left, p - 1)
69
+ quick_sort(arr, p + 1, right)
70
+
71
+ # ---- Example ----------------------------------------------------
72
+ data = [3, 6, 8, 10, 1, 2, 1]
73
+ quick_sort(data)
74
+ print(data) # -> [1, 1, 2, 3, 6, 8, 10]
75
+ ```
76
+
77
+ **Notes**
78
+
79
+ * The helper `_partition` returns the final index of the pivot.
80
+ * The algorithm is *stable only if you change the comparison* to `<=` or `>=` depending on the ordering you need.
81
+ * For very large lists you may want to switch to a non‑recursive implementation to avoid hitting Python’s recursion depth limit.
82
+
83
+ ---
84
+
85
+ ## 2. C++ (in‑place, STL‑like)
86
+
87
+ ```cpp
88
+ #include <vector>
89
+ #include <algorithm> // for std::swap
90
+
91
+ template<typename T>
92
+ void quick_sort(std::vector<T>& a, int left = 0, int right = -1)
93
+ {
94
+ if (right == -1) right = static_cast<int>(a.size()) - 1;
95
+
96
+ auto partition = [&](int lo, int hi) -> int {
97
+ T pivot = a[lo];
98
+ int i = lo + 1, j = hi;
99
+ while (true) {
100
+ while (i <= hi && a[i] < pivot) ++i;
101
+ while (j >= lo + 1 && a[j] > pivot) --j;
102
+ if (i >= j) break;
103
+ std::swap(a[i], a[j]);
104
+ }
105
+ std::swap(a[lo], a[j]); // pivot in place
106
+ return j;
107
+ };
108
+
109
+ if (left < right) {
110
+ int p = partition(left, right);
111
+ quick_sort(a, left, p - 1);
112
+ quick_sort(a, p + 1, right);
113
+ }
114
+ }
115
+
116
+ // ---- Example ----------------------------------------------------
117
+ int main() {
118
+ std::vector<int> v{3, 6, 8, 10, 1, 2, 1};
119
+ quick_sort(v);
120
+ for (int x : v) std::cout << x << ' ';
121
+ // Output: 1 1 2 3 6 8 10
122
+ }
123
+ ```
124
+
125
+ ---
126
+
127
+ ## 3. Java (in‑place)
128
+
129
+ ```java
130
+ public static <T extends Comparable<? super T>> void quickSort(T[] a) {
131
+ quickSort(a, 0, a.length - 1);
132
+ }
133
+
134
+ private static <T extends Comparable<? super T>> void quickSort(T[] a,
135
+ int left, int right) {
136
+ if (left >= right) return;
137
+
138
+ int pivotIdx = partition(a, left, right);
139
+ quickSort(a, left, pivotIdx - 1);
140
+ quickSort(a, pivotIdx + 1, right);
141
+ }
142
+
143
+ private static <T extends Comparable<? super T>> int partition(T[] a,
144
+ int left, int right) {
145
+ T pivot = a[left];
146
+ int i = left + 1, j = right;
147
+
148
+ while (true) {
149
+ while (i <= right && a[i].compareTo(pivot) < 0) i++;
150
+ while (j >= left + 1 && a[j].compareTo(pivot) > 0) j--;
151
+ if (i >= j) break;
152
+ T tmp = a[i]; a[i] = a[j]; a[j] = tmp;
153
+ }
154
  // put pivot into final place
155
  T tmp = a[left]; a[left] = a[j]; a[j] = tmp;
156
  return j;