code for quick sort
- User
code for quick sort
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
code for quick sort
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Quick Sort is a highly efficient, comparison-based sorting algorithm. It works on the principle of Divide and Conquer.
Here's the general idea:
Pick a Pivot: Choose an element from the array, called the pivot. The choice of pivot greatly affects the algorithm's performance. Common strategies include:
Partition: Rearrange the array such that all elements smaller than the pivot come before it, and all elements greater than the pivot come after it. Elements equal to the pivot can go on either side. After this step, the pivot is in its final sorted position.
Recurse: Recursively apply the above two steps to the sub-array of elements with smaller values and separately to the sub-array of elements with greater values.
The recursion stops when a sub-array has zero or one element, as these are already sorted.
The most common and conceptually simple partitioning scheme is Lomuto's. Let's say we pick the last element as the pivot.
i (index of smaller element) to low - 1.j from low to high - 1 (where high is the pivot's index).arr[j] is less than or equal to the pivot:
i.arr[i] and arr[j]. This moves elements smaller than the pivot to the left side of i.arr[i + 1] and arr[high] (the pivot). This places the pivot in its correct sorted position.i + 1, which is the pivot's final index.def partition(arr, low, high):
"""
This function takes last element as pivot, places the pivot element at its correct position in sorted array,
and places all smaller (than pivot) to left of pivot and all greater elements to right of pivot.
"""
pivot = arr[high] # Choose the last element as the pivot
i = low - 1 # Index of smaller element
for j in range(low, high):
# If current element is smaller than or equal to pivot
if arr[j] <= pivot:
# Increment index of smaller element
i = i + 1
arr[i], arr[j] = arr[j], arr[i] # Swap
arr[i + 1], arr[high] = arr[high], arr[i + 1] # Swap pivot to its correct position
return i + 1 # Return the partitioning index
def quick_sort(arr, low, high):
"""
The main function that implements QuickSort
arr[] --> Array to be sorted,
low --> Starting index,
high --> Ending index
"""
if low < high:
# pi is partitioning index, arr[pi] is now at right place
pi = partition(arr, low, high)
# Separately sort elements before partition and after partition
quick_sort(arr, low, pi - 1)
quick_sort(arr, pi + 1, high)
# Example Usage:
my_list = [10, 7, 8, 9, 1, 5]
n = len(my_list)
print("Original list:", my_list)
quick_sort(my_list, 0, n - 1)
print("Sorted list: ", my_list)
my_list2 = [64, 34, 25, 12, 22, 11, 90]
n2 = len(my_list2)
print("Original list:", my_list2)
quick_sort(my_list2, 0, n2 - 1)
print("Sorted list: ", my_list2)
import java.util.Arrays;
public class QuickSort {
// A utility function to swap two elements
static void swap(int[] arr, int i, int j) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
/*
* This function takes last element as pivot, places the pivot element at its
* correct position in sorted array, and places all smaller (than pivot) to
* left of pivot and all greater elements to right of pivot
*/
static int partition(int[] arr, int low, int high) {
// Choosing the last element as the pivot
int pivot = arr[high];
// Index of smaller element and indicates the right position of pivot found so far
int i = (low - 1);
for (int j = low; j <= high - 1; j++) {
// If current element is smaller than or equal to pivot
if (arr[j] <= pivot) {
i++; // increment index of smaller element
swap(arr, i, j);
}
}
swap(arr, i + 1, high);
return (i + 1);
}
/*
* The main function that implements QuickSort
* arr[] --> Array to be sorted,
* low --> Starting index,
* high --> Ending index
*/
static void quickSort(int[] arr, int low, int high) {
if (low < high) {
// pi is partitioning index, arr[pi] is now at right place
int pi = partition(arr, low, high);
// Recursively sort elements before partition and after partition
quickSort(arr, low, pi - 1);
quickSort(arr, pi + 1, high);
}
}
public static void main(String[] args) {
int[] arr = {10, 7, 8, 9, 1, 5};
int n = arr.length;
System.out.println("Original array: " + Arrays.toString(arr));
quickSort(arr, 0, n - 1);
System.out.println("Sorted array: " + Arrays.toString(arr));
int[] arr2 = {64, 34, 25, 12, 22, 11, 90};
int n2 = arr2.length;
System.out.println("Original array: " + Arrays.toString(arr2));
quickSort(arr2, 0, n2 - 1);
System.out.println("Sorted array: " + Arrays.toString(arr2));
}
}
#include <iostream>
#include <vector>
#include <algorithm> // Required for std::swap
// Function to swap two elements
void swap(std::vector<int>& arr, int i, int j) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
/*
* This function takes last element as pivot, places the pivot element at its correct position in sorted array,
* and places all smaller (than pivot) to left of pivot and all greater elements to right of pivot
*/
int partition(std::vector<int>& arr, int low, int high) {
int pivot = arr[high]; // Choose the last element as the pivot
int i = (low - 1); // Index of smaller element
for (int j = low; j <= high - 1; j++) {
// If current element is smaller than or equal to pivot
if (arr[j] <= pivot) {
i++; // Increment index of smaller element
swap(arr, i, j);
}
}
swap(arr, i + 1, high); // Put the pivot in its correct position
return (i + 1); // Return the partitioning index
}
/*
* The main function that implements QuickSort
* arr --> Array to be sorted,
* low --> Starting index,
* high --> Ending index
*/
void quickSort(std::vector<int>& arr, int low, int high) {
if (low < high) {
// pi is partitioning index, arr[pi] is now at right place
int pi = partition(arr, low, high);
// Separately sort elements before partition and after partition
quickSort(arr, low, pi - 1);
quickSort(arr, pi + 1, high);
}
}
int main() {
std::vector<int> arr = {10, 7, 8, 9, 1, 5};
int n = arr.size();
std::cout << "Original array: ";
for (int x : arr) {
std::cout << x << " ";
}
std::cout << std::endl;
quickSort(arr, 0, n - 1);
std::cout << "Sorted array: ";
for (int x : arr) {
std::cout << x << " ";
}
std::cout << std::endl;
std::vector<int> arr2 = {64, 34, 25, 12, 22, 11, 90};
int n2 = arr2.size();
std::cout << "Original array: ";
for (int x : arr2) {
std::cout << x << " ";
}
std::cout << std::endl;
quickSort(arr2, 0, n2 - 1);
std::cout << "Sorted array: ";
for (int x : arr2) {
std::cout << x << " ";
}
std::cout << std::endl;
return 0;
}
function swap(arr, i, j) {
[arr[i], arr[j]] = [arr[j], arr[i]]; // ES6 array destructuring for swap
}
/*
* This function takes last element as pivot, places the pivot element at its correct position in sorted array,
* and places all smaller (than pivot) to left of pivot and all greater elements to right of pivot
*/
function partition(arr, low, high) {
const pivot = arr[high]; // Choose the last element as the pivot
let i = (low - 1); // Index of smaller element
for (let j = low; j <= high - 1; j++) {
// If current element is smaller than or equal to pivot
if (arr[j] <= pivot) {
i++; // Increment index of smaller element
swap(arr, i, j);
}
}
swap(arr, i + 1, high); // Put the pivot in its correct position
return (i + 1); // Return the partitioning index
}
/*
* The main function that implements QuickSort
* arr --> Array to be sorted,
* low --> Starting index,
* high --> Ending index
*/
function quickSort(arr, low, high) {
if (low < high) {
// pi is partitioning index, arr[pi] is now at right place
const pi = partition(arr, low, high);
// Separately sort elements before partition and after partition
quickSort(arr, low, pi - 1);
quickSort(arr, pi + 1, high);
}
}
// Example Usage:
const myArray = [10, 7, 8, 9, 1, 5];
const n = myArray.length;
console.log("Original array:", myArray);
quickSort(myArray, 0, n - 1);
console.log("Sorted array: ", myArray);
const myArray2 = [64, 34, 25, 12, 22, 11, 90];
const n2 = myArray2.length;
console.log("Original array:", myArray2);
quickSort(myArray2, 0, n2 - 1);
console.log("Sorted array: ", myArray2);
Time Complexity:
n-1 elements. This leads to n recursive calls, each doing O(n) work.log n, and each level of recursion involves processing n elements (across all sub-problems at that level).Space Complexity:
n (due to unbalanced partitions), requiring n stack frames.Stability: Quick Sort is generally not stable. The relative order of equal elements might not be preserved.
In-Place: It is an in-place sorting algorithm because it typically requires only a constant amount of extra space (excluding the recursion stack space).
Quick Sort is one of the most widely used sorting algorithms.
O(n log n) algorithms like Merge Sort for average cases, largely due to better constant factors and cache performance (because of its in-place nature).O(n^2) performance is unacceptable (e.g., in real-time critical systems where a guaranteed upper bound on time is needed), other algorithms like Heap Sort or Merge Sort might be preferred.