#include

本文是后续文章中涉及到的基本函数源代码。

001、生成随机数

View Code
 1 unsigned int latestRandNum = 1;
 2 
 3 /*设置随机数种子*/
 4 void srandGLibC(unsigned int seed)
 5 {
 6     latestRandNum = (seed == 0 ? 1 : seed);
 7 }
 8 
 9 /*生成[0, 2147483647]之间的随机数*/
10 int randGLibC(void)
11 {
12     int randNum = ((latestRandNum * 1103515245) + 12345) & 0x7fffffff;
13     latestRandNum = randNum;
14     
15     return (latestRandNum); 
16 }
17 
18 /*在指定范围内生成随机数*/
19 int randRange(int min, int max)
20 {
21     unsigned int range = max - min + 1;
22 
23     if (range == 1 || range > 2147483648)
24     {
25         return min;
26     }
27     else if (range < 1)
28     {
29         throw std::range_error("The upper limit is less than the lower limit!");
30         return -1;
31     }
32 
33     return (randGLibC() % range + min);
34 }

002、生成元素为[min, max]之间随机数的整型数组

View Code
1 /*生成元素为[min, max]之间随机数的整型数组*/
2 void randArray(int a[], int len, int min, int max)
3 {
4     //srandGLibC(static_cast<unsigned int>(time(NULL)) + rand());
5     for (int i = 0; i != len; ++i)
6     {
7         a[i] = randRange(min, max);
8     }
9 }

003、复制数组

View Code
1 /*复制数组*/
2 void copyArray(int src[], int src_beg, int dest[], int dest_beg, int len)
3 {
4     for (int i = 0; i != len; ++i)
5     {
6         dest[dest_beg + i] = src[src_beg + i];
7     }
8 }

004、交换数组元素

View Code
 1 /*交换数组元素*/
 2 void swap(int a[], int loca, int locb)
 3 {
 4     if (loca != locb)
 5     {
 6         int temp = a[loca];
 7         a[loca] = a[locb];
 8         a[locb] = temp;
 9     }
10 }

005、快速排序

View Code
 1 /*快排一趟划分*/
 2 int partition(int a[], int beg, int end)
 3 {
 4     int pivot = a[end];
 5     int i = beg - 1;
 6 
 7     for (int j = beg; j != end; ++j)
 8     {
 9         if (a[j] <= pivot)
10         {
11             swap(a, ++i, j);
12         }
13     }
14     swap(a, i + 1, end);
15 
16     return i + 1;
17 }
18 
19 /*快速排序*/
20 void quickSort(int a[], int beg, int end)
21 {
22     if (beg < end)
23     {
24         int mid = partition(a, beg, end);
25         quickSort(a, beg, mid - 1);
26         quickSort(a, mid + 1, end);
27     }
28 }
1 /*交换数组元素*/
2 void swap(int a[], int loca, int locb)
3 {
4     int temp = a[loca];
5     a[loca] = a[locb];
6     a[locb] = temp;
7 }
 1 /*指定枢轴节点的划分*/
 2 int partition_specify_pivot(int a[], int beg, int end, int pivotloc)
 3 {
 4     int pivot = a[pivotloc];
 5     int i = beg - 1;
 6     int e = end;
 7 
 8     swap(a, pivotloc, e);
 9     for (int j = beg; j != e; ++j)
10     {
11         if (a[j] <= pivot)
12         {
13             ++i;
14             swap(a, i, j);
15         }
16     }
17     swap(a, i + 1, e);
18 
19     return i + 1;
20 }

006、直接插入排序

View Code
 1 /*直接插入排序*/
 2 void insertionSort(int a[], int beg, int end)
 3 {
 4     for (int j = beg + 1; j <= end; ++j)
 5     {
 6         int key = a[j];
 7         int i = j - 1;
 8         while ((i >= beg) && (a[i] > key))
 9         {
10             a[i + 1] = a[i];
11             --i;
12         }
13         a[i + 1] = key;
14     }
15 }

007、输出数组元素

View Code
 1 /*输出数组元素*/
 2 void outputArray(std::ostream& os, int a[], int beg, int end, int step, int width)
 3 {
 4     int len = end - beg + 1;
 5     //下标所需的位数
 6     int iterWidth = static_cast<int>(log(static_cast<double>(len - 1)) / log(10.0)) + 1;
 7 
 8     os << "The content of the array is:" << std::endl;
 9     if (len > 0)
10     {
11         os  << "array[" << std::setfill('0') << std::setw(iterWidth) << beg 
12             << "] = " << std::setfill(' ') << std::setw(width) << a[beg] 
13             << std::endl;
14     }
15     for (int i = beg + step; i <= end; i += step)
16     {
17         os << "array[" << std::setfill('0') << std::setw(iterWidth) << i << "] = " 
18            << std::setfill(' ') << std::setw(width) << a[i] << std::endl;
19     }
20 }

 

 

 

posted on 2012-10-28 23:15  Snser  阅读(465)  评论(0编辑  收藏  举报