// SPDX-License-Identifier: Apache-2.0
// Copyright 2025 MergeSort Implementation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#ifndef MERGE_SORT_H
#define MERGE_SORT_H

#include <vector>
#include <iostream>

/**
 * @class MergeSort
 * @brief Implements the merge sort algorithm for sorting arrays
 * 
 * This class provides a stable, divide-and-conquer sorting algorithm
 * with O(n log n) time complexity and O(n) space complexity.
 */
class MergeSort {
public:
    // LIFECYCLE
    MergeSort();
    ~MergeSort();

    // OPERATIONS
    /**
     * @brief Sorts an array using merge sort algorithm
     * @param rArray Reference to the vector to be sorted
     */
    void Sort(std::vector<int>& rArray);
    
    /**
     * @brief Sorts a portion of array using merge sort algorithm
     * @param rArray Reference to the vector to be sorted
     * @param leftIndex Starting index of the portion to sort
     * @param rightIndex Ending index of the portion to sort
     */
    void Sort(std::vector<int>& rArray, int leftIndex, int rightIndex);
    
    /**
     * @brief Prints the array contents
     * @param rArray Reference to the vector to print
     */
    void PrintArray(const std::vector<int>& rArray);

private:
    // OPERATIONS
    /**
     * @brief Merges two sorted subarrays into one sorted array
     * @param rArray Reference to the array containing both subarrays
     * @param leftIndex Starting index of first subarray
     * @param middleIndex Ending index of first subarray
     * @param rightIndex Ending index of second subarray
     */
    void Merge(std::vector<int>& rArray, int leftIndex, int middleIndex, int rightIndex);
    
    /**
     * @brief Recursive helper function for merge sort
     * @param rArray Reference to the vector to be sorted
     * @param leftIndex Starting index of the portion to sort
     * @param rightIndex Ending index of the portion to sort
     */
    void MergeSortRecursive(std::vector<int>& rArray, int leftIndex, int rightIndex);
};

#endif // MERGE_SORT_H