Naive Partition Algorithm (original) (raw)

Last Updated : 07 Nov, 2024

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

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

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

**Examples:

**Input: arr[] = [5, 13, 6, 9, 12, 11, 8]
**Output: [5, 6, **8, 13, 9, 12, 11]
**Explanation: Allelements smaller than pivot element [5, 6] were arranged before it and elements larger than pivot [13, 9, 12, 11] were arranged after it.

**Input: arr[] = [4, 10, 9, 8, 16, 19, 9]
**Output: [4, 9, 8, **9, 10, 16, 19]
**Explanation: Allelements smaller than or equal to pivot element [4, 9, 8] were arranged before it and elements larger than pivot [10, 16, 19] were arranged after it.

Table of Content

Naive approach to Partition an Array

A simple approach to **partition an array is to create a new temporary array which will store the rearranged elements. In this approach, we first iterate over the original array and add all elements that are smaller than or equal to the pivot to the temporary array. Then, we add the pivot element to the temporary array. Finally, we fill the remaining part of the temporary array with elements that are greater than the **pivot.

This ensures that the smaller elements come before the **pivot, and the larger elements come after it. Now, copy the elements from the temporary array back to the original array.

C++ `

// C++ program to partition the array // using naive partition approach #include #include using namespace std;

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

  // Last element will be the pivot value
  int pivot = arr[n - 1];

  // create a temp array to store the elements in order
  vector<int> temp(n);
  int idx = 0;
  
  // First fill element smaller than or equal to
  // pivot, into the temp array
  for (int i = 0; i < n; i++) {
      if (arr[i] <= pivot) 
          temp[idx++] = arr[i];
}
  
  // Now fill the elements greater than pivot
  for (int i = 0; i < n; i++) {
      if (arr[i] > pivot) 
          temp [idx++] = arr[i];
}
  
  // copy the elements from temp to arr
  arr = temp;

} int main() { vector arr = {5, 13, 6, 9, 12, 11, 8}; partition(arr);

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

}

C

// C program to partition the array // using naive partition approach #include <stdio.h> #include <stdlib.h>

// Function to partition the array according // to pivot index element void partition(int *arr, int n) { // Last element will be the pivot value int pivot = arr[n - 1];

// create a temp array to store the elements in order
int *temp = (int *)malloc(n * sizeof(int));
int idx = 0;

// First fill element smaller than or equal to
// pivot, into the temp array
for (int i = 0; i < n; i++) {
    if (arr[i] <= pivot) 
        temp[idx++] = arr[i];
}

// Now fill the elements greater than pivot
for (int i = 0; i < n; i++) {
    if (arr[i] > pivot) 
        temp[idx++] = arr[i];
}

// copy the elements from temp to arr
for (int i = 0; i < n; i++) {
    arr[i] = temp[i];
}
free(temp);

}

int main() { int arr[] = {5, 13, 6, 9, 12, 11, 8}; 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 naive partition approach import java.util.Arrays;

// Function to partition the array according // to pivot index element class GfG { static void partition(int[] arr) { int n = arr.length;

    // Last element will be the pivot value
    int pivot = arr[n - 1];
  
    // create a temp array to store the 
      // elements in order
    int[] temp = new int[n];
    int idx = 0;
    
    // First fill element smaller than or 
      // equal to pivot, into the temp array
    for (int i = 0; i < n; i++) {
        if (arr[i] <= pivot) 
            temp[idx++] = arr[i];
    }
    
    // Now fill the elements greater than pivot
    for (int i = 0; i < n; i++) {
        if (arr[i] > pivot) 
            temp[idx++] = arr[i];
    }
    
    // copy the elements from temp to arr
    for (int i = 0; i < n; i++) 
          arr[i] = temp[i];
}

public static void main(String[] args) {
    int[] arr = {5, 13, 6, 9, 12, 11, 8};
    partition(arr);
  
      for (int ele: arr)
        System.out.print(ele + " ");
}

}

Python

Function to partition the array according

to pivot index element

def partition(arr): n = len(arr)

# Last element will be the pivot value
pivot = arr[n - 1]

# create a temp array to store 
# the elements in order
temp = [0] * n
idx = 0

# First fill elements smaller than or equal to
# pivot, into the temp array
for i in range(n):
    if arr[i] <= pivot:
        temp[idx] = arr[i]
        idx += 1

# Now fill the elements greater than pivot
# into the temp array
for i in range(n):
    if arr[i] > pivot:
        temp[idx] = arr[i]
        idx += 1

# copy the elements from temp to arr
for i in range(n):
    arr[i] = temp[i]

if name == "main": arr = [5, 13, 6, 9, 12, 11, 8] partition(arr)

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

C#

// C# program to partition the array // using naive partition approach using System; class GfG {

// Function to partition the array according 
// to pivot index element
static void Partition(int[] arr) {
    int n = arr.Length;
  
    // Last element will be the pivot value
    int pivot = arr[n - 1];
  
    // create a temp array to store the elements 
      // in order
    int[] temp = new int[n];
    int idx = 0;
    
    // First fill element smaller than or equal to
    // pivot, into the temp array
    for (int i = 0; i < n; i++) {
        if (arr[i] <= pivot) 
            temp[idx++] = arr[i];
    }
    
    // Now fill the elements greater than pivot
    for (int i = 0; i < n; i++) {
        if (arr[i] > pivot) 
            temp[idx++] = arr[i];
    }
    
    // copy the elements from temp to arr
    Array.Copy(temp, arr, n);
}

static void Main() {
    int[] arr = {5, 13, 6, 9, 12, 11, 8};
    Partition(arr);
    Console.WriteLine(string.Join(" ", arr));
}

}

JavaScript

// JavaScript program to partition the array // using naive partition approach

// Function to partition the array according // to pivot index element function partition(arr) { let n = arr.length;

// Last element will be the pivot value
let pivot = arr[n - 1];

// create a temp array to store the 
// elements in order
let temp = new Array(n);
let idx = 0;

// First fill element smaller than or equal to
// pivot, into the temp array
for (let i = 0; i < n; i++) {
    if (arr[i] <= pivot) 
        temp[idx++] = arr[i];
}

// Now fill the elements greater than pivot
for (let i = 0; i < n; i++) {
    if (arr[i] > pivot) 
        temp[idx++] = arr[i];
}

// copy the elements from temp to arr
for (let i = 0; i < n; i++) {
    arr[i] = temp[i];
}

}

// Driver Code let arr = [5, 13, 6, 9, 12, 11, 8]; partition(arr); console.log(arr.join(' '));

`

**Time Complexity: O(n), for array traversal
**Auxiliary Space: O(n), As it uses a temporary array

Some Interesting Facts