Hoare's Partition Algorithm (original) (raw)

Last Updated : 11 Nov, 2024

Given an array arr[], the task is to **partition the array by assuming first element as **pivot element.

The partition of an array must satisfy the following two conditions:

**Partition index is equal to count of elements, strictly smaller than **pivot, minus one.

**Note: There might me more than one possible partition arrays.

**Examples:

**Input: arr[] = [5, 3, 8, 4, 2, 7, 1, 10]
**Output: [1, 3, 2, **4, 8, 7, 5, 10]
**Explanation: The partition index is 3 and pivot element is 5, allelements smaller than pivot element [1, 3, 2, 4] were arranged before partition index and elements larger than or equal to pivot [13, 9, 12, 11] were arranged after partition index.

**Input: arr[] = [12, 10, 9, 16, 19, 9]
**Output: [9, 10, **9, 16, 19, 12]
**Explanation: The partition index is 2 and pivot element is 12, allelements smaller than pivot element [9, 10, 9] were arranged before or at partition index and elements larger than or equal to pivot [16, 19, 12] were arranged after partition index.

Table of Content

Hoare's Algorithm for Array Partition

**Hoare's partitioning algorithm is an efficient way to partition an array around a pivot. It’s based on two pointers that start at opposite ends of the array and move toward each other until they find elements that need to be swapped.

Step-by-step explanation of algorithm:

// C++ program to partition the array // using Hoare's Partition Algorithm #include #include using namespace std;

// Function to partition the array according // to pivot index element void partition(vector &arr) { int n = arr.size(); int pivot = arr[0];

  int i = -1, j = n;
  while (true) {
  
      // find next element larger than pivot 
      // from the left
      do {
          i++;
    } while (arr[i] < pivot);
      
      // find next element smaller than pivot 
      // from the right
      do {
          j--;
    } while (arr[j] > pivot);
      
      // if left and right crosses each other
      // no swapping required
      if (i > j) break;
      
      // swap larger and smaller elements
      swap(arr[i], arr[j]);
}

}

int main() { vector arr = {5, 3, 8, 4, 2, 7, 1, 10}; partition(arr);

  for (int i = 0; i < arr.size(); i++) 
      cout << arr[i] << " "; 
return 0;

}

C

// C program to partition the array // using Hoare's Partition Algorithm #include <stdio.h> #include <stdlib.h>

// Function to partition the array according // to pivot index element void partition(int arr[], int n) { int pivot = arr[0]; int i = -1, j = n; while (1) {

    // find next element larger than pivot 
    // from the left
    do {
        i++;
    } while (arr[i] < pivot);
    
    // find next element smaller than pivot 
    // from the right
    do {
        j--;
    } while (arr[j] > pivot);
    
    // if left and right crosses each other
    // no swapping required
    if (i > j) break;
    
    // swap larger and smaller elements
    int temp = arr[i];
    arr[i] = arr[j];
    arr[j] = temp;
}

}

int main() { int arr[] = {5, 3, 8, 4, 2, 7, 1, 10}; int n = sizeof(arr) / sizeof(arr[0]); partition(arr, n);

for (int i = 0; i < n; i++) 
    printf("%d ", arr[i]); 
return 0;

}

Java

// Java program to partition the array // using Hoare's Partition Algorithm import java.util.Arrays;

class GfG {

// Function to partition the array according 
// to pivot index element
static void partition(int[] arr) {
    int n = arr.length;
    int pivot = arr[0];
    int i = -1, j = n;
    while (true) {
      
        // find next element larger than pivot 
        // from the left
        do {
            i++;
        } while (arr[i] < pivot);
        
        // find next element smaller than pivot 
        // from the right
        do {
            j--;
        } while (arr[j] > pivot);
        
        // if left and right crosses each other
        // no swapping required
        if (i > j) break;
        
        // swap larger and smaller elements
        int temp = arr[i];
        arr[i] = arr[j];
        arr[j] = temp;
    }
}

public static void main(String[] args) {
    int[] arr = {5, 3, 8, 4, 2, 7, 1, 10};
    partition(arr);
  
      for (int ele: arr)
    System.out.print(ele + " ");
}

}

Python

Python program to partition the array

using Hoare's Partition Algorithm

Function to partition the array according to pivot index element

def partition(arr): n = len(arr) pivot = arr[0]

i = -1
j = n
while True:
  
    # find next element larger than pivot from the left
    while True:
        i += 1
        if arr[i] >= pivot:
            break
    
    # find next element smaller than pivot from the right
    while True:
        j -= 1
        if arr[j] <= pivot:
            break
    
    # if left and right crosses each other no swapping required
    if i > j:
        break
    
    # swap larger and smaller elements
    arr[i], arr[j] = arr[j], arr[i]

if name == "main": arr = [5, 3, 8, 4, 2, 7, 1, 10] partition(arr)

for num in arr:
    print(num, end=' ')

C#

// C# program to partition the array // using Hoare's Partition Algorithm using System;

class GfG {

// Function to partition the array according 
// to pivot index element
static void partition(int[] arr) {
    int n = arr.Length;
    int pivot = arr[0];
    int i = -1, j = n;
    while (true) {
      
        // find next element larger than pivot 
        // from the left
        do {
            i++;
        } while (arr[i] < pivot);
        
        // find next element smaller than pivot 
        // from the right
        do {
            j--;
        } while (arr[j] > pivot);
        
        // if left and right crosses each other
        // no swapping required
        if (i > j) break;
        
        // swap larger and smaller elements
        int temp = arr[i];
        arr[i] = arr[j];
        arr[j] = temp;
    }
}

static void Main() {
    int[] arr = {5, 3, 8, 4, 2, 7, 1, 10};
    partition(arr);
    Console.WriteLine(string.Join(" ", arr));
}

}

JavaScript

// JavaScript program to partition the array // using Hoare's Partition Algorithm function partition(arr) { let n = arr.length; let pivot = arr[0]; let i = -1, j = n; while (true) {

    // find next element larger than pivot 
    // from the left
    do {
        i++;
    } while (arr[i] < pivot);
    
    // find next element smaller than pivot 
    // from the right
    do {
        j--;
    } while (arr[j] > pivot);
    
    // if left and right crosses each other
    // no swapping required
    if (i > j) break;
    
    // swap larger and smaller elements
    [arr[i], arr[j]] = [arr[j], arr[i]];
}

}

// Driver Code let arr = [5, 3, 8, 4, 2, 7, 1, 10]; partition(arr); console.log(arr.join(' '));

`

**Time Complexity: O(n)
**Auxiliary Space: O(1)

Some Interesting Facts