Sunday, September 24, 2023

Merge Sort in C++

 A simple program to demonstrate merge sort algorithm in C++ programming language.

I am currently accepting programming work, IT projects, school and application development, programming projects, thesis and capstone projects, IT consulting work, computer tutorials, and web development work kindly contact me at the following email address for further details.  If you want to advertise on my website kindly contact me also in my email address also. Thank you.

My email address is the following jakerpomperada@gmail.com, jakerpomperada@aol.com, and jakerpomperada@yahoo.com.

My mobile number here in the Philippines is 09173084360.

Please subscribe to my channel  https://www.youtube.com/channel/UCOs-lpOoIeJoh6gpJthPoGg

=================================================


You can buy my C++ book online at  


https://www.mindshaperspublishing.com/product/beginners-guide-to-c-programming/


You can buy my book in introduction to computer networking at 

https://www.unlimitedbooksph.com/product-page/introduction-to-computer-networking


Want to support my channel?

GCash Account

Jake Pomperada




09173084360


Paypal

https://paypal.me/jakerpomperada


Patreon

https://www.patreon.com/jakerpomperada


Thank you very much for your support.




Program Listing

merge.cpp

#include <iostream>

#include <vector>


// Merge two sorted arrays into one sorted array

void merge(std::vector<int>& arr, int left, int mid, int right) {

    int n1 = mid - left + 1;

    int n2 = right - mid;


    std::vector<int> L(n1);

    std::vector<int> R(n2);


    // Copy data to temporary arrays L and R

    for (int i = 0; i < n1; ++i)

        L[i] = arr[left + i];

    for (int j = 0; j < n2; ++j)

        R[j] = arr[mid + 1 + j];


    int i = 0, j = 0, k = left;


    // Merge the temporary arrays back into arr

    while (i < n1 && j < n2) {

        if (L[i] <= R[j]) {

            arr[k] = L[i];

            ++i;

        } else {

            arr[k] = R[j];

            ++j;

        }

        ++k;

    }


    // Copy the remaining elements of L and R (if any)

    while (i < n1) {

        arr[k] = L[i];

        ++i;

        ++k;

    }


    while (j < n2) {

        arr[k] = R[j];

        ++j;

        ++k;

    }

}


// Merge Sort function

void mergeSort(std::vector<int>& arr, int left, int right) {

    if (left < right) {

        int mid = left + (right - left) / 2;


        // Sort the first and second halves

        mergeSort(arr, left, mid);

        mergeSort(arr, mid + 1, right);


        // Merge the sorted halves

        merge(arr, left, mid, right);

    }

}


int main() {

    std::vector <int> arr = {12, 11, 13,-67,23,-452, 5, 6, 7};



    std::cout << "\n\t\tMerge Sort in C++\n\n";

    std::cout << "\n\tOriginal array: ";

    for (int num : arr)

        std::cout << num << " ";


    mergeSort(arr, 0, arr.size() - 1);


    std::cout << "\n\n\tSorted array: ";

    for (int num : arr)

        std::cout << num << " ";

    std::cout <<"\n\n";

    return 0;

}


No comments:

Post a Comment