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 根据人口普查结果,知道目前淄 ...
随机推荐
- 专家来了-提测-改bug-上线10号
集成那天,同事帮忙改了三个bug, 适配ios6约束,方法被调用两次, 郑晓杨吃饭,好像还欠我钱呢 Product-archive 打包 ------------------------------ ...
- Anti-pattern
https://en.wikipedia.org/wiki/Anti-pattern https://zh.wikipedia.org/wiki/反面模式 An anti-pattern is a c ...
- Redis学习笔记(1)-Key
package cn.com; import java.text.ParseException; import java.util.List; import java.util.Set; import ...
- 自己diy一个jquery分页插件
js基础学习过程中,期间经历换工作的各种面试,很多面试官问过:有没有写过jquery插件?等类似问题. 就个人而言,关于jquery插件的文章确实看过不少,但是一直没有动手写一个,一是不想在目前学习j ...
- Aptana Studio 3的汉化
Aptana Studio 3(下面简称Aptana 3)的汉化方法 1.找到这个网站 http://aptana.com/support 2.单击下面的链接 view documentation 在 ...
- Java学习-024-获取当前类名或方法名二三文
今天,看朋友编写程序,打印日志时,需要记录当前类的类名以及当前方法的方法名,我发现 TA 将类名或者方法名直接写死在了代码中...虽说这样可以实现记录类名和方法名,但是当有特殊情况需要修改类名或者方法 ...
- OceanBase架构浅析(二)
单点性能 OceanBase架构的优势在于既支持跨行跨表事务,又支持存储服务器线性扩展.当然,这个架构也有一个明显的缺陷:UpdateServer单点,这个问题限制了OceanBase集群的整体读写性 ...
- SQL Server Logical/Physical Reads
Summary Info: Logical Reads : Reading Data pages from CachePhysical Reads : Reading Data pages ...
- iOS:访问地址薄
地址簿的访问 介绍: 地址簿(Address Book)是一个共享的联系人信息数据库.任何iOS应用程序都可以使用.通过提供常用联系人信息,而不是让每一个应用程序管理独立的联系人列表,可改善用户体验. ...
- 微信小店开发(2) DIY货架
微信小店的货架支持开放给开发者使用,即开发者可以将自己的页面作为货架,通过JavaScript API来调起微信客户端原生的商品详情页. 请注意:1. 开发者需要预先通过1.1中的增加商品API,预先 ...