[building block] merge sort @ Python

Here is the basic algorithm about merge sort:

def merge(s1,s2,s):
    i=j=0
    while i + j < len(s):
        if j == len(s2) or (i < len(s1) and s1[i] < s2[j]):
            s[i + j] = s1[i]
            i += 1
        else:
            s[i + j] = s2[j]
            j += 1

def merge_sort(s):
    """ Sort the elements of python list s using merge-sort algorithm"""
    # Time compelxity: O(nlogn), where n = len(s)
    n = len(s)
    if n < 2:
        return
    # Divide
    mid = n // 2
    s1 = s[:mid]
    s2 = s[mid:]    

    #conquer (with recursion)
    merge_sort(s1)
    merge_sort(s2)

    # merge results
    merge(s1,s2,s)
        

insertion sort: time complexity: O(n^2)

def insertion_sort(A):
    ”””Sort list of comparable elements into nondecreasing order.”””
    for k in range(1, len(A)): # from 1 to n-1
        cur = A[k] # current element to be inserted
        j = k # find correct index j for current
        while j > 0 and A[j-1] > cur: # element A[j-1] must be after current
            A[j] = A[j-1]
            j -= 1
        A[j] = cur # cur is now in the right place
时间: 2024-11-03 12:05:51

[building block] merge sort @ Python的相关文章

【 python 学习笔记 -- 数据结构与算法 】归并排序 Merge Sort

[归并排序]这里我们利用递归算法不断地将列表一分为二,base case就是列表中没有元素或者只剩一个元素,因为此时这个子列表必然是正序的:然后再逐步把两个排序完成的子列表合并成一个新的正序列表,直到所有元素排序完毕. [示意图]这是一个从下至上的过程(Bottom-Up) 将列表不断从中间分成两个子列表,直到到达最底部,子列表中只有一个元素 然后,从下至上不断合并两个子列表,将两个子列表的所有元素排序形成一个新的列表. [ implementation of merge sort ] 可以利用

[leetcode]Merge Intervals @ Python

原题地址:https://oj.leetcode.com/problems/merge-intervals/ 题意: Given a collection of intervals, merge all overlapping intervals. For example,Given [1,3],[2,6],[8,10],[15,18],return [1,6],[8,10],[15,18]. 解题思路:先将区间按照每个start的值来排序,排好序以后判断一个区间的start值是否处在前一个区间

hdu 2818 Building Block

Building Block Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 3986    Accepted Submission(s): 1235 Problem Description John are playing with blocks. There are N blocks (1 <= N <= 30000) numbe

Building Block[HDU2818]

Building Block Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 5426 Accepted Submission(s): 1663 Problem DescriptionJohn are playing with blocks. There are N blocks (1 <= N <= 30000) numbered 1...

Building Block HDU - 2818 (并查集)

Building Block HDU - 2818 题意:搬砖...每一次可以把a所在的那一堆放到b所在的那一堆上面,问第x号砖下面有几块砖. 记录一下到根节点的距离(dw),以及根节点上方有几块砖(up). 1 #include <bits/stdc++.h> 2 using namespace std; 3 4 const int maxn=30010; 5 int f[maxn],up[maxn],dw[maxn]; 6 7 int gf(int x){ 8 if(x==f[x]){ 9

[算法]——归并排序(Merge Sort)

归并排序(Merge Sort)与快速排序思想类似:将待排序数据分成两部分,继续将两个子部分进行递归的归并排序:然后将已经有序的两个子部分进行合并,最终完成排序.其时间复杂度与快速排序均为O(nlogn),但是归并排序除了递归调用间接使用了辅助空间栈,还需要额外的O(n)空间进行临时存储.从此角度归并排序略逊于快速排序,但是归并排序是一种稳定的排序算法,快速排序则不然. 所谓稳定排序,表示对于具有相同值的多个元素,其间的先后顺序保持不变.对于基本数据类型而言,一个排序算法是否稳定,影响很小,但是

排序算法二:归并排序(Merge sort)

归并排序(Merge sort)用到了分治思想,即分-治-合三步,算法平均时间复杂度是o(nlgn). (一)算法实现 1 private void merge_sort(int[] array, int first, int last) { 2 if (first + 1 < last) { 3 int mid = (first + last) / 2; 4 merge_sort(array, first, mid); 5 merge_sort(array, mid, last); 6 7 /

SQL Tuning 基础概述06 - 表的连接方式:Nested Loops Join,Merge Sort Join &amp; Hash Join

nested loops join 嵌套循环 merge sort join 排序合并 hash join 哈希连接 nested loops join(嵌套循环)   驱动表返回几条结果集,被驱动表访问多少次,有驱动顺序,无须排序,无任何限制. 驱动表限制条件有索引,被驱动表连接条件有索引. hints:use_nl() merge sort join(排序合并)   驱动表和被驱动表都是最多访问1次,无驱动顺序,需要排序(SORT_AREA_SIZE),连接条件是<>或like导致无法使用

STL 源码剖析 算法 stl_algo.h -- merge sort

本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie merge sort ---------------------------------------------------------------------- 描述:归并排序 思路: 1.将区间对半分割 2.对左.右段分别排序 3.利用inplace_merge将左.右段合并成为一个完整的有序序列 复杂度:O(nlog n) 源码: template<class Bidirection