Groupon面经Prepare: Sort given a range && Summary: Bucket Sort
首先是如何sort一个只有0和1的数组,要求inplace. follow up是告诉一个range,如何在O(N)时间内sort好
两个pointer可解
package Sorting;
import java.util.*; public class InplaceSorting {
public void sorting(int[] arr) {
if (arr==null || arr.length==0) return;
int l=0, r=arr.length-1;
while (l < r) {
while (l<r && arr[l]==0) {
l++;
}
while (l<r && arr[r]==1) {
r--;
}
if (l == r) return;
swap(arr, l, r);
}
} public void swap(int[] arr, int l, int r) {
int temp = arr[l];
arr[l] = arr[r];
arr[r] = temp;
} /**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
InplaceSorting sol = new InplaceSorting();
int[] arr = new int[]{0,1,1,0,1,0,0,1};
sol.sorting(arr);
System.out.println(Arrays.toString(arr));
} }
BucketSort可解
Best case performance: O(n + k), where k is the size of buckets or the range between min and max in original array
Worst case performance: O(n^2)
Aver case performance: O(n + k)
Worst case space: O(n*k)
BucketSort works as follows:
1. set up an array of initially empty buckets(should know the range)
2. Go over the original array, put each object in its bucket
3. Sort each non-empty bucket.
4. visit the buckets in order and put all elements back into the original array.
package Sorting;
import java.util.Arrays;
public class Solution {
public void bucketSort(int[] arr, int min, int max) {
int[] bucket = new int[max-min+1];
for (int elem : arr) {
bucket[elem-min]++;
}
int cur = 0;
for (int i=0; i<bucket.length; i++) {
while (bucket[i] > 0) {
arr[cur++] = i+min;
bucket[i]--;
}
}
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Solution sol = new Solution();
int[] arr = new int[]{5,6,9,10,4,11,5,7,6,11};
sol.bucketSort(arr, 4, 11);
System.out.println(Arrays.toString(arr));
}
}
Groupon面经Prepare: Sort given a range && Summary: Bucket Sort的更多相关文章
- Bucket Sort
(referrence: GeekforGeeks) Bucket sort is mainly useful when input is uniformly distributed over a r ...
- Bucket Sort - leetcode [桶排序]
桶排序(Bucket sort)或所谓的箱排序,是一个排序算法,工作的原理是将数组分到有限数量的桶里.每个桶再个别排序(有可能再使用别的排序算法或是以递归方式继续使用桶排序进行排序).桶排序是鸽巢排序 ...
- 计数排序与桶排序(bucket sort)
Bucket Sort is a sorting method that subdivides the given data into various buckets depending on cer ...
- 桶排序(bucket sort)
Bucket Sort is a sorting method that subdivides the given data into various buckets depending on cer ...
- 桶排序bucket sort
桶排序 (Bucket sort)或所谓的箱排序的原理是将数组分到有限数量的桶子里,然后对每个桶子再分别排序(有可能再使用别的排序算法或是以递归方式继续使用桶排序进行排序),最后将各个桶中的数据有序的 ...
- SDUT OJ 数据结构实验之排序三:bucket sort
数据结构实验之排序三:bucket sort Time Limit: 250 ms Memory Limit: 65536 KiB Submit Statistic Discuss Problem D ...
- SDUT 3400 数据结构实验之排序三:bucket sort
数据结构实验之排序三:bucket sort Time Limit: 150MS Memory Limit: 65536KB Submit Statistic Problem Description ...
- Algorithms - Bucket sort
印象 图1 将元素分布在桶中 图2 元素在每个桶中排序 思想 桶排序将数组分到有限数量的桶子里.每个桶子再个别排序(有可能再使用别的排序算法或是以递归方式继续使用桶排序进行排序). 分析 时间复杂度: ...
- SDUT-3400_数据结构实验之排序三:bucket sort
数据结构实验之排序三:bucket sort Time Limit: 250 ms Memory Limit: 65536 KiB Problem Description 根据人口普查结果,知道目前淄 ...
随机推荐
- hitTest:WithEvent 和Responder Chain
这个方法是找到那个View被touch,当找到后就成为响应链的第一个了,如果他不能处理这个Event,那么就找nextResponder 直至application 如果不能处理,那就会丢弃掉. ht ...
- 关于java字符串编译优化问题
情景一:不好的字符串拼接习惯 起因是这样的:一个大牛在写了一篇关于java字符串优化问题的讲解,他提到:不要使用strObj+otherValue的方法将otherValue转换为字符串形式,因 ...
- python 之 utf-8编码的秘密
python3的默认编码方案是utf-8编码,看了些资料,来做总结. 要说utf-8,就要说说unicode,要说unicode,就要说ASCII,我们还是慢慢来. 1.ASCII ASCII编码最初 ...
- android Listview item 中有button,item就不响应触摸事件
为button设置 beanButton.getButton().setFocusable(false); beanButton.getButton().setFocusableInTouchMode ...
- ligerui多选动态下拉框
今天下午要求做一个支持多选的,并且插件用ligerui的,当时有点小懵了,因为没用过ligerui啊!而且按照API的介绍,我做得也很好啊,可是为什么就是显示不出来?据说有位小神比较厉害,请教来之,两 ...
- JavaScript学习之窗口
窗口 一.Window 对象 Window 对象表示浏览器中打开的窗口.如果文档包含框架(frame 或 iframe 标签),浏览器会为 HTML 文档创建一个 window 对象,并为每个框架创建 ...
- boost.compressed_pair源码剖析
意义 当compressed_pair的某一个模板参数为一个空类的时候将对其进行“空基类优化”,这样可以使得compressed_pair占用的空间比std::pair的更小. 参考如下代码: #in ...
- How to read the HTML DTD
Contents How to read the HTML DTD 1. DTD Comments 2. Parameter Entity definitions 3. Element declara ...
- MSYS2 安装和配置
MSYS2 安装和配置 msys2 和 cygwin 类似,提供了一个类 Linux 的 shell 环境和工具链,同时还使用了 arch linux 的 pacman 管理软件包,比 cygwin ...
- 指向函数的指针与iOS-Block相关知识
指向函数的指针与iOS-Block相关知识 一. 函数指针的定义和调用: 关于函数指针的知识详细可参考:http://www.cnblogs.com/mjios/archive/2013/03/19/ ...