首页 > 编程语言 >vc++template function get random and quick sort

vc++template function get random and quick sort

时间:2022-09-04 22:13:01浏览次数:72  
标签:function sort arr get int void len low template

// 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

 

标签:function,sort,arr,get,int,void,len,low,template
From: https://www.cnblogs.com/Fred1987/p/16656278.html

相关文章

  • Maximum Number of Robots Within Budget
    MaximumNumberofRobotsWithinBudgetYouhave$n$robots.Youaregiventwo0-indexedintegerarrays, chargeTimes and runningCosts ,bothoflength$n$.......
  • mt19937_64 get random numbers
    #include<ctime>#include<iostream>#include<random>usingnamespacestd;voiduInt32Array(intlen);intmain(intargs,char**argv){uInt32Array(a......
  • Sorting Color Balls
    ProblemStatementThereare$N$ballsarrangedfromlefttoright.Thecolorofthe$i$-thballfromtheleftisColor$C_i$,andaninteger$X_i$iswritteno......
  • [Go] Functions
    Functioncanreturnmultivalues:funcprintAge(ageint)(int,int){ return12,age}funcmain(){ age1,age2:=printAge(8) fmt.Println(age1) fmt.Print......
  • .Net下的简易Http请求调用(Post与Get)
    http请求调用是开发中经常会用到的功能。在内,调用自有项目的WebApi等形式接口时会用到;在外,调用一些第三方功能接口时,也会用到,因为,这些第三方功能往往是通过http地址的形式......
  • leetcode 83. Remove Duplicates from Sorted List 删除排序链表中的重复元素(简单)
    一、题目大意给定一个已排序的链表的头head,删除所有重复的元素,使每个元素只出现一次。返回已排序的链表。示例1:输入:head=[1,1,2]输出:[1,2]示例2:输入:h......
  • mysql 报错This function has none of DETERMINISTIC解决方案
    本文章向朋友们介绍开启bin-log日志mysql报错:ThisfunctionhasnoneofDETERMINISTIC,NOSQL解决办法,创建存储过程时出错信息:ERROR1418(HY000):Thisfunctionh......
  • .Net下的Http请求调用(Post与Get)
    http请求调用是开发中经常会用到的功能。在内,调用自有项目的WebApi等形式接口时会用到;在外,调用一些第三方功能接口时,也会用到,因为,这些第三方功能往往是通过http地址的形式......
  • MySQL教程 - 存储过程与自定义函数(Produce & Function)
    更新记录转载请注明出处。2022年9月4日发布。2022年9月4日从笔记迁移到博客。存储过程与函数说明存储过程和函数是一条或多条SQL语句的集合存储过程的返回值通......
  • Ubuntu下使用apt-get命令查询并安装指定版本的软件
    执行以下命令,查询软件所有的版本号sudoapt-cachemadison<package><package>为需要安装的包名,返回结果第二列即可用的版本号执行以下命令,安装指定版本的软件sudoapt......