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. ASP常用函数表

    ASP常用函数表(新手们的好工具) 作者:未知 Array() 函数返回一个数组 表达式 Array(list) 允许数据类型: 字符,数字均可 实例: <% Dim myArray() For ...

  2. DB2查看表空间大小及使用情况

    1.SQL 查询(选择Schema为SYSPROC) SELECT SUBSTR(TABLESPACE_NAME,,) AS TBSPC_NAME, AS "TOTAL(MB)", ...

  3. swift闭包传值

    不知道原理,就知道这么用的,皮毛上的那一点. 寻思着把以前的项目改成swift的,结果了,,, 反向传值 一. //类似于OC中的typedef typealias sendValueClosure= ...

  4. C#对象XML序列化

    1.Xml序列化操作类 .Net Framework提供了对应的System.Xml.Seriazliation.XmlSerializer负责把对象序列化到XML,和从XML中反序列化为对象. 以下 ...

  5. java匿名内部类练习

    interface Inter { void method(); } class Test { //补足代码.通过匿名内部类. /* static class Inner implements Int ...

  6. Excel常用函数

    1.基本的算数函数 sum() average() 2.三角函数 sin() cos() 3.

  7. CAF(C++ actor framework)(序列化之复杂类,分析 还有自己不懂的细思恐极函数实现)(三)

    这里应该是序列化的最后一篇.感觉自己写的不是很好,也一点点在学习.这次就不贴上代码了.代码在github上的announce5.cpp.代码简单,但是分析下去会有细思恐极的感觉! 先看一下几个函数是干 ...

  8. windows phone URI映射

    UriMapping用于在一个较短的URI和你项目中的xaml页的完整路径定义一个映射(别名).通过使用别名URI,开发者可以在不改变导航代码的情况下来改变一个项目的内部结构.该机制还提供了一个简单的 ...

  9. TreeView递归绑定数据的两种方法

    #region 绑定TreeView /// <summary> /// 绑定TreeView(利用TreeNode) /// </summary> /// <param ...

  10. jquery返回顶部特效

    <style> p#back-to-top{position:fixed; bottom:100px;right:10px; display: none; } p#back-to-top ...