Partition an array around an interger
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的更多相关文章
- Lintcode: Partition Array
Given an array "nums" of integers and an int "k", Partition the array (i.e move ...
- Partition Array
Given an array nums of integers and an int k, partition the array (i.e move the elements in "nu ...
- [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 ...
- 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 ...
- 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 ...
- LeetCode 1043. Partition Array for Maximum Sum
原题链接在这里:https://leetcode.com/problems/partition-array-for-maximum-sum/ 题目: Given an integer array A, ...
- 【leetcode】1043. Partition Array for Maximum Sum
题目如下: Given an integer array A, you partition the array into (contiguous) subarrays of length at mos ...
- 【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 ...
- 【LeetCode】1020. Partition Array Into Three Parts With Equal Sum 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
随机推荐
- ActiveMQ(5.10.0) - Configuring the Simple Authentication Plug-in
The easiest way to secure the broker is through the use of authentication credentials placed directl ...
- C#算法基础之插入排序
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- Objective-C中一个方法如何传递多个参数的理解
原来如此 Objective-C语法中多参数传递方法经常是初学者最容易犯困的地方.我自己也是刚刚悟出来与大家分享. 分析 由于我们已有的语言经验告诉我们定义方法都是: 一个类型匹配一个参数(动态语言甚 ...
- 相对完美的后台Service实现播放音乐功能
对于用Context.startService()启动的service生命周期为onCreate()-onStartCommand()-onDestroy();如果多次用context.startSe ...
- Android Wi-Fi基本操作
从用户角度看,Android Wi-Fi模块自下向上可以看为5层:硬件驱动程序,wpa_suppplicant,JNI,WiFi API,WifiSettings应用程序. 1.wpa_supplic ...
- 如何查看Windows8.1计算机体验指数评分
如果你已经安装使用了Windows 8.1,你就会发现自从Vista时代开始的计算机体验评分消失了,在文章<微软取消Windows 8 计算机评分功能>中,我猜测了微软取消评分功能的可能原 ...
- UIView-4-EventForViews(在view上加入button时候的事件处理)
#import "ViewController.h" @interface ViewController () @end @implementation ViewControlle ...
- OC3_歌词解析
// // LrcManager.h // OC3_歌词解析 // // Created by zhangxueming on 15/6/15. // Copyright (c) 2015年 zhan ...
- Apache 安装与配置
安装Apache 1.下载apache软件http://www.apache.org 2.安装 以管理员身份进行安装 3.设置主机名与域名 4.选择安装类型 5.自定义安装路径 6.开始安装 7.安装 ...
- LXC-Linux Containers介绍
Linux Containers,Linux的容器,容器嘛,可以想象成一个大的装东西的罐子,罐子口很大,里面可以装很多同样形状,只不过大小不同的小罐子.专业的话,叫做基于容器的操作系统层面的虚拟化技术 ...