Decrease-and-Conquer

2023-11-11 22:59
文章标签 conquer decrease

本文主要是介绍Decrease-and-Conquer,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

                        第四章 Decrease-and-Conquer

 增量方法:自下而上的变化通常是以迭代方式实现;以问题的一个小地方开始。

The bottom-up variation is usually implemented iteratively, starting with a solution to the smallest instance of the problem; it is called sometimes the incremental approach.

Three major variations of decrease-and-conquer:

               Decrease by a constant
               Decrease by a constant factor
               Variable size decrease

 

4.1 Insertion Sort

      4.1.1 介绍

    如果我们已经找到了数组中n-2个元素的正确位置,那么第 A[n-1] 个元组放在哪呢?只需要在刚刚排好序的数组中,找到

一个位置插入这最后一个元素即可

Following the technique’s idea, we assume that the smaller problem of sorting the array A[0..n − 2] has already been solved to give us a sorted array of size n − 1: A[0]≤ . . . ≤ A[n − 2]. How can we take advantage of this solution to the smaller problem to get a solution to the original problem by taking into account the element A[n − 1]? Obviously, all we need is to find an appropriate position for A[n − 1] among the sorted elements and insert it there.

 

        这通常是通过从右到左扫描已排序的子阵列来完成的,直到遇到小于或等于[N-1]的第一个元素,在该元素后面插入一个[N-1]。

This is usually done by scanning the sorted subarray from right to left until the first element smaller than or equal to A[n − 1] is encountered to insert A[n − 1] right after that element. 

 

虽然插入排序显然基于递归思想,但是自下而上(即迭代)实现该算法更有效。

Though insertion sort is clearly based on a recursive idea, it is more efficient to implement this algorithm bottom up, i.e., iteratively

 

4.1.2 伪代码:

           

代码实现或其他详细解析请参考: https://blog.csdn.net/qq_36098284/article/details/98098798

 

4.1.3例子:

              

4.1.4  时间复杂度分析

该算法的关键是每次的比较过程,即 A[j] 和 v 的大小

The basic operation of the algorithm is the key comparison A[j] > v.

 

比较的次数依赖于输入的数据,最坏的情况是,当序列恰巧是降序,而我们希望得到升序的时候,从i = 1 开始,需要比较该数字其前面的所有数字,并且需要依次进行移动。最坏的情况下,比较的次数和选择排序相同。

T e number of key comparisons in this algorithm obviously depends on the nature of the input. In the worst case, A[j]> v is executed the largest number of times, i.e., for every j = i − 1, . . . , 0. Since v = A[i], it happens if and only if A[j]> A[i] for j = i − 1, . . . , 0.Thus, in the worst case, insertion sort makes exactly the same number of comparisons as selection sort

选择排序可以参考: https://blog.csdn.net/qq_36098284/article/details/97812203

 

因此当出现这种情况的时候,时间复杂是 O(n^2)

Thus, for the worst-case input, we get A[0]>A[1] (for i = 1), A[1]> A[2] (for i = 2), . . . , A[n − 2]> A[n − 1]

                    

最好的情况是,序列是 升序,同时我们也想要升序,时间复杂度是 n - 1

In  the best case, the comparison A [j]> v is executed only once on every iteration of the outer loop. It happens if and only if A[i − 1]≤ A[i] for every

                                      

虽然平均效率是O(n^2) 使得,相对于其他的排序算法,还是比较脱出。

                                        

--------------------------------------------------------------------------------------------------------------------------

4.2 Topological Sort

更新时间2019.8.9!!!

持续更新

 

这篇关于Decrease-and-Conquer的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/393401

相关文章

Tree的Traverse and divided conquer

Tree的traverse,Preorder, Inorder, Postorder ,这些都是用stack来模拟考察的比较多。参考这里: PreOrder, InOrder, PostOrder 题型总结  这里主要总结,divide and conquer 逻辑,往上返回result的情况; Lowest Common Ancestor of a Binary Search Tree 思路

LeetCode—Divide and Conquer--53. Maximum Subarray

问题:https://leetcode.com/problems/maximum-subarray/?tab=Description Find the contiguous subarray within an array (containing at least one number) which has the largest sum.For example, given the array

The Divide-and-Conquer Paradigm分而治之范式

In general, the divide-and-conquer paradigm consists of the following steps. The divide step. The input is partitioned into 1 ≤ p ≤ n parts. The conquer step. This step consi

Divide and conquer:Moo University - Financial Aid(POJ 2010)

Moo University - Financial Aid    其实是老题了http://www.cnblogs.com/Philip-Tell-Truth/p/4926008.html   这一次我们换二分法来做这一道题,其实二分法比我以前那个方法好想一点,主要是这次我们可以根据下标进行二分,然后排两次序,第一次是根据分数来排

Halfedge 数据结构 + Delaunay三角剖分之分治法(Divide and Conquer)

*声明:才疏学浅,希望各位大神多多指教 图片来源于网络,侵删。 Halfedge数据结构 最近在搞Delaunay三角剖分算法,没啥经验,在github看到一份代码(https://github.com/eloraiby/delaunay),其中用到了Halfedge的数据结构(即是Doubly connected edge list, DCEL数据结构。https://en.wikiped

算法策略 - 分治(Divide And Conquer)

分治,也就是分而治之。它的一般步骤是 将原问题分解成若干个规模较小的子问题(子问题和原问题的结构一样,只是规模不一样)子问题又不断分解成规模更小的子问题,直到不能再分解(知道可以轻易算出子问题的解)利用子问题的解推导出原问题的解 因此,分治策略非常适合用递归 需要注意的是:子问题之间是互相独立的 分治的应用 快速排序归并排序Karatsuba算法(大数乘法) 主定力(Master

缺陷定位论文阅读:[Dongsun Kim] [TSE在投] DC: A Divide-and-Conquer Approach to IR-based Bug Localization

文章目录 前言0 阅读方案1. D&C: A Divide-and-Conquer Approach to IR-based Bug Localization1.1 基本信息1.2 文章内容1.3 几个QA1.4 感想 前言 每天都得阅读一定数量的论文。 在此阅读: 1)D&C: A Divide-and-Conquer Approach to IR-based Bug Lo

Python - 深夜数据结构与算法之 Divide Conquer Backtrack

目录 一.引言 二.分治与回溯简介 1.Divide & Conquer 分治 2.BackTrack 回溯 三.经典算法实战 1.Combination-Of-Phone [17] 2.Permutations [46] 3.Permutations-2 [47] 4.Pow-X [50] 5.N-Queen [51] 6.Combinations [78] 7.Su

2D凸包算法(五):Divide and Conquer

Divide and Conquer 图示 先对所有的点集进行分割成各个区域,对每个子区域进行凸包算法(如jarvis’s march)求得子凸包。 合并凸包,找到两条外公切线即可。从左凸包最右点、右凸包最左点开始,固定左端顺时针转、固定右端逆时针转,轮流前进直到符合凸包要求,且下一步就会破坏其规则为止。同理,可以得到另一半。 时间复杂度为 O(NlogN) 其中一次排序的时间,通常为 O(

Python高级数据结构——分治法(Divide and Conquer)

Python中的分治法(Divide and Conquer):高级算法解析 分治法是一种将问题划分为更小的子问题,解决子问题后再将结果合并的算法设计方法。它常被应用于解决复杂问题,如排序、搜索、图问题等。在本文中,我们将深入讲解Python中的分治法,包括基本概念、算法框架、具体应用场景,并使用代码示例演示分治法在实际问题中的应用。 基本概念 1. 分治法的定义 分治法将一个大问题划分为