Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create Quick Sort #243

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions algorithms/javascript/Quick Sort
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// Quick sort in javascript

//TC - O(N2)
//SC - O(1)


// Function to partition the array and return the partition index
function partition(arr, low, high) {
// Choosing the pivot
let pivot = arr[high];

// Index of smaller element and indicates the right position of pivot found so far
let i = low - 1;

for (let j = low; j <= high - 1; j++) {
// If current element is smaller than the pivot
if (arr[j] < pivot) {
// Increment index of smaller element
i++;
[arr[i], arr[j]] = [arr[j], arr[i]]; // Swap elements
}
}

[arr[i + 1], arr[high]] = [arr[high], arr[i + 1]]; // Swap pivot to its correct position
return i + 1; // Return the partition index
}

// The main function that implements QuickSort
function quickSort(arr, low, high) {
if (low < high) {
// pi is the partitioning index, arr[pi] is now at the right place
let pi = partition(arr, low, high);

// Separately sort elements before partition and after partition
quickSort(arr, low, pi - 1);
quickSort(arr, pi + 1, high);
}
}

// Driver code
let arr = [10, 7, 8, 9, 1, 5];
let N = arr.length;

// Function call
quickSort(arr, 0, N - 1);
console.log("Sorted array:");
console.log(arr.join(" "));

//Output - 1 5 7 8 9 10