`

Sort Part to Make Entire Array Sorted

 
阅读更多

Given an unsorted array arr[0..n-1] of size n, find the minimum length subarray arr[s..e] such that sorting this subarray makes the whole array sorted.

 

Examples:
1) If the input array is [10, 12, 20, 30, 25, 40, 32, 31, 35, 50, 60], your program should be able to find that the subarray lies between the indexes 3 and 8.

2) If the input array is [0, 1, 15, 25, 6, 7, 30, 40, 50], your program should be able to find that the subarray lies between the indexes 2 and 5.

Solution:
1) Find the candidate unsorted subarray 
a) Scan from left to right and find the first element which is greater than the next element. Let be the index of such an element. In the above example 1, is 3 (index of 30).
b) Scan from right to left and find the first element (first in right to left order) which is smaller than the next element (next in right to left order). Let be the index of such an element. In the above example 1, e is 7 (index of 31).

2) Check whether sorting the candidate unsorted subarray makes the complete array sorted or not. If not, then include more elements in the subarray.
a) Find the minimum and maximum values in arr[s..e]. Let minimum and maximum values be min and maxminand max for [30, 25, 40, 32, 31] are 25 and 40 respectively.
b) Find the first element (if there is any) in arr[0..s-1] which is greater than min, change to index of this element. There is no such element in above example 1.
c) Find the last element (if there is any) in arr[e+1..n-1] which is smaller than max, change to index of this element. In the above example 1, e is changed to 8 (index of 35)

3) Print and e.

 

Lets take an example:

int a[]={1,3,4,6,10,6,16,12,13,15,16,19,20,22,25}

Now if i will put this in to the graph (X-coordinate -> array index and Y-coordinate -> array's value) then the graph will look like as below: enter image description here

Now if we see the graph there are two places where dip happens one is after 10 and another after 16. Now in the zig zag portion if we see the min value is 6 and max val is 16. So the portion which we should sort to make the whole array sorted is between (6,16). Please refer to the below image:

enter image description here

Now we can easily divide the array in to three part. And middle part the one which we want to sort so that the whole array will be sorted.

 

Reference:

http://www.geeksforgeeks.org/minimum-length-unsorted-subarray-sorting-which-makes-the-complete-array-sorted/

http://stackoverflow.com/questions/15855594/min-n-m-so-that-whole-array-will-be-sorted/15855670

分享到:
评论

相关推荐

    tool to download entire bsp,appplication including mvc

    Tool to download entire BSP Application including MVC, Views, Page with Flow logic, Page Fragments and Global Classes ©

    Make a 2D RPG in a Weekend Apress 2015

    Make a 2D RPG in a Weekend shows you how to create your very own dungeon crawler game in RPG Maker VX Ace in a single weekend. The entire process, from start to finish, is covered within this book. ...

    The.Agile.Consultant.Guiding.Clients.to.Enterprise.Agility.epub

    Agile methods are displacing traditional, process-heavy project management techniques, and are poised to migrate from software development to the rest of IT, and to the entire enterprise. Agile’s ...

    entire frame loss.part2.rar

    整帧丢失相关论文 整帧丢失相关论文 整帧丢失相关论文 aiming at the error concealment of the entire frame loss.

    Practical Guide to Salesforce Communities pdf

    The book takes you through the entire process: from planning and designing a community to configuration/build, setup, and administration, all the way to deployment. Detailed explanations are provided...

    OpenGL SuperBible 4th Edition PDF part 1 of 2

    This book is both a comprehensive reference of the entire OpenGL API and a tutorial that will teach you how to use this powerful API to create stunning 3D visualizations, games, and other ...

    OpenGL SuperBible 4th Edition PDF part 2 of 2

    This book is both a comprehensive reference of the entire OpenGL API and a tutorial that will teach you how to use this powerful API to create stunning 3D visualizations, games, and other ...

    Silverlight 4 Unleashed(part 1)

    Printed entirely in color, with helpful figures and syntax coloring to make code samples appear as they do in Visual Studio and Expression Blend! Using Silverlight 4, you can build richer and more ...

    Silverlight 4 Unleashed(part 2)

    Printed entirely in color, with helpful figures and syntax coloring to make code samples appear as they do in Visual Studio and Expression Blend! Using Silverlight 4, you can build richer and more ...

    Learn.Mobile.Game.Development.in.One.Day.Using.Gamesalad

    Once you are familiar with Gamesalad, you will be able to prototype a game in a few days and complete the entire project in as little as a few weeks instead of the typical months or years it could ...

    SQLToolbelt part1

    Safari allows you to read entire books online, as well as quickly search the collection for relevant information. Learn more Products included in the SQL Toolbelt: * SQL Compare® Pro: compare ...

    Introduction.To.Algorithms,.Second.Edition

    have tried, however, to make this a book that will be useful to you now as a course textbook and also later in your career as a mathematical desk reference or an engineering handbook. What are the ...

    Oracle RMAN for Absolute Beginners(Apress,2014)

    Oracle RMAN for Absolute Beginners is a gentle introduction to the use of Oracle's Recovery Manager software to make backups of an Oracle database, and to restore all or part of a database in the ...

    A 32-Unit 240-GHz Heterodyne Receiver Array

    Meanwhile, the entire LO network is also locked to a 75-MHz reference, facilitating phase-coherent pairing with external sub-terahertz transmitters. A chip prototype using a bulk 65-nm CMOS ...

    Absolute Beginner's Guide to Project Management (2nd Edition)

    Absolute Beginner's Guide to Project Management will show you exactly where to start–and walk you step by step through your entire project! Expert project manager Gregory Horine shows you exactly ...

    Game Coding Complete 3rd Edition

    bringing all the code in the book together to make a little game. You‘ll also see some of my best debugging tricks and an entire chapter on how it feels to be there when you release a commercial game...

    Site.Reliability.Engineering.2016.3.pdf

    In this collection of essays and articles, key members of Google’s Site Reliability Team explain how and why their commitment to the entire lifecycle has enabled the company to successfully build, ...

    Site Reliability Engineering

    In this collection of essays and articles, key members of Google’s Site Reliability Team explain how and why their commitment to the entire lifecycle has enabled the company to successfully build, ...

    How to be a Programmer

    But it is really child's play compared to everything else that a good programmer must do to make a software system that succeeds for both the customer and myriad colleagues for whom he or she is ...

    The Executive’s How-To Guide to Automation

    The Executive’s How-To Guide to Automation: Mastering AI and Algorithm-Driven Business By 作者: George E. Danner ISBN-10 书号: 3319997882 ISBN-13 书号: 9783319997889 Edition 版本: 1st ed. 2019 出版...

Global site tag (gtag.js) - Google Analytics