本人没进面试,下面是身边同学的面试题目。
1. 如何优化快速排序
在这里我用的是Median Of Three Partitioning
#include <iostream> using namespace std; void swap(int *a, int *b) { int t = *a; *a = *b; *b = t; } int partition(int data[], int low, int high) { int pivot = data[high]; int i = low - 1; int j = high; while (1) { while (data[++i] < pivot) { } while (data[--j] > pivot) { if (j == low) { break; } } if (i >= j) { break; } swap(data[i], data[j]); } swap(data[i], data[high]); return i; } void QuickSort(int data[], int low, int high) { if (low >= high) { return ; } int pivot = partition(data, low, high); QuickSort(data, low, pivot-1); QuickSort(data, pivot+1, high); } /* 函数介绍:Improved QuickSort Median-of-Three Partitioning */ void QuickSort_Opt(int data[], int low, int high) { if (low >= high) { return ; } int mid = low + (high - low) / 2; swap(data[mid], data[high-1]); // 用data[high-1]记录middle value, // 并作为支点 if (data[low] > data[high-1]) { swap(data[low], data[high-1]); } if (data[low] > data[high]) { swap(data[low], data[high]); } if (data[high-1] > data[high]) { swap(data[high-1], data[high]); } int pivot = partition(data, low+1, high-1); // 此处剔除了第一个元素和最后一个元素, // 因为第一个元素肯定小于支点的值, //最后一个元素肯定大于支点的值; QuickSort_Opt(data, low, pivot-1); QuickSort_Opt(data, pivot+1, high); } int main() { int data[15] = {1, 3, 5, 7, 9, 2, 4, 6, 8, 10, -1, -3, -5, -7, -9}; QuickSort_Opt(data, 0, 14); for (int i=0; i<15; ++i) { cout<<data[i]<<" "; } cout<<endl; system("pause"); return 0; }
2. 图算法
最小生成树之Kruskal
3. 如何判断两个节点是不是属于同一棵树?
考察并查集
Quick Find
Quick Union
Weighted version of quick union
Weighted quick union with Path compression by halving
4. 虚函数表放在什么地方?
根据网上查资料,得出结论是虚函数表在Linux/Unix中存放在可执行文件的只读数据段中(rodata);在微软的编译器中,虚函数表存放在常量段中。