Qwen3-Coder-30B-A3B-Instruct-int4-AutoRound
582
3
30.0B
license:apache-2.0
by
Intel
Code Model
OTHER
30B params
New
582 downloads
Early-stage
Edge AI:
Mobile
Laptop
Server
68GB+ RAM
Mobile
Laptop
Server
Quick Summary
This model is an int4 model with groupsize 128 and symmetric quantization of Qwen/Qwen3-Coder-30B-A3B-Instruct generated by intel/auto-round algorithm.
Device Compatibility
Mobile
4-6GB RAM
Laptop
16GB RAM
Server
GPU
Minimum Recommended
28GB+ RAM
Code Examples
Example usage:python
def quicksort(arr):
'''
Sorts an array using the quicksort algorithm.
Args:
arr: List of comparable elements
Returns:
None (sorts in-place)
'''
if len(arr) <= 1:
return
def partition(low, high):
'''Partition function using the last element as pivot'''
pivot = arr[high]
i = low - 1 # Index of smaller element
for j in range(low, high):
if arr[j] <= pivot:
i += 1
arr[i], arr[j] = arr[j], arr[i] # Swap elements
arr[i + 1], arr[high] = arr[high], arr[i + 1] # Place pivot in correct position
return i + 1
def quicksort_helper(low, high):
'''Recursive helper function'''
if low < high:
# Partition the array and get pivot index
pi = partition(low, high)
# Recursively sort elements before and after partition
quicksort_helper(low, pi - 1)
quicksort_helper(pi + 1, high)
quicksort_helper(0, len(arr) - 1)
# Example usage:
if __name__ == "__main__":
# Test the algorithm
test_array = [64, 34, 25, 12, 22, 11, 90]
print("Original array:", test_array)
quicksort(test_array)
print("Sorted array:", test_array)
# Test with other examples
test_cases = [
[5, 2, 8, 1, 9],
[1],
[],
[3, 3, 3, 3],
[5, 4, 3, 2, 1]
]
for i, case in enumerate(test_cases):
original = case.copy()
quicksort(case)
print(f"Test {i+1}: {original} → {case}")
**How it works:**
1. **Divide**: Choose a "pivot" element and partition the array so that elements smaller than the pivot are on the left, and larger elements are on the right.
2. **Conquer**: Recursively apply quicksort to the sub-arrays on both sides of the pivot.
3. **Combine**: Since we're sorting in-place, no additional combining step is needed.
**Key features:**
- **Time Complexity**: O(n log n) average case, O(n²) worst case
- **Space Complexity**: O(log n) due to recursion stack
- **In-place sorting**: Modifies the original array
- **Not stable**: Relative order of equal elements may change
**Alternative version with random pivot selection** (better average performance):Example usage:python
def quicksort(arr):
'''
Sorts an array using the quicksort algorithm.
Args:
arr: List of comparable elements
Returns:
None (sorts in-place)
'''
if len(arr) <= 1:
return
def partition(low, high):
'''Partition function using the last element as pivot'''
pivot = arr[high]
i = low - 1 # Index of smaller element
for j in range(low, high):
if arr[j] <= pivot:
i += 1
arr[i], arr[j] = arr[j], arr[i] # Swap elements
arr[i + 1], arr[high] = arr[high], arr[i + 1] # Place pivot in correct position
return i + 1
def quicksort_helper(low, high):
'''Recursive helper function'''
if low < high:
# Partition the array and get pivot index
pi = partition(low, high)
# Recursively sort elements before and after partition
quicksort_helper(low, pi - 1)
quicksort_helper(pi + 1, high)
quicksort_helper(0, len(arr) - 1)
# Example usage:
if __name__ == "__main__":
# Test the algorithm
test_array = [64, 34, 25, 12, 22, 11, 90]
print("Original array:", test_array)
quicksort(test_array)
print("Sorted array:", test_array)
# Test with other examples
test_cases = [
[5, 2, 8, 1, 9],
[1],
[],
[3, 3, 3, 3],
[5, 4, 3, 2, 1]
]
for i, case in enumerate(test_cases):
original = case.copy()
quicksort(case)
print(f"Test {i+1}: {original} → {case}")
**How it works:**
1. **Divide**: Choose a "pivot" element and partition the array so that elements smaller than the pivot are on the left, and larger elements are on the right.
2. **Conquer**: Recursively apply quicksort to the sub-arrays on both sides of the pivot.
3. **Combine**: Since we're sorting in-place, no additional combining step is needed.
**Key features:**
- **Time Complexity**: O(n log n) average case, O(n²) worst case
- **Space Complexity**: O(log n) due to recursion stack
- **In-place sorting**: Modifies the original array
- **Not stable**: Relative order of equal elements may change
**Alternative version with random pivot selection** (better average performance):Example usage:python
def quicksort(arr):
'''
Sorts an array using the quicksort algorithm.
Args:
arr: List of comparable elements
Returns:
None (sorts in-place)
'''
if len(arr) <= 1:
return
def partition(low, high):
'''Partition function using the last element as pivot'''
pivot = arr[high]
i = low - 1 # Index of smaller element
for j in range(low, high):
if arr[j] <= pivot:
i += 1
arr[i], arr[j] = arr[j], arr[i] # Swap elements
arr[i + 1], arr[high] = arr[high], arr[i + 1] # Place pivot in correct position
return i + 1
def quicksort_helper(low, high):
'''Recursive helper function'''
if low < high:
# Partition the array and get pivot index
pi = partition(low, high)
# Recursively sort elements before and after partition
quicksort_helper(low, pi - 1)
quicksort_helper(pi + 1, high)
quicksort_helper(0, len(arr) - 1)
# Example usage:
if __name__ == "__main__":
# Test the algorithm
test_array = [64, 34, 25, 12, 22, 11, 90]
print("Original array:", test_array)
quicksort(test_array)
print("Sorted array:", test_array)
# Test with other examples
test_cases = [
[5, 2, 8, 1, 9],
[1],
[],
[3, 3, 3, 3],
[5, 4, 3, 2, 1]
]
for i, case in enumerate(test_cases):
original = case.copy()
quicksort(case)
print(f"Test {i+1}: {original} → {case}")
**How it works:**
1. **Divide**: Choose a "pivot" element and partition the array so that elements smaller than the pivot are on the left, and larger elements are on the right.
2. **Conquer**: Recursively apply quicksort to the sub-arrays on both sides of the pivot.
3. **Combine**: Since we're sorting in-place, no additional combining step is needed.
**Key features:**
- **Time Complexity**: O(n log n) average case, O(n²) worst case
- **Space Complexity**: O(log n) due to recursion stack
- **In-place sorting**: Modifies the original array
- **Not stable**: Relative order of equal elements may change
**Alternative version with random pivot selection** (better average performance):Example usage:python
def quicksort(arr):
'''
Sorts an array using the quicksort algorithm.
Args:
arr: List of comparable elements
Returns:
None (sorts in-place)
'''
if len(arr) <= 1:
return
def partition(low, high):
'''Partition function using the last element as pivot'''
pivot = arr[high]
i = low - 1 # Index of smaller element
for j in range(low, high):
if arr[j] <= pivot:
i += 1
arr[i], arr[j] = arr[j], arr[i] # Swap elements
arr[i + 1], arr[high] = arr[high], arr[i + 1] # Place pivot in correct position
return i + 1
def quicksort_helper(low, high):
'''Recursive helper function'''
if low < high:
# Partition the array and get pivot index
pi = partition(low, high)
# Recursively sort elements before and after partition
quicksort_helper(low, pi - 1)
quicksort_helper(pi + 1, high)
quicksort_helper(0, len(arr) - 1)
# Example usage:
if __name__ == "__main__":
# Test the algorithm
test_array = [64, 34, 25, 12, 22, 11, 90]
print("Original array:", test_array)
quicksort(test_array)
print("Sorted array:", test_array)
# Test with other examples
test_cases = [
[5, 2, 8, 1, 9],
[1],
[],
[3, 3, 3, 3],
[5, 4, 3, 2, 1]
]
for i, case in enumerate(test_cases):
original = case.copy()
quicksort(case)
print(f"Test {i+1}: {original} → {case}")
**How it works:**
1. **Divide**: Choose a "pivot" element and partition the array so that elements smaller than the pivot are on the left, and larger elements are on the right.
2. **Conquer**: Recursively apply quicksort to the sub-arrays on both sides of the pivot.
3. **Combine**: Since we're sorting in-place, no additional combining step is needed.
**Key features:**
- **Time Complexity**: O(n log n) average case, O(n²) worst case
- **Space Complexity**: O(log n) due to recursion stack
- **In-place sorting**: Modifies the original array
- **Not stable**: Relative order of equal elements may change
**Alternative version with random pivot selection** (better average performance):Example usage:python
def quicksort(arr):
'''
Sorts an array using the quicksort algorithm.
Args:
arr: List of comparable elements
Returns:
None (sorts in-place)
'''
if len(arr) <= 1:
return
def partition(low, high):
'''Partition function using the last element as pivot'''
pivot = arr[high]
i = low - 1 # Index of smaller element
for j in range(low, high):
if arr[j] <= pivot:
i += 1
arr[i], arr[j] = arr[j], arr[i] # Swap elements
arr[i + 1], arr[high] = arr[high], arr[i + 1] # Place pivot in correct position
return i + 1
def quicksort_helper(low, high):
'''Recursive helper function'''
if low < high:
# Partition the array and get pivot index
pi = partition(low, high)
# Recursively sort elements before and after partition
quicksort_helper(low, pi - 1)
quicksort_helper(pi + 1, high)
quicksort_helper(0, len(arr) - 1)
# Example usage:
if __name__ == "__main__":
# Test the algorithm
test_array = [64, 34, 25, 12, 22, 11, 90]
print("Original array:", test_array)
quicksort(test_array)
print("Sorted array:", test_array)
# Test with other examples
test_cases = [
[5, 2, 8, 1, 9],
[1],
[],
[3, 3, 3, 3],
[5, 4, 3, 2, 1]
]
for i, case in enumerate(test_cases):
original = case.copy()
quicksort(case)
print(f"Test {i+1}: {original} → {case}")
**How it works:**
1. **Divide**: Choose a "pivot" element and partition the array so that elements smaller than the pivot are on the left, and larger elements are on the right.
2. **Conquer**: Recursively apply quicksort to the sub-arrays on both sides of the pivot.
3. **Combine**: Since we're sorting in-place, no additional combining step is needed.
**Key features:**
- **Time Complexity**: O(n log n) average case, O(n²) worst case
- **Space Complexity**: O(log n) due to recursion stack
- **In-place sorting**: Modifies the original array
- **Not stable**: Relative order of equal elements may change
**Alternative version with random pivot selection** (better average performance):Example usage:python
def quicksort(arr):
'''
Sorts an array using the quicksort algorithm.
Args:
arr: List of comparable elements
Returns:
None (sorts in-place)
'''
if len(arr) <= 1:
return
def partition(low, high):
'''Partition function using the last element as pivot'''
pivot = arr[high]
i = low - 1 # Index of smaller element
for j in range(low, high):
if arr[j] <= pivot:
i += 1
arr[i], arr[j] = arr[j], arr[i] # Swap elements
arr[i + 1], arr[high] = arr[high], arr[i + 1] # Place pivot in correct position
return i + 1
def quicksort_helper(low, high):
'''Recursive helper function'''
if low < high:
# Partition the array and get pivot index
pi = partition(low, high)
# Recursively sort elements before and after partition
quicksort_helper(low, pi - 1)
quicksort_helper(pi + 1, high)
quicksort_helper(0, len(arr) - 1)
# Example usage:
if __name__ == "__main__":
# Test the algorithm
test_array = [64, 34, 25, 12, 22, 11, 90]
print("Original array:", test_array)
quicksort(test_array)
print("Sorted array:", test_array)
# Test with other examples
test_cases = [
[5, 2, 8, 1, 9],
[1],
[],
[3, 3, 3, 3],
[5, 4, 3, 2, 1]
]
for i, case in enumerate(test_cases):
original = case.copy()
quicksort(case)
print(f"Test {i+1}: {original} → {case}")
**How it works:**
1. **Divide**: Choose a "pivot" element and partition the array so that elements smaller than the pivot are on the left, and larger elements are on the right.
2. **Conquer**: Recursively apply quicksort to the sub-arrays on both sides of the pivot.
3. **Combine**: Since we're sorting in-place, no additional combining step is needed.
**Key features:**
- **Time Complexity**: O(n log n) average case, O(n²) worst case
- **Space Complexity**: O(log n) due to recursion stack
- **In-place sorting**: Modifies the original array
- **Not stable**: Relative order of equal elements may change
**Alternative version with random pivot selection** (better average performance):Example usage:python
def quicksort(arr):
'''
Sorts an array using the quicksort algorithm.
Args:
arr: List of comparable elements
Returns:
None (sorts in-place)
'''
if len(arr) <= 1:
return
def partition(low, high):
'''Partition function using the last element as pivot'''
pivot = arr[high]
i = low - 1 # Index of smaller element
for j in range(low, high):
if arr[j] <= pivot:
i += 1
arr[i], arr[j] = arr[j], arr[i] # Swap elements
arr[i + 1], arr[high] = arr[high], arr[i + 1] # Place pivot in correct position
return i + 1
def quicksort_helper(low, high):
'''Recursive helper function'''
if low < high:
# Partition the array and get pivot index
pi = partition(low, high)
# Recursively sort elements before and after partition
quicksort_helper(low, pi - 1)
quicksort_helper(pi + 1, high)
quicksort_helper(0, len(arr) - 1)
# Example usage:
if __name__ == "__main__":
# Test the algorithm
test_array = [64, 34, 25, 12, 22, 11, 90]
print("Original array:", test_array)
quicksort(test_array)
print("Sorted array:", test_array)
# Test with other examples
test_cases = [
[5, 2, 8, 1, 9],
[1],
[],
[3, 3, 3, 3],
[5, 4, 3, 2, 1]
]
for i, case in enumerate(test_cases):
original = case.copy()
quicksort(case)
print(f"Test {i+1}: {original} → {case}")
**How it works:**
1. **Divide**: Choose a "pivot" element and partition the array so that elements smaller than the pivot are on the left, and larger elements are on the right.
2. **Conquer**: Recursively apply quicksort to the sub-arrays on both sides of the pivot.
3. **Combine**: Since we're sorting in-place, no additional combining step is needed.
**Key features:**
- **Time Complexity**: O(n log n) average case, O(n²) worst case
- **Space Complexity**: O(log n) due to recursion stack
- **In-place sorting**: Modifies the original array
- **Not stable**: Relative order of equal elements may change
**Alternative version with random pivot selection** (better average performance):Example usage:python
def quicksort(arr):
'''
Sorts an array using the quicksort algorithm.
Args:
arr: List of comparable elements
Returns:
None (sorts in-place)
'''
if len(arr) <= 1:
return
def partition(low, high):
'''Partition function using the last element as pivot'''
pivot = arr[high]
i = low - 1 # Index of smaller element
for j in range(low, high):
if arr[j] <= pivot:
i += 1
arr[i], arr[j] = arr[j], arr[i] # Swap elements
arr[i + 1], arr[high] = arr[high], arr[i + 1] # Place pivot in correct position
return i + 1
def quicksort_helper(low, high):
'''Recursive helper function'''
if low < high:
# Partition the array and get pivot index
pi = partition(low, high)
# Recursively sort elements before and after partition
quicksort_helper(low, pi - 1)
quicksort_helper(pi + 1, high)
quicksort_helper(0, len(arr) - 1)
# Example usage:
if __name__ == "__main__":
# Test the algorithm
test_array = [64, 34, 25, 12, 22, 11, 90]
print("Original array:", test_array)
quicksort(test_array)
print("Sorted array:", test_array)
# Test with other examples
test_cases = [
[5, 2, 8, 1, 9],
[1],
[],
[3, 3, 3, 3],
[5, 4, 3, 2, 1]
]
for i, case in enumerate(test_cases):
original = case.copy()
quicksort(case)
print(f"Test {i+1}: {original} → {case}")
**How it works:**
1. **Divide**: Choose a "pivot" element and partition the array so that elements smaller than the pivot are on the left, and larger elements are on the right.
2. **Conquer**: Recursively apply quicksort to the sub-arrays on both sides of the pivot.
3. **Combine**: Since we're sorting in-place, no additional combining step is needed.
**Key features:**
- **Time Complexity**: O(n log n) average case, O(n²) worst case
- **Space Complexity**: O(log n) due to recursion stack
- **In-place sorting**: Modifies the original array
- **Not stable**: Relative order of equal elements may change
**Alternative version with random pivot selection** (better average performance):Example usage:python
def quicksort(arr):
'''
Sorts an array using the quicksort algorithm.
Args:
arr: List of comparable elements
Returns:
None (sorts in-place)
'''
if len(arr) <= 1:
return
def partition(low, high):
'''Partition function using the last element as pivot'''
pivot = arr[high]
i = low - 1 # Index of smaller element
for j in range(low, high):
if arr[j] <= pivot:
i += 1
arr[i], arr[j] = arr[j], arr[i] # Swap elements
arr[i + 1], arr[high] = arr[high], arr[i + 1] # Place pivot in correct position
return i + 1
def quicksort_helper(low, high):
'''Recursive helper function'''
if low < high:
# Partition the array and get pivot index
pi = partition(low, high)
# Recursively sort elements before and after partition
quicksort_helper(low, pi - 1)
quicksort_helper(pi + 1, high)
quicksort_helper(0, len(arr) - 1)
# Example usage:
if __name__ == "__main__":
# Test the algorithm
test_array = [64, 34, 25, 12, 22, 11, 90]
print("Original array:", test_array)
quicksort(test_array)
print("Sorted array:", test_array)
# Test with other examples
test_cases = [
[5, 2, 8, 1, 9],
[1],
[],
[3, 3, 3, 3],
[5, 4, 3, 2, 1]
]
for i, case in enumerate(test_cases):
original = case.copy()
quicksort(case)
print(f"Test {i+1}: {original} → {case}")
**How it works:**
1. **Divide**: Choose a "pivot" element and partition the array so that elements smaller than the pivot are on the left, and larger elements are on the right.
2. **Conquer**: Recursively apply quicksort to the sub-arrays on both sides of the pivot.
3. **Combine**: Since we're sorting in-place, no additional combining step is needed.
**Key features:**
- **Time Complexity**: O(n log n) average case, O(n²) worst case
- **Space Complexity**: O(log n) due to recursion stack
- **In-place sorting**: Modifies the original array
- **Not stable**: Relative order of equal elements may change
**Alternative version with random pivot selection** (better average performance):Example usage:python
def quicksort(arr):
'''
Sorts an array using the quicksort algorithm.
Args:
arr: List of comparable elements
Returns:
None (sorts in-place)
'''
if len(arr) <= 1:
return
def partition(low, high):
'''Partition function using the last element as pivot'''
pivot = arr[high]
i = low - 1 # Index of smaller element
for j in range(low, high):
if arr[j] <= pivot:
i += 1
arr[i], arr[j] = arr[j], arr[i] # Swap elements
arr[i + 1], arr[high] = arr[high], arr[i + 1] # Place pivot in correct position
return i + 1
def quicksort_helper(low, high):
'''Recursive helper function'''
if low < high:
# Partition the array and get pivot index
pi = partition(low, high)
# Recursively sort elements before and after partition
quicksort_helper(low, pi - 1)
quicksort_helper(pi + 1, high)
quicksort_helper(0, len(arr) - 1)
# Example usage:
if __name__ == "__main__":
# Test the algorithm
test_array = [64, 34, 25, 12, 22, 11, 90]
print("Original array:", test_array)
quicksort(test_array)
print("Sorted array:", test_array)
# Test with other examples
test_cases = [
[5, 2, 8, 1, 9],
[1],
[],
[3, 3, 3, 3],
[5, 4, 3, 2, 1]
]
for i, case in enumerate(test_cases):
original = case.copy()
quicksort(case)
print(f"Test {i+1}: {original} → {case}")
**How it works:**
1. **Divide**: Choose a "pivot" element and partition the array so that elements smaller than the pivot are on the left, and larger elements are on the right.
2. **Conquer**: Recursively apply quicksort to the sub-arrays on both sides of the pivot.
3. **Combine**: Since we're sorting in-place, no additional combining step is needed.
**Key features:**
- **Time Complexity**: O(n log n) average case, O(n²) worst case
- **Space Complexity**: O(log n) due to recursion stack
- **In-place sorting**: Modifies the original array
- **Not stable**: Relative order of equal elements may change
**Alternative version with random pivot selection** (better average performance):Example usage:python
def quicksort(arr):
'''
Sorts an array using the quicksort algorithm.
Args:
arr: List of comparable elements
Returns:
None (sorts in-place)
'''
if len(arr) <= 1:
return
def partition(low, high):
'''Partition function using the last element as pivot'''
pivot = arr[high]
i = low - 1 # Index of smaller element
for j in range(low, high):
if arr[j] <= pivot:
i += 1
arr[i], arr[j] = arr[j], arr[i] # Swap elements
arr[i + 1], arr[high] = arr[high], arr[i + 1] # Place pivot in correct position
return i + 1
def quicksort_helper(low, high):
'''Recursive helper function'''
if low < high:
# Partition the array and get pivot index
pi = partition(low, high)
# Recursively sort elements before and after partition
quicksort_helper(low, pi - 1)
quicksort_helper(pi + 1, high)
quicksort_helper(0, len(arr) - 1)
# Example usage:
if __name__ == "__main__":
# Test the algorithm
test_array = [64, 34, 25, 12, 22, 11, 90]
print("Original array:", test_array)
quicksort(test_array)
print("Sorted array:", test_array)
# Test with other examples
test_cases = [
[5, 2, 8, 1, 9],
[1],
[],
[3, 3, 3, 3],
[5, 4, 3, 2, 1]
]
for i, case in enumerate(test_cases):
original = case.copy()
quicksort(case)
print(f"Test {i+1}: {original} → {case}")
**How it works:**
1. **Divide**: Choose a "pivot" element and partition the array so that elements smaller than the pivot are on the left, and larger elements are on the right.
2. **Conquer**: Recursively apply quicksort to the sub-arrays on both sides of the pivot.
3. **Combine**: Since we're sorting in-place, no additional combining step is needed.
**Key features:**
- **Time Complexity**: O(n log n) average case, O(n²) worst case
- **Space Complexity**: O(log n) due to recursion stack
- **In-place sorting**: Modifies the original array
- **Not stable**: Relative order of equal elements may change
**Alternative version with random pivot selection** (better average performance):Example usage:python
def quicksort(arr):
'''
Sorts an array using the quicksort algorithm.
Args:
arr: List of comparable elements
Returns:
None (sorts in-place)
'''
if len(arr) <= 1:
return
def partition(low, high):
'''Partition function using the last element as pivot'''
pivot = arr[high]
i = low - 1 # Index of smaller element
for j in range(low, high):
if arr[j] <= pivot:
i += 1
arr[i], arr[j] = arr[j], arr[i] # Swap elements
arr[i + 1], arr[high] = arr[high], arr[i + 1] # Place pivot in correct position
return i + 1
def quicksort_helper(low, high):
'''Recursive helper function'''
if low < high:
# Partition the array and get pivot index
pi = partition(low, high)
# Recursively sort elements before and after partition
quicksort_helper(low, pi - 1)
quicksort_helper(pi + 1, high)
quicksort_helper(0, len(arr) - 1)
# Example usage:
if __name__ == "__main__":
# Test the algorithm
test_array = [64, 34, 25, 12, 22, 11, 90]
print("Original array:", test_array)
quicksort(test_array)
print("Sorted array:", test_array)
# Test with other examples
test_cases = [
[5, 2, 8, 1, 9],
[1],
[],
[3, 3, 3, 3],
[5, 4, 3, 2, 1]
]
for i, case in enumerate(test_cases):
original = case.copy()
quicksort(case)
print(f"Test {i+1}: {original} → {case}")
**How it works:**
1. **Divide**: Choose a "pivot" element and partition the array so that elements smaller than the pivot are on the left, and larger elements are on the right.
2. **Conquer**: Recursively apply quicksort to the sub-arrays on both sides of the pivot.
3. **Combine**: Since we're sorting in-place, no additional combining step is needed.
**Key features:**
- **Time Complexity**: O(n log n) average case, O(n²) worst case
- **Space Complexity**: O(log n) due to recursion stack
- **In-place sorting**: Modifies the original array
- **Not stable**: Relative order of equal elements may change
**Alternative version with random pivot selection** (better average performance):Example usage:python
def quicksort(arr):
'''
Sorts an array using the quicksort algorithm.
Args:
arr: List of comparable elements
Returns:
None (sorts in-place)
'''
if len(arr) <= 1:
return
def partition(low, high):
'''Partition function using the last element as pivot'''
pivot = arr[high]
i = low - 1 # Index of smaller element
for j in range(low, high):
if arr[j] <= pivot:
i += 1
arr[i], arr[j] = arr[j], arr[i] # Swap elements
arr[i + 1], arr[high] = arr[high], arr[i + 1] # Place pivot in correct position
return i + 1
def quicksort_helper(low, high):
'''Recursive helper function'''
if low < high:
# Partition the array and get pivot index
pi = partition(low, high)
# Recursively sort elements before and after partition
quicksort_helper(low, pi - 1)
quicksort_helper(pi + 1, high)
quicksort_helper(0, len(arr) - 1)
# Example usage:
if __name__ == "__main__":
# Test the algorithm
test_array = [64, 34, 25, 12, 22, 11, 90]
print("Original array:", test_array)
quicksort(test_array)
print("Sorted array:", test_array)
# Test with other examples
test_cases = [
[5, 2, 8, 1, 9],
[1],
[],
[3, 3, 3, 3],
[5, 4, 3, 2, 1]
]
for i, case in enumerate(test_cases):
original = case.copy()
quicksort(case)
print(f"Test {i+1}: {original} → {case}")
**How it works:**
1. **Divide**: Choose a "pivot" element and partition the array so that elements smaller than the pivot are on the left, and larger elements are on the right.
2. **Conquer**: Recursively apply quicksort to the sub-arrays on both sides of the pivot.
3. **Combine**: Since we're sorting in-place, no additional combining step is needed.
**Key features:**
- **Time Complexity**: O(n log n) average case, O(n²) worst case
- **Space Complexity**: O(log n) due to recursion stack
- **In-place sorting**: Modifies the original array
- **Not stable**: Relative order of equal elements may change
**Alternative version with random pivot selection** (better average performance):Example usage:python
def quicksort(arr):
'''
Sorts an array using the quicksort algorithm.
Args:
arr: List of comparable elements
Returns:
None (sorts in-place)
'''
if len(arr) <= 1:
return
def partition(low, high):
'''Partition function using the last element as pivot'''
pivot = arr[high]
i = low - 1 # Index of smaller element
for j in range(low, high):
if arr[j] <= pivot:
i += 1
arr[i], arr[j] = arr[j], arr[i] # Swap elements
arr[i + 1], arr[high] = arr[high], arr[i + 1] # Place pivot in correct position
return i + 1
def quicksort_helper(low, high):
'''Recursive helper function'''
if low < high:
# Partition the array and get pivot index
pi = partition(low, high)
# Recursively sort elements before and after partition
quicksort_helper(low, pi - 1)
quicksort_helper(pi + 1, high)
quicksort_helper(0, len(arr) - 1)
# Example usage:
if __name__ == "__main__":
# Test the algorithm
test_array = [64, 34, 25, 12, 22, 11, 90]
print("Original array:", test_array)
quicksort(test_array)
print("Sorted array:", test_array)
# Test with other examples
test_cases = [
[5, 2, 8, 1, 9],
[1],
[],
[3, 3, 3, 3],
[5, 4, 3, 2, 1]
]
for i, case in enumerate(test_cases):
original = case.copy()
quicksort(case)
print(f"Test {i+1}: {original} → {case}")
**How it works:**
1. **Divide**: Choose a "pivot" element and partition the array so that elements smaller than the pivot are on the left, and larger elements are on the right.
2. **Conquer**: Recursively apply quicksort to the sub-arrays on both sides of the pivot.
3. **Combine**: Since we're sorting in-place, no additional combining step is needed.
**Key features:**
- **Time Complexity**: O(n log n) average case, O(n²) worst case
- **Space Complexity**: O(log n) due to recursion stack
- **In-place sorting**: Modifies the original array
- **Not stable**: Relative order of equal elements may change
**Alternative version with random pivot selection** (better average performance):Example usage:python
def quicksort(arr):
'''
Sorts an array using the quicksort algorithm.
Args:
arr: List of comparable elements
Returns:
None (sorts in-place)
'''
if len(arr) <= 1:
return
def partition(low, high):
'''Partition function using the last element as pivot'''
pivot = arr[high]
i = low - 1 # Index of smaller element
for j in range(low, high):
if arr[j] <= pivot:
i += 1
arr[i], arr[j] = arr[j], arr[i] # Swap elements
arr[i + 1], arr[high] = arr[high], arr[i + 1] # Place pivot in correct position
return i + 1
def quicksort_helper(low, high):
'''Recursive helper function'''
if low < high:
# Partition the array and get pivot index
pi = partition(low, high)
# Recursively sort elements before and after partition
quicksort_helper(low, pi - 1)
quicksort_helper(pi + 1, high)
quicksort_helper(0, len(arr) - 1)
# Example usage:
if __name__ == "__main__":
# Test the algorithm
test_array = [64, 34, 25, 12, 22, 11, 90]
print("Original array:", test_array)
quicksort(test_array)
print("Sorted array:", test_array)
# Test with other examples
test_cases = [
[5, 2, 8, 1, 9],
[1],
[],
[3, 3, 3, 3],
[5, 4, 3, 2, 1]
]
for i, case in enumerate(test_cases):
original = case.copy()
quicksort(case)
print(f"Test {i+1}: {original} → {case}")
**How it works:**
1. **Divide**: Choose a "pivot" element and partition the array so that elements smaller than the pivot are on the left, and larger elements are on the right.
2. **Conquer**: Recursively apply quicksort to the sub-arrays on both sides of the pivot.
3. **Combine**: Since we're sorting in-place, no additional combining step is needed.
**Key features:**
- **Time Complexity**: O(n log n) average case, O(n²) worst case
- **Space Complexity**: O(log n) due to recursion stack
- **In-place sorting**: Modifies the original array
- **Not stable**: Relative order of equal elements may change
**Alternative version with random pivot selection** (better average performance):Example usage:python
def quicksort(arr):
'''
Sorts an array using the quicksort algorithm.
Args:
arr: List of comparable elements
Returns:
None (sorts in-place)
'''
if len(arr) <= 1:
return
def partition(low, high):
'''Partition function using the last element as pivot'''
pivot = arr[high]
i = low - 1 # Index of smaller element
for j in range(low, high):
if arr[j] <= pivot:
i += 1
arr[i], arr[j] = arr[j], arr[i] # Swap elements
arr[i + 1], arr[high] = arr[high], arr[i + 1] # Place pivot in correct position
return i + 1
def quicksort_helper(low, high):
'''Recursive helper function'''
if low < high:
# Partition the array and get pivot index
pi = partition(low, high)
# Recursively sort elements before and after partition
quicksort_helper(low, pi - 1)
quicksort_helper(pi + 1, high)
quicksort_helper(0, len(arr) - 1)
# Example usage:
if __name__ == "__main__":
# Test the algorithm
test_array = [64, 34, 25, 12, 22, 11, 90]
print("Original array:", test_array)
quicksort(test_array)
print("Sorted array:", test_array)
# Test with other examples
test_cases = [
[5, 2, 8, 1, 9],
[1],
[],
[3, 3, 3, 3],
[5, 4, 3, 2, 1]
]
for i, case in enumerate(test_cases):
original = case.copy()
quicksort(case)
print(f"Test {i+1}: {original} → {case}")
**How it works:**
1. **Divide**: Choose a "pivot" element and partition the array so that elements smaller than the pivot are on the left, and larger elements are on the right.
2. **Conquer**: Recursively apply quicksort to the sub-arrays on both sides of the pivot.
3. **Combine**: Since we're sorting in-place, no additional combining step is needed.
**Key features:**
- **Time Complexity**: O(n log n) average case, O(n²) worst case
- **Space Complexity**: O(log n) due to recursion stack
- **In-place sorting**: Modifies the original array
- **Not stable**: Relative order of equal elements may change
**Alternative version with random pivot selection** (better average performance):Example usage:python
def quicksort(arr):
'''
Sorts an array using the quicksort algorithm.
Args:
arr: List of comparable elements
Returns:
None (sorts in-place)
'''
if len(arr) <= 1:
return
def partition(low, high):
'''Partition function using the last element as pivot'''
pivot = arr[high]
i = low - 1 # Index of smaller element
for j in range(low, high):
if arr[j] <= pivot:
i += 1
arr[i], arr[j] = arr[j], arr[i] # Swap elements
arr[i + 1], arr[high] = arr[high], arr[i + 1] # Place pivot in correct position
return i + 1
def quicksort_helper(low, high):
'''Recursive helper function'''
if low < high:
# Partition the array and get pivot index
pi = partition(low, high)
# Recursively sort elements before and after partition
quicksort_helper(low, pi - 1)
quicksort_helper(pi + 1, high)
quicksort_helper(0, len(arr) - 1)
# Example usage:
if __name__ == "__main__":
# Test the algorithm
test_array = [64, 34, 25, 12, 22, 11, 90]
print("Original array:", test_array)
quicksort(test_array)
print("Sorted array:", test_array)
# Test with other examples
test_cases = [
[5, 2, 8, 1, 9],
[1],
[],
[3, 3, 3, 3],
[5, 4, 3, 2, 1]
]
for i, case in enumerate(test_cases):
original = case.copy()
quicksort(case)
print(f"Test {i+1}: {original} → {case}")
**How it works:**
1. **Divide**: Choose a "pivot" element and partition the array so that elements smaller than the pivot are on the left, and larger elements are on the right.
2. **Conquer**: Recursively apply quicksort to the sub-arrays on both sides of the pivot.
3. **Combine**: Since we're sorting in-place, no additional combining step is needed.
**Key features:**
- **Time Complexity**: O(n log n) average case, O(n²) worst case
- **Space Complexity**: O(log n) due to recursion stack
- **In-place sorting**: Modifies the original array
- **Not stable**: Relative order of equal elements may change
**Alternative version with random pivot selection** (better average performance):Example usage:python
def quicksort(arr):
'''
Sorts an array using the quicksort algorithm.
Args:
arr: List of comparable elements
Returns:
None (sorts in-place)
'''
if len(arr) <= 1:
return
def partition(low, high):
'''Partition function using the last element as pivot'''
pivot = arr[high]
i = low - 1 # Index of smaller element
for j in range(low, high):
if arr[j] <= pivot:
i += 1
arr[i], arr[j] = arr[j], arr[i] # Swap elements
arr[i + 1], arr[high] = arr[high], arr[i + 1] # Place pivot in correct position
return i + 1
def quicksort_helper(low, high):
'''Recursive helper function'''
if low < high:
# Partition the array and get pivot index
pi = partition(low, high)
# Recursively sort elements before and after partition
quicksort_helper(low, pi - 1)
quicksort_helper(pi + 1, high)
quicksort_helper(0, len(arr) - 1)
# Example usage:
if __name__ == "__main__":
# Test the algorithm
test_array = [64, 34, 25, 12, 22, 11, 90]
print("Original array:", test_array)
quicksort(test_array)
print("Sorted array:", test_array)
# Test with other examples
test_cases = [
[5, 2, 8, 1, 9],
[1],
[],
[3, 3, 3, 3],
[5, 4, 3, 2, 1]
]
for i, case in enumerate(test_cases):
original = case.copy()
quicksort(case)
print(f"Test {i+1}: {original} → {case}")
**How it works:**
1. **Divide**: Choose a "pivot" element and partition the array so that elements smaller than the pivot are on the left, and larger elements are on the right.
2. **Conquer**: Recursively apply quicksort to the sub-arrays on both sides of the pivot.
3. **Combine**: Since we're sorting in-place, no additional combining step is needed.
**Key features:**
- **Time Complexity**: O(n log n) average case, O(n²) worst case
- **Space Complexity**: O(log n) due to recursion stack
- **In-place sorting**: Modifies the original array
- **Not stable**: Relative order of equal elements may change
**Alternative version with random pivot selection** (better average performance):Example usage:python
def quicksort(arr):
'''
Sorts an array using the quicksort algorithm.
Args:
arr: List of comparable elements
Returns:
None (sorts in-place)
'''
if len(arr) <= 1:
return
def partition(low, high):
'''Partition function using the last element as pivot'''
pivot = arr[high]
i = low - 1 # Index of smaller element
for j in range(low, high):
if arr[j] <= pivot:
i += 1
arr[i], arr[j] = arr[j], arr[i] # Swap elements
arr[i + 1], arr[high] = arr[high], arr[i + 1] # Place pivot in correct position
return i + 1
def quicksort_helper(low, high):
'''Recursive helper function'''
if low < high:
# Partition the array and get pivot index
pi = partition(low, high)
# Recursively sort elements before and after partition
quicksort_helper(low, pi - 1)
quicksort_helper(pi + 1, high)
quicksort_helper(0, len(arr) - 1)
# Example usage:
if __name__ == "__main__":
# Test the algorithm
test_array = [64, 34, 25, 12, 22, 11, 90]
print("Original array:", test_array)
quicksort(test_array)
print("Sorted array:", test_array)
# Test with other examples
test_cases = [
[5, 2, 8, 1, 9],
[1],
[],
[3, 3, 3, 3],
[5, 4, 3, 2, 1]
]
for i, case in enumerate(test_cases):
original = case.copy()
quicksort(case)
print(f"Test {i+1}: {original} → {case}")
**How it works:**
1. **Divide**: Choose a "pivot" element and partition the array so that elements smaller than the pivot are on the left, and larger elements are on the right.
2. **Conquer**: Recursively apply quicksort to the sub-arrays on both sides of the pivot.
3. **Combine**: Since we're sorting in-place, no additional combining step is needed.
**Key features:**
- **Time Complexity**: O(n log n) average case, O(n²) worst case
- **Space Complexity**: O(log n) due to recursion stack
- **In-place sorting**: Modifies the original array
- **Not stable**: Relative order of equal elements may change
**Alternative version with random pivot selection** (better average performance):Example usage:python
def quicksort(arr):
'''
Sorts an array using the quicksort algorithm.
Args:
arr: List of comparable elements
Returns:
None (sorts in-place)
'''
if len(arr) <= 1:
return
def partition(low, high):
'''Partition function using the last element as pivot'''
pivot = arr[high]
i = low - 1 # Index of smaller element
for j in range(low, high):
if arr[j] <= pivot:
i += 1
arr[i], arr[j] = arr[j], arr[i] # Swap elements
arr[i + 1], arr[high] = arr[high], arr[i + 1] # Place pivot in correct position
return i + 1
def quicksort_helper(low, high):
'''Recursive helper function'''
if low < high:
# Partition the array and get pivot index
pi = partition(low, high)
# Recursively sort elements before and after partition
quicksort_helper(low, pi - 1)
quicksort_helper(pi + 1, high)
quicksort_helper(0, len(arr) - 1)
# Example usage:
if __name__ == "__main__":
# Test the algorithm
test_array = [64, 34, 25, 12, 22, 11, 90]
print("Original array:", test_array)
quicksort(test_array)
print("Sorted array:", test_array)
# Test with other examples
test_cases = [
[5, 2, 8, 1, 9],
[1],
[],
[3, 3, 3, 3],
[5, 4, 3, 2, 1]
]
for i, case in enumerate(test_cases):
original = case.copy()
quicksort(case)
print(f"Test {i+1}: {original} → {case}")
**How it works:**
1. **Divide**: Choose a "pivot" element and partition the array so that elements smaller than the pivot are on the left, and larger elements are on the right.
2. **Conquer**: Recursively apply quicksort to the sub-arrays on both sides of the pivot.
3. **Combine**: Since we're sorting in-place, no additional combining step is needed.
**Key features:**
- **Time Complexity**: O(n log n) average case, O(n²) worst case
- **Space Complexity**: O(log n) due to recursion stack
- **In-place sorting**: Modifies the original array
- **Not stable**: Relative order of equal elements may change
**Alternative version with random pivot selection** (better average performance):Example usage:python
def quicksort(arr):
'''
Sorts an array using the quicksort algorithm.
Args:
arr: List of comparable elements
Returns:
None (sorts in-place)
'''
if len(arr) <= 1:
return
def partition(low, high):
'''Partition function using the last element as pivot'''
pivot = arr[high]
i = low - 1 # Index of smaller element
for j in range(low, high):
if arr[j] <= pivot:
i += 1
arr[i], arr[j] = arr[j], arr[i] # Swap elements
arr[i + 1], arr[high] = arr[high], arr[i + 1] # Place pivot in correct position
return i + 1
def quicksort_helper(low, high):
'''Recursive helper function'''
if low < high:
# Partition the array and get pivot index
pi = partition(low, high)
# Recursively sort elements before and after partition
quicksort_helper(low, pi - 1)
quicksort_helper(pi + 1, high)
quicksort_helper(0, len(arr) - 1)
# Example usage:
if __name__ == "__main__":
# Test the algorithm
test_array = [64, 34, 25, 12, 22, 11, 90]
print("Original array:", test_array)
quicksort(test_array)
print("Sorted array:", test_array)
# Test with other examples
test_cases = [
[5, 2, 8, 1, 9],
[1],
[],
[3, 3, 3, 3],
[5, 4, 3, 2, 1]
]
for i, case in enumerate(test_cases):
original = case.copy()
quicksort(case)
print(f"Test {i+1}: {original} → {case}")
**How it works:**
1. **Divide**: Choose a "pivot" element and partition the array so that elements smaller than the pivot are on the left, and larger elements are on the right.
2. **Conquer**: Recursively apply quicksort to the sub-arrays on both sides of the pivot.
3. **Combine**: Since we're sorting in-place, no additional combining step is needed.
**Key features:**
- **Time Complexity**: O(n log n) average case, O(n²) worst case
- **Space Complexity**: O(log n) due to recursion stack
- **In-place sorting**: Modifies the original array
- **Not stable**: Relative order of equal elements may change
**Alternative version with random pivot selection** (better average performance):Example usage:python
def quicksort(arr):
'''
Sorts an array using the quicksort algorithm.
Args:
arr: List of comparable elements
Returns:
None (sorts in-place)
'''
if len(arr) <= 1:
return
def partition(low, high):
'''Partition function using the last element as pivot'''
pivot = arr[high]
i = low - 1 # Index of smaller element
for j in range(low, high):
if arr[j] <= pivot:
i += 1
arr[i], arr[j] = arr[j], arr[i] # Swap elements
arr[i + 1], arr[high] = arr[high], arr[i + 1] # Place pivot in correct position
return i + 1
def quicksort_helper(low, high):
'''Recursive helper function'''
if low < high:
# Partition the array and get pivot index
pi = partition(low, high)
# Recursively sort elements before and after partition
quicksort_helper(low, pi - 1)
quicksort_helper(pi + 1, high)
quicksort_helper(0, len(arr) - 1)
# Example usage:
if __name__ == "__main__":
# Test the algorithm
test_array = [64, 34, 25, 12, 22, 11, 90]
print("Original array:", test_array)
quicksort(test_array)
print("Sorted array:", test_array)
# Test with other examples
test_cases = [
[5, 2, 8, 1, 9],
[1],
[],
[3, 3, 3, 3],
[5, 4, 3, 2, 1]
]
for i, case in enumerate(test_cases):
original = case.copy()
quicksort(case)
print(f"Test {i+1}: {original} → {case}")
**How it works:**
1. **Divide**: Choose a "pivot" element and partition the array so that elements smaller than the pivot are on the left, and larger elements are on the right.
2. **Conquer**: Recursively apply quicksort to the sub-arrays on both sides of the pivot.
3. **Combine**: Since we're sorting in-place, no additional combining step is needed.
**Key features:**
- **Time Complexity**: O(n log n) average case, O(n²) worst case
- **Space Complexity**: O(log n) due to recursion stack
- **In-place sorting**: Modifies the original array
- **Not stable**: Relative order of equal elements may change
**Alternative version with random pivot selection** (better average performance):Example usage:python
def quicksort(arr):
'''
Sorts an array using the quicksort algorithm.
Args:
arr: List of comparable elements
Returns:
None (sorts in-place)
'''
if len(arr) <= 1:
return
def partition(low, high):
'''Partition function using the last element as pivot'''
pivot = arr[high]
i = low - 1 # Index of smaller element
for j in range(low, high):
if arr[j] <= pivot:
i += 1
arr[i], arr[j] = arr[j], arr[i] # Swap elements
arr[i + 1], arr[high] = arr[high], arr[i + 1] # Place pivot in correct position
return i + 1
def quicksort_helper(low, high):
'''Recursive helper function'''
if low < high:
# Partition the array and get pivot index
pi = partition(low, high)
# Recursively sort elements before and after partition
quicksort_helper(low, pi - 1)
quicksort_helper(pi + 1, high)
quicksort_helper(0, len(arr) - 1)
# Example usage:
if __name__ == "__main__":
# Test the algorithm
test_array = [64, 34, 25, 12, 22, 11, 90]
print("Original array:", test_array)
quicksort(test_array)
print("Sorted array:", test_array)
# Test with other examples
test_cases = [
[5, 2, 8, 1, 9],
[1],
[],
[3, 3, 3, 3],
[5, 4, 3, 2, 1]
]
for i, case in enumerate(test_cases):
original = case.copy()
quicksort(case)
print(f"Test {i+1}: {original} → {case}")
**How it works:**
1. **Divide**: Choose a "pivot" element and partition the array so that elements smaller than the pivot are on the left, and larger elements are on the right.
2. **Conquer**: Recursively apply quicksort to the sub-arrays on both sides of the pivot.
3. **Combine**: Since we're sorting in-place, no additional combining step is needed.
**Key features:**
- **Time Complexity**: O(n log n) average case, O(n²) worst case
- **Space Complexity**: O(log n) due to recursion stack
- **In-place sorting**: Modifies the original array
- **Not stable**: Relative order of equal elements may change
**Alternative version with random pivot selection** (better average performance):Example usage:python
def quicksort(arr):
'''
Sorts an array using the quicksort algorithm.
Args:
arr: List of comparable elements
Returns:
None (sorts in-place)
'''
if len(arr) <= 1:
return
def partition(low, high):
'''Partition function using the last element as pivot'''
pivot = arr[high]
i = low - 1 # Index of smaller element
for j in range(low, high):
if arr[j] <= pivot:
i += 1
arr[i], arr[j] = arr[j], arr[i] # Swap elements
arr[i + 1], arr[high] = arr[high], arr[i + 1] # Place pivot in correct position
return i + 1
def quicksort_helper(low, high):
'''Recursive helper function'''
if low < high:
# Partition the array and get pivot index
pi = partition(low, high)
# Recursively sort elements before and after partition
quicksort_helper(low, pi - 1)
quicksort_helper(pi + 1, high)
quicksort_helper(0, len(arr) - 1)
# Example usage:
if __name__ == "__main__":
# Test the algorithm
test_array = [64, 34, 25, 12, 22, 11, 90]
print("Original array:", test_array)
quicksort(test_array)
print("Sorted array:", test_array)
# Test with other examples
test_cases = [
[5, 2, 8, 1, 9],
[1],
[],
[3, 3, 3, 3],
[5, 4, 3, 2, 1]
]
for i, case in enumerate(test_cases):
original = case.copy()
quicksort(case)
print(f"Test {i+1}: {original} → {case}")
**How it works:**
1. **Divide**: Choose a "pivot" element and partition the array so that elements smaller than the pivot are on the left, and larger elements are on the right.
2. **Conquer**: Recursively apply quicksort to the sub-arrays on both sides of the pivot.
3. **Combine**: Since we're sorting in-place, no additional combining step is needed.
**Key features:**
- **Time Complexity**: O(n log n) average case, O(n²) worst case
- **Space Complexity**: O(log n) due to recursion stack
- **In-place sorting**: Modifies the original array
- **Not stable**: Relative order of equal elements may change
**Alternative version with random pivot selection** (better average performance):Deploy This Model
Production-ready deployment in minutes
Together.ai
Instant API access to this model
Production-ready inference API. Start free, scale to millions.
Try Free APIReplicate
One-click model deployment
Run models in the cloud with simple API. No DevOps required.
Deploy NowDisclosure: We may earn a commission from these partners. This helps keep LLMYourWay free.