how to calculate the performed permutations of elements and display the sorting process of Quicksort in c++?

I am a newbie in quicksort algorithm and I wanted to count swap and output the process of it(display the intermediate states of the array being sorted after each performed permutation.). Thank you.

#include <iostream>
#include <iomanip>
using namespace std;


void print(const int a[])
{
    for(int i=0;i < 10; i++) {
    cout << setw(3) << a[i];
    }
    cout << endl;
}

int Partition( int *A , int start , int end )
{
     int pivot = A[end]; 
     int partitionIndex = start; 

     int i = start;
 while ( i < end ){ 
    if( A[i] < = pivot ){
        swap ( A[i], A[partitionIndex] );
        partitionIndex++; 
    }
    i ++ ;
    print(A); //something wrong here?
}
    swap( A[partitionIndex], A[end]);
    return partitionIndex;  
}

Here is my recursion function

void QuickSort( int *A , int start , int end ){
        if( start < end ){
           int partitionIndex = Partition( A , start , end );
            QuickSort( A , start , partitionIndex - 1 );
            QuickSort( A , partitionIndex + 1 , end );
 }

The main function

int main(){
     int n = 10;
     int *A = new int[n]; 

     for (int i = 0 ; i < n ; i ++ ){
         cin >> A[i];
     }
     cout<<endl;

  QuickSort( A , 0 , n - 1 ); 
  for(int i = 0 ; i < n ; i++){
    cout << A[i] <<" "; 
  }
  delete []A;
 }


Read more here: https://stackoverflow.com/questions/66343001/how-to-calculate-the-performed-permutations-of-elements-and-display-the-sorting

Content Attribution

This content was originally published by Napole at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: