目錄:
- 1.quick sort 和 merge sort 的比較
- 2.quick sort
- 3.merge sort
1. quick sort 和 merge sort 的區別於聯繫
1). 時間複雜度:都是
構建線段樹:
/**
* Definition of SegmentTreeNode:
* class SegmentTreeNode {
* public:
* int start, end;
* SegmentTreeNode *left, *right;
* SegmentTreeNode(int start, int end) {
* this->start = start, this->end = end;
* this->left = this->right = NULL;
* }
* }
*/
class Solution {
public:
/*
* @param start: start value.
* @param end: end value.
* @return: The root of Segment Tree.
*/
SegmentTreeNode * build(int start, int end) {
// write your code here
if(start > end){
return NULL;
}
SegmentTreeNode *root = new SegmentTreeNode(start,end);
if(start==end){
return root;
}
int mid = (start+end)/2;
root->left = build(start,mid);
root->right = build(mid+1,end);
return root;
}
};
/**
* Definition of SegmentTreeNode:
* class SegmentTreeNode {
* public:
* int start, end, max; //min
* SegmentTreeNode *left, *right;
* SegmentTreeNode(int start, int end, int max) {
* this->start = start, this->end = end;
* this->left = this->right = NULL;
* this->max = max;
* }
* }
*/
class Solution {
public:
/*
* @param start: start value.
* @param end: end value.
* @return: The root of Segment Tree.
*/
SegmentTreeNode * buildhelper(int start, int end, int max) {
// write your code here
if(start > end){
return NULL;
}
SegmentTreeNode *root = new SegmentTreeNode(start,end,max);
if(start==end){
return root;
}
int mid = (start+end)/2;
root->left = build(start,mid);
root->right = build(mid+1,end);
root->max = max(root->left->max, root->right->max);
return root;
}
};