《数据结构与算法分析(c描述)》—— 归并排序

news/2024/5/17 15:35:36 标签: 归并排序, 数据结构, 算法, 排序算法

归并排序是分治法的一个好例子,属于基于比较的内部/外部排序算法。普通的归并算法具有 O(n * log(n)) 的时间和 O(n) 的空间复杂度。就地归并算法能帮助降低额外空间开销,使得归并排序更高效。

时间复复杂度的推导也非常的典型:

T(N)=2T(N2)+N

T(N)N=T(N/2)N/2+1

T(N)N=11+logN

T(N)=O(NlogN)

归并排序之所以可以轻松应用到外部排序上,是因为归并排序的归并过程是顺序扫描的,可以用来处理顺序存储的文件,多路归并还可以用多线程或是并行计算来加速,这样处理大文件就方便了。而想随机访问一个文件,就没那么快了。

排序数组

void merge(int a[], int temp[], int left, int right, int rightEnd) {
    int leftEnd = right - 1;
    int numElement = rightEnd - left + 1;
    int i = left;
    while (left <= leftEnd && right <= rightEnd) {
        if (a[left] < a[right])
            temp[i++] = a[left++];
        else
            temp[i++] = a[right++];
    }

    while (left <= leftEnd)
        temp[i++] = a[left++];

    while (right <= rightEnd)
        temp[i++] = a[right++];

    for (i = 0; i < numElement; i++, rightEnd--)
        a[rightEnd] = temp[rightEnd];
}

void mergeSort(int a[], int temp[], int leftBegin, int rightEnd) {
    int mid;
    if (leftBegin < rightEnd) {
        mid = (leftBegin + rightEnd)/2;
        mergeSort(a, temp, leftBegin, mid);
        mergeSort(a, temp, mid + 1, rightEnd);
        merge(a, temp, leftBegin, mid + 1, rightEnd);
    }
}


int main () {
    int a[10] = {9, 2, 8, 3, 4, 1, 5, 18, 11, 14};
    int  temp[10];
    mergeSort(a, temp, 0, 9);
    for (int i = 0; i < 10 ; i++)
        printf("%d#", a[i]);
    printf("\n");
}

合并两个有序的链表

leetcode 21 题正是应用归并排序来实现两个有序链表的合并。

下面代码可以通过测试。

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
    ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
        if (l1 == NULL)
            return l2;
        if (l2 == NULL)
            return l1;
        ListNode *head = new ListNode(0);
        ListNode *pos = head;
        int temp;

        while(l1 && l2) {
            if (l1->val < l2->val) {
                temp = l1->val;
                l1 = l1->next;
            }
            else {
                temp = l2->val;
                l2 = l2->next;
            }
            pos->next = new ListNode(temp);
            pos = pos->next;
        }

        while(l1 != NULL) {
            pos->next = new ListNode(l1->val);
            pos = pos->next;
            l1 = l1->next;
        }

        while(l2 != NULL) {
            pos->next = new ListNode(l2->val);
            pos = pos->next;
            l2 = l2->next;
        }
        return head->next;
    }

http://www.niftyadmin.cn/n/941467.html

相关文章

[leetcode Q3] —— Longest Substring Without Repeating Characters

Given a string, find the length of the longest substring without repeating characters. For example, the longest substring without repeating letters for “abcabcbb” is “abc”, which the length is 3. For “bbbbb” the longest substring is “b”, with the l…

linux 后台开发类常见问题及知识点

一、linux和os: netstat &#xff1a;显示网络状态tcpdump&#xff1a;主要是截获通过本机网络接口的数据&#xff0c;用以分析。能够截获当前所有通过本机网卡的数据包。它拥有灵活的过滤机制&#xff0c;可以确保得到想要的数据。ipcs&#xff1a;检查系统上共享内存的分配i…

网易2014校招-运维开发工程师:面试题目

python 1、python是强类型还是弱类型的语言 2、python的动态性体现在哪 3、python的namespace&#xff1a;四种&#xff1b;len()等函数的命名空间 4、range和xrange的区别&#xff0c;谈到了迭代器 5、于是问怎么实现迭代器&#xff0c;然后又问了生成器&#xff0c;yiel…

《数据结构与算法分析(c描述》—— 快速排序

1. 快速排序 快速排序是最快的已知排序算法&#xff0c;平均运行时间为 O(NlogN) &#xff0c;最坏情况的性能为 O(N^2)。 将数组 S 快速排序由下列简单的四步组成&#xff1a; 如果 S 中元素个素是0或1&#xff0c;则返回取 S 中任一元素作为枢纽元将 S - {v} &#xff08;…

[leetcode Q20] Valid Parentheses

题目要求检查括号字符串序列是否匹配 简单的算法是使用一个栈&#xff1a; 做一个空栈&#xff0c;顺序扫描字符串直到末尾如果读到开放符号 “([{” 则入栈否则读到 “)]}”&#xff0c;检查栈是否为空&#xff0c;空则报错栈不空则检查栈顶元素是否与字符匹配扫描结束&…

[leetcode Q26] Remove Duplicates from Sorted Array

1. 题目要求 Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length. Do not allocate extra space for another array, you must do this in place with constant memory. For example, Given input arr…

[leetcode Q28] Implement strStr()

实现 c 函数 strstr() Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack. 返回子字符串在原字符串中第一次匹配的位置&#xff0c;否则放回 -1 实现还是比较简单的就是需要注意一些细节 思路…

[leetcode Q29] Divide Two Integers

1. 题目 Divide two integers without using multiplication, division and mod operator. If it is overflow, return MAX_INT. 要求不使用乘除法实现两个整数相除运算。 2. 思路 不使用乘法除法&#xff0c;要想快数得到结果就只能考虑位运算。本题主要考察位运算。 被除…