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. Redis - HyperLogLogs

    A HyperLogLog is a probabilistic data structure used in order to count unique things (technically th ...

  2. spring+hibernate管理多个数据源(非分布式事务)

    本文通过一个demo,介绍如何使用spring+hibernate管理多个数据源,注意,本文的事务管理并非之前博文介绍的分布式事务. 这个demo将使用两个事务管理器分别管理两个数据源.对于每一个独立 ...

  3. fresco的源码学习自我总结

    前言 对fresco框架源码的阅读学习,学习优秀的编码方式和较为实用常见设计模式,该篇讲得比较浅,主要是理清三个主要类的之间的关系. 本篇目录 fresco框架的MVC模式 fresco的Drawee ...

  4. Android之帧动画

    MySurfaceView类: package com.fm; import android.content.Context; import android.graphics.Bitmap; impo ...

  5. IUSR和IIS_IUSRS

    在早期的IIS版本中,随着IIS的安装,系统会创建一个IUSR_MachineName用户.IIS启用匿名访问,就是通过此用户进行身份认证的,包括FTP匿名访问,HTTP匿名访问.   在创建IUSR ...

  6. Cocos移植到Android-通过命令行交叉编译打包

    当我们在Windows下使用Visual Studio开发好Win32平台的游戏程序后,我们需要把它们移植到不同的平台上.Cocos2d-x支持很多个平台,然而,我们不可能介绍全部平台的移植.我们总结 ...

  7. c/c++读取文件

    #include <iostream> #include <string> #include <fstream> int main() { std::ifstrea ...

  8. L005-oldboy-mysql-dba-lesson05

    L005-oldboy-mysql-dba-lesson05 在线改表工具:pt-onine-schema-change 来自为知笔记(Wiz)

  9. IDEA 2016.2.2激活地址(2016-08-22)

    http://idea.imsxm.com/ 2016/10/13  http://idea.iteblog.com/key.php

  10. Bind安装配置及应用

    Bind安装配置及应用 BIND:Berkeley Internet Name Domain ,ISC.org     DNS服务的实现:     监听端口:53/UDP , 53/TCP     程 ...