Keep and Share logo     Log In  |  Mobile View  |  Help  
 
Visiting
 
Select a Color
   
 












01.DS.02.010. Sort an Array Using Quicksort Algorithm.html -- 1.7 meg
 Sort an Array Using Quicksort Algorithm - Coderust: Hacking the Coding Interview
Log InJoin for free
Back To Module Home
Data Structures
0% completed
Further Reading
Mark Module as Completed
Ask a Question

Sort an Array Using Quicksort Algorithm

Given an integer array, sort it in ascending order using quicksort.

Statement#

Given an array of integers nums, sort it in ascending order using the quicksort algorithm.

Example#

Here’s an example of the quicksort algorithm:

garrayOriginal array01234552326225array2Sorted array01234223252655

Sample input#

[55, 23, 26, 2, 25]

Expected output#

[2, 23, 25, 26, 55]

Try it yourself#

C++
Java
Python
JS
Ruby
Go
1
2
3
4
5
6
7
8
9
10
#include <iostream>
#include <vector>
using namespace std;
void QuickSort(vector<int> &nums, int size) { 
    // TODO: Write - Your - Code
    return;
}
#
#include <iostream> #include <vector> using namespace std; void QuickSort(vector<int> &nums, int size) { // TODO: Write - Your - Code return; }
Enter to Rename, Shift+Enter to Preview
Test
Need Hint?
Save
Reset

Solution#

Here is an overview of how the quicksort algorithm works:

  • Select a pivot element from the array to divide it into two parts.
    • We pick the first element as the pivot if we follow Hoare’s algorithm. Another common approach is to select a random element as the pivot.
  • Reorder the array by comparing elements with the pivot element such that smaller values end up at the left side and larger values end up at the right side of the pivot.
  • Now, the pivot element is in its correct sorted position.

By applying the above steps, we can recursively sort the sublists on the right and left sides of the pivot.

Let’s see a visual representation of the algorithm:

Created with Fabric.js 3.6.601234567895523262187823823Let's sort this array using quicksort.
1 of 22
C++
Java
Python
JS
Ruby
Go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream>
#include <vector>
using namespace std;
// Using Hoare's partitioning scheme
int Partition(vector<int> &nums, int low, int high) {
    // Initializing pivot's index to low
    int pivot_value = nums[low];
    int i = low;
    int j = high;
    // Loop till i pointer crosses j pointer
    while (< j) {
        // Increment the 'i' pointer till it finds an element greater than pivot
        while (<= high && nums[i] <= pivot_value) i++;
        // Decrement the 'j' pointer till it finds an element less than pivot
        while (nums[j] > pivot_value) j--;
        // Swap the numbers on 'i' and 'j'
        if (< j) {
            // Using the stl swap function to swap the numbers
            swap(nums[i], nums[j]);
        }
    }
    // Swap pivot element with element on 'j' pointer.
    nums[low] = nums[j];
    nums[j] = pivot_value;
    // Return the pivot index
#
#include <iostream> #include <vector> using namespace std; // Using Hoare's partitioning scheme int Partition(vector<int> &nums, int low, int high) { // Initializing pivot's index to low int pivot_value = nums[low]; int i = low;
Enter to Rename, Shift+Enter to Preview
Run
Save
Reset

Time complexity#

The time complexity of this solution is linearithmic, O(n logn)O(n \space logn)O(n logn).

Space complexity#

The space complexity of this solution is logarithmic, O(logn)O(logn)O(logn), because it consumes memory on the stack.

Back
Find Pair With Given Sum in an Array
Next
Equal Subset Sum Partition
Mark as Completed
Report an Issue


Creation date: Oct 7, 2022 7:33pm     Last modified date: Oct 7, 2022 7:33pm   Last visit date: Dec 3, 2024 2:25am
    Report Objectionable Content