排序

快速排序的时间复杂度O(nlogn)每次排序需要执行n次,一共需要执行log2 n 次。

链表快排:

struct listNode {
int val;
listNode *next;
listNode(int x) : val(x), next(NULL) {};
};

void qSort(listNode *, listNode *);
listNode *partion(listNode *, listNode *);
listNode *quickSort(listNode *head) {
if(head == NULL && head -> next == NULL)
return head;
qSort(head, NULL);
return head;
};
void qSort(listNode *head, listNode *tail) {
if(head != tail && head -> next != tail) {    //head != NULL && head -> next != NULL;
listNode *mid = partion(head, tail);
qSort(head, mid);
qSort(mid -> next, tail);
}
};
listNode *partion(listNode *head, listNode *tail) {
int key = head -> val;
listNode *loc = head;
for(listNode *i = head -> next; i != tail; i = i -> next)
if(key > (i -> val)) {
loc = loc -> next;
if(loc != i)
swap(loc -> val, i -> val);
}
swap(loc -> val, head -> val);
return loc;
};

归并排序:

非递归算法:百度百科,递归算法需要额外开销,非递归主要是设置步长并且在合并函数要考虑先处理最后一步剩下的元素。

void Merge(int* data,int a,int b,int length,int n){
 int right;
 if(b+length-1 >= n-1) right = n-b;
 else right = length;
 int* temp = new int[length+right];
 int i=0, j=0;
 while(i<=length-1 && j<=right-1){
     if(data[a+i] <= data[b+j]){
         temp[i+j] = data[a+i];i++;
      }
     else{
        temp[i+j] = data[b+j];
        j++;
      }
 }
 if(j == right){//a中还有元素,且全都比b中的大,a[i]还未使用
   memcpy(temp + i + j, data + a + i, (length - i) * sizeof(int));
 }
  else if(i == length){
      memcpy(temp + i + j, data + b + j, (right - j)*sizeof(int));
  }
 memcpy(data+a, temp, (right + length) * sizeof(int));
 delete [] temp;
}
void MergeSort(int* data, int n){
 int step = 1;
 while(step < n){
     for(int i=0; i<=n-step-1; i+=2*step)
         Merge(data, i, i+step, step, n);
    //将i和i+step这两个有序序列进行合并
    //序列长度为step
    //当i以后的长度小于或者等于step时,退出
     step*=2;//在按某一步长归并序列之后,步长加倍
 }
}
归并排序是链表的最佳排序手段,在链表中由于指针避免了额外的空间开销。
class Solution {
public:
    ListNode *mergeSortList(ListNode *head) {
        // IMPORTANT: Please reset any member data you declared, as
        // the same Solution instance will be reused for each test case.
        //链表归并排序
        if(head == NULL || head->next == NULL)return head;
        else
        {
            //快慢指针找到中间节点
            ListNode *fast = head,*slow = head;
            while(fast->next != NULL && fast->next->next != NULL)
            {
                fast = fast->next->next;
                slow = slow->next;
            }
            fast = slow;
            slow = slow->next;
            fast->next = NULL;
            fast = sortList(head);//前半段排序
            slow = sortList(slow);//后半段排序
            return merge(fast,slow);
        }
         
    }
    // merge two sorted list to one
    ListNode *merge(ListNode *head1, ListNode *head2)
    {
        if(head1 == NULL)return head2;
        if(head2 == NULL)return head1;
        ListNode *res , *p ;//res用来作为头结点,不变,链表的链接指针用p来指定 在最开始没用P,错误
        if(head1->val < head2->val)
            {res = head1; head1 = head1->next;}
        else{res = head2; head2 = head2->next;}
        p = res;
         
        while(head1 != NULL && head2 != NULL)
        {
            if(head1->val < head2->val)
            {
                p->next = head1;
                head1 = head1->next;
            }
            else
            {
                p->next = head2;
                head2 = head2->next;
            }
            p = p->next;
        }
        if(head1 != NULL)p->next = head1;
        else if(head2 != NULL)p->next = head2;
        return res;
    }
};
 
posted on 2017-08-07 00:41  bloomingFlower  阅读(140)  评论(0编辑  收藏  举报