# C++ Program for Iterative Quick Sort

In this article, we will discuss the C++ program for iterative quick sort. But before going to its implementation, we must know about the iterative quick sort with its algorithm and example.

One popular sorting algorithm well-known for its practical efficiency and efficacy is called 'Quick Sort'. Although the recursive variant of Quick Sort is more frequently used, an iterative version can also be implemented to circumvent the overhead associated with recursive function calls. This iterative method uses a stack to manage the array's partitions that need to be sorted.

The fundamental principle of Quick Sort is to divide the remaining components of the array into two sub-arrays according to whether they are more or less than the pivot and then select one pivot element from the array. After that, the sub-arrays are subjected to this procedure iteratively, which sorts the array as a whole.

The partition function, which chooses a pivot element and partitions the array, is defined at the program's start. The 'iterativeQuickSort' function uses a stack to keep track of the sub-arrays that require sorting. It pushes the resulting sub-arrays back onto the stack after continuously removing sub-arrays from the stack and partitioning them with the partition function. This operation is repeated until the stack is empty to ensure every element is sorted correctly.

The program makes use of a sample array to show off the capability. The array is shown both before and after the sorting procedure, giving a clear picture of how the elements are arranged in ascending order using the iterative Quick Sort method.

Developers can appreciate the efficiency of Quick Sort and learn how to optimize sorting algorithms for practical settings by comprehending and putting this iterative version of the algorithm into practice. The trade-offs between recursion and iteration in algorithm design are brought to light by the iterative approach's usage of an explicit stack.

### Iterative Quicksort Algorithm

1. Start
2. Pick any element as a pivot.
3. If the element is less than the pivot.
4. Swap with the element at the index.
5. Increment index.
6. Push all the elements less than pivot to the left.
7. Push all the elements greater elements to the right.
8. Swap the rightmost element with the element at the index.
9. Return index.
10. Print sorted array.
11. Stop

Complexity Analysis:

Time Complexity:

Quick Sort has an O(n log n) average and best-case time complexity, which makes it incredibly effective for big datasets. On the other hand, the time complexity can drop to O(n^2) in the worst scenario if the pivot selection repeatedly causes unbalanced partitions. The recursive and iterative versions preserve the same temporal complexity properties.

Space Complexity:

Its memory requirements are not proportionate to the amount of the input because Quick Sort is an in-place sorting algorithm. The iterative version might utilize less memory than the recursive version because it uses an explicit stack.

### Example:

Let us take an example to illustrate the iterative quick sort in C:

Output:

```Original array: 12 4 5 6 7 3 1 15
Sorted array: 1 3 4 5 6 7 12 15
```

Explanation:

1. In this example, the program includes the typical directives for C++. Include <iostream> header file to operate on input/output, and include <stack> to use the stack data structure.
2. It is how the partition function works. Its parameters are the low index (low), the high index (high), and an array (arr). After selecting the pivot as the element at the high index, the Function rearranges the array's elements so that any element less than or equal to the pivot is on the left and any element more significant than the pivot is on the right. After dividing, the pivot element's index is returned.
3. It is the Function known as iterativeQuickSort. The low index (low), the high index (high), and an array (arr) are required. The sub-arrays that require sorting are tracked using a stack (stk). It uses a while loop to handle the sub-arrays iteratively. It removes a sub-array from the stack at the beginning of each iteration, divides it using the partition function, and pushes it back onto the stack if the sub-arrays are already sorted.
4. After that, we use the printArray Function, whose parameters are an array (arr) and size (size). It outputs the array's elements to the standard output.
5. The main() function is the primary Function and the program's point of entry. First, an array is initialized, and then it uses the printArray Function to display the original array, the iterativeQuickSort method to sort it, and finally it displays the sorted array again.

## Conclusion:

In conclusion, the software effectively manages the sorting process by implementing the iterative Quick Sort algorithm utilizing a stack. Rearranging elements around a pivot is the responsibility of the partition function, and sorting is done iteratively using a stack by the iterativeQuickSort Function. Using an example array, the main Function shows how to use these functions.