Partition an array of integers around a value such taht all elements less than x come before elements greater than or equal to x.

Idea: Just use of subroutine PARTION of quicksort() with a slight modification.

array[start, end] to be partitioned around value x.

We need to additional pointers i and j, to maintain an invariant, which is true at all times:

  array[start, i] stores elements less than x

  array[i+1, j-1] stores elements greater than or equal to x

  array[j, end] stores elements which haven't been explored yet.

void Partition(A, start, end){
int i = start - 1;
for(int j = start; j <= end; ++j){
if(A[j] < x){
i++;
swap A[i] with A[j];
}
}
}

Partition an array around an interger的更多相关文章

  1. Lintcode: Partition Array

    Given an array "nums" of integers and an int "k", Partition the array (i.e move ...

  2. Partition Array

    Given an array nums of integers and an int k, partition the array (i.e move the elements in "nu ...

  3. [Swift]LeetCode1013. 将数组分成和相等的三个部分 | Partition Array Into Three Parts With Equal Sum

    Given an array A of integers, return true if and only if we can partition the array into three non-e ...

  4. LeetCode 1013 Partition Array Into Three Parts With Equal Sum 解题报告

    题目要求 Given an array A of integers, return true if and only if we can partition the array into three  ...

  5. Partition Array Into Three Parts With Equal Sum LT1013

    Given an array A of integers, return true if and only if we can partition the array into three non-e ...

  6. LeetCode 1043. Partition Array for Maximum Sum

    原题链接在这里:https://leetcode.com/problems/partition-array-for-maximum-sum/ 题目: Given an integer array A, ...

  7. 【leetcode】1043. Partition Array for Maximum Sum

    题目如下: Given an integer array A, you partition the array into (contiguous) subarrays of length at mos ...

  8. 【leetcode】1020. Partition Array Into Three Parts With Equal Sum

    题目如下: Given an array A of integers, return true if and only if we can partition the array into three ...

  9. 【LeetCode】1020. Partition Array Into Three Parts With Equal Sum 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

随机推荐

  1. ActiveMQ(5.10.0) - Building a custom security plug-in

    If none of any built-in security mechanisms works for you, you can always build your own. Though the ...

  2. MongoDB - Introduction to MongoDB, BSON Types

    BSON is a binary serialization format used to store documents and make remote procedure calls in Mon ...

  3. SQL<>0查询不到NUll的值

    这几天遇到这样一个问题,sql中写<>0,刚好某个记录是NULL,道理上是满足<>0的啊,可是就是抽不出来,关于这个问题,到处找了点资料,算是这里 写一个总结出来. 用java ...

  4. Ajax B/S 聊天工具txt文件保存

    打算做一个两个或多个网页之间交流的功能,思路是多个页面聊天的内容存放到一个文件里,每个页面都有提交聊天功能,当提交聊天信息时保存到上面那个文件里, 在每个也页面里放一个定时器,每秒钟获取聊天文件里的记 ...

  5. 和阿文一起学H5--设计稿尺寸全攻略

  6. C#中 多线程执行含有返回值的函数

    C# 中,传统的多线程并不支持多线程执行含有返回结果的函数.虽然可以通过制作外壳类来使得返回结果得以保留,但如果一定时间内函数未执行完,简单的外壳类可能就无法满足需求了. class netHelpe ...

  7. Easyui 加载树(easyui-tree)[dotnet]

    前台 html: <ul class="easyui-tree" id="ul_Tree" data-options="fit:true,ani ...

  8. CodeSmith Template Model Output

    背景:上学那会儿就接触CodeSmith,是一款非常优秀的代码自动生成工具.以前写过好些基本模版,可惜早不知道扔到哪儿去了,如今只能重新开始,把它捡回来,加油. 效果:将数据库 DataBase 应用 ...

  9. WebGIS基础复习笔记

    明天要考试了,突击一下. 1.万维网:www是world wide web的简称是在超文本基础上形成的信息网 2.互联网:即广域局域网及单机按照一定的通讯协议组成的国际计算机网络 3.WebGIS:网 ...

  10. 3月3日(3) Binary Tree Preorder Traversal

    原题 Binary Tree Preorder Traversal 没什么好说的... 二叉树的前序遍历,当然如果我一样忘记了什么是前序遍历的..  啊啊.. 总之,前序.中序.后序,是按照根的位置来 ...