vc++template function get random and quick sort

// ConsoleApplication2.cpp : This file contains the 'main' function. Program execution begins and ends there.
//
#include <ctime>
#include <iostream>
#include <random>

using namespace std;

template<typename T>
void swap(T* x, T* y);

template<typename T>
int partitionAsc(T* arr, int low, int high);

template<typename T>
void quickSortAsc(T* arr, int low, int high);

template<typename T>
void printArray(T* arr, int len);

template<typename T>
void getTArray(T* arr, int len);

template<typename T>
void arrayTDemo(int len);


int main(int args,char**argv)
{
    arrayTDemo<uint32_t>(atoi(argv[1]));
    std::cout << "Hello World!n";
}

template<typename T>
void arrayTDemo(int len)
{
    T* arr = new T[len];
    getTArray(arr, len);
    cout << "Before quick sort:" << endl;
    printArray(arr, len);
    cout << "After the quick sort:" << endl;
    quickSortAsc(arr, 0, len - 1);
    printArray(arr, len);
    delete[]arr;
    cout << "Finished in " << __FUNCTION__ << ",line " << __LINE__ << endl;
}

template<typename T>
void getTArray(T* arr, int len)
{
    mt19937_64 mt(time(nullptr));
    for (int i = 0;i < len;i++)
    {
        arr[i] = mt();
    }
}

template<typename T>
void printArray(T* arr, int len)
{
    for (int i = 0;i < len;i++)
    {
        cout << arr[i] << "t";
    }
    cout << endl << endl;
}

template<typename T>
void quickSortAsc(T* arr, int low, int high)
{
    if (low <= high)
    {
        int pivot = partitionAsc(arr, low, high);
        quickSortAsc(arr, low, pivot - 1);
        quickSortAsc(arr, pivot + 1, high);
    }
}

template<typename T>
int partitionAsc(T* arr, int low, int high)
{
    T pivot = arr[high];
    int i = low - 1;
    for (int j = low;j <= high;j++)
    {
        if (arr[j] < pivot)
        {
            i = i + 1;
            swap(&arr[i], &arr[j]);
        }
    }
    swap(&arr[i + 1], &arr[high]);
    return i + 1;
}

template<typename T>
void swap(T* x, T* y)
{
    T temp = *x;
    *x = *y;
    *y = temp;
}

// Run program: Ctrl + F5 or Debug > Start Without Debugging menu
// Debug program: F5 or Debug > Start Debugging menu

// Tips for Getting Started: 
//   1. Use the Solution Explorer window to add/manage files
//   2. Use the Team Explorer window to connect to source control
//   3. Use the Output window to see build output and other messages
//   4. Use the Error List window to view errors
//   5. Go to Project > Add New Item to create new code files, or Project > Add Existing Item to add existing code files to the project
//   6. In the future, to open this project again, go to File > Open > Project and select the .sln file

vc++template function get random and quick sort

 

原文链接: https://www.cnblogs.com/Fred1987/p/16656278.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍;

也有高质量的技术群,里面有嵌入式、搜广推等BAT大佬

    vc++template function get random and quick sort

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/401346

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年4月19日 上午9:13
下一篇 2023年4月19日 上午9:13

相关推荐