Partitioning an array in Java

In many programming scenarios, you may encounter the need to partition an array based on a certain condition. Partitioning an array involves rearranging its elements in such a way that all elements satisfying a given condition appear before those that do not. This approach is commonly used in algorithms such as quicksort and finding the median of an array.

In this article, we will explore how to efficiently partition an array in Java.

The Partitioning Algorithm

The algorithm for partitioning an array can be implemented using the following steps:

  1. Choose a pivot element from the array. The pivot element is used as a reference point to divide the array into two partitions.
  2. Rearrange the elements of the array so that all elements smaller than the pivot appear before it, and all elements greater than the pivot appear after it. This step is known as the partitioning step.

Here’s an example implementation of the partition algorithm in Java:

public static int partition(int[] arr, int low, int high) {
    int pivot = arr[high]; // Choose the last element as the pivot
    int i = (low - 1); // Index of the smaller element

    for (int j = low; j < high; j++) {
        if (arr[j] < pivot) {
            i++;
            // Swap arr[i] and arr[j]
            int temp = arr[i];
            arr[i] = arr[j];
            arr[j] = temp;
        }
    }

    // Swap arr[i+1] and arr[high] (pivot element)
    int temp = arr[i + 1];
    arr[i + 1] = arr[high];
    arr[high] = temp;

    return i + 1; // Return the partition index
}

Partitioning Example

Let’s see an example of using the partition algorithm on an array:

int[] arr = { 10, 80, 30, 90, 40, 50, 70 };
int n = arr.length;
int pivotIndex = partition(arr, 0, n - 1);

System.out.println("Array after partition:");
for (int i = 0; i < n; i++) {
    System.out.print(arr[i] + " ");
}

The output of the above code will be:

Array after partition: 10 30 40 50 80 90 70

In this example, the pivot element is chosen as the last element of the array (70). After performing the partition, all elements less than 70 are placed before it, and all elements greater than 70 are placed after it.

Conclusion

Partitioning an array is a fundamental operation in various algorithms. By using the partition algorithm, you can efficiently divide an array into two partitions based on a certain condition. Understanding and implementing this algorithm is crucial in building efficient sorting and searching algorithms.

Remember to choose an appropriate pivot element to ensure efficient partitioning of the array.

#java #partitioning