排序演算法實現(下)

來源:互聯網
上載者:User

標籤:時間   last   osi   head   from   nod   tab   時間複雜度   適用於   

④希爾排序

時間複雜度:n^1.25 ~ 1.6n^1.25。

優點:快,資料移動少。

缺點:不穩定,增量的選擇無法確切知道,只能憑經驗來取。

template <class Record>void Sortable_list<Record>::shell_sort() {    int increment, start;    increment = count;    do {        increment = increment/3+1;        for (start = 0; start < increment; start++) {            sort_interval(start, increment); //插入排序         }    } while (increment > 1);} 


⑤歸併排序

時間複雜度:O(nlgn)。

優點:穩定,快。

缺點:需要O(n)的輔助空間,空間複雜度高。

//鏈表template <class Record>void Sortable_list<Record>::merge_sort() {    recursive_merge_sort(head);}template <class Record>void Sortable_list<Record>::recursive_merge_sort(Node<Record>* &sub_list) {    if (sub_list != NULL && sub_list->next != NULL) {        Node<Record>* second_half = divide_from(sub_list);        recursive_merge_sort(sub_list);        recursive_merge_sort(second_half);        sub_list = merge(sub_list, second_half);    }}//將一個鏈表分成兩半template <class Record>void Sortable_list<Record>::divide_from(Node<Record>* sub_list) {    Node<Record>* position,                * midpoint,                *second_half;    if ((midpoint = sub_list) == NULL) return NULL;    position = midpoint->next;    while (position != NULL) {        position = position->next;        if (position != NULL) {            midpoint = midpoint->next;            position = position->next;        }    }    second_half = midpoint->next;    midpoint->next = NULL;    return second_half;}//歸併兩個已排序的鏈表template <class Record>void Sortable_list<Record>::merge(Node<Record>* first, Node<Record>* second) {    Node<Record>* last_sorted;    Node<Record> combined;    last_sorted = &combined;    while (first != NULL && second != NULL) {        if (first->entry <= second->entry) {            last_sorted->next = first;            last_sorted = first;            first = first->next;        }        else {            last_sorted->next = second;            last_sorted = second;            second = second->next;        }    }    if (first == NULL) last_sorted->next = second;    else last_sorted->next = first;    return combined->next;}


⑥快速排序

時間複雜度:O(nlgn)。

優點:極快,資料移動少。

缺點:不穩定。

//快速排序 O(nlgn)template <class Record>void Sortable_list<Record>::quick_sort() {    recursive_quick_sort(0, count-1);}template <class Record>void Sortable_list<Record>::recursive_quick_sort(int low, int high) {    int pivot_position;    if (low < high) {        pivot_position = partition(low, high);        recursive_quick_sort(low, pivot_position-1);        recursive_quick_sort(pivot_position+1, high);    }}template <class Record>void Sortable_list<Record>::partition(int low, int high) {    Record pivot;    int i, last_small;    swap(low, (low+high)/2);    pivot = entry[low];    last_small = low;    for (i = low+1; i < high; i++) {        if (entry[i] < pivot) {            last_small = last_small+1;            swap(last_small, i);        }    }    swap(low, last_small);    return last_small;}


⑦堆排序

時間複雜度:O(nlgn)。

優點:空間複雜度小。

缺點:只適用於順序表。

//堆排序 O(nlgn)//只適用於順序表template <class Record>void Sortable_list<Record>::heap_sort() {    Record current;    int last_unsorted;    build_heap();    for (last_unsorted = count-1; last_unsorted > 0; last_unsorted--) {        current = entry[last_unsorted];        entry[last_unsorted] = entry[0];        insert_heap(current, 0, last_unsorted-1);    }}template <class Record>void Sortable_list<Record>::insert_heap(const Record ¤t, int low, int high) {    int large;    large = 2*low+1;    while (large <= high) {        if (large < high && entry[large] < entry[large+1]) large++;        if (current >= entry[large]) break;        else {            entry[low] = entry[large];            low = large;            large = 2*low+1;        }    }    entry[low] = current;}template <class Record>void Sortable_list<Record>::build_heap() { //建立初始的堆    int low;    for (low = count/2-1; low >= 0; low--) {        Record current = entry[low];        insert_heap(current, low, count-1);    }}

 

排序演算法實現(下)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.