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 根据人口普查结果,知道目前淄 ...
随机推荐
- GenderGuesser
http://www.hackerfactor.com/GenderGuesser.php#Analyze
- Java - HttpURLConnection
JDK中的URLConnection参数详解 1:> URL请求的类别: 分为二类,GET与POST请求.二者的区别在于: a:) get请求可以获取静态页面,也可以把参数放在URL字串后面,传 ...
- vbox共享文件 挂载
环境:主机操作系统是Windows 7,虚拟机是open suse 12.0,虚拟机是VirtualBox 4.2.1. 1. 安装增强功能包(Guest Additions) 安装好open sus ...
- Qt中sleep()的实现(耳目一新的两种方法)
在Qt中并没有Sleep函数可以调用,在程序编写时往往需要休眠几秒,这里举出两个方法,不知道是否啥不良隐患没~~ 方法一: class SleeperThread : public QThread{p ...
- C++用数组实现的静态队列
#ifndef _STATIC_QUEUE_H_ #define _STATIC_QUEUE_H_ // 静态queue模板,用数组实现的队列,在初始化的时候需要指定长度 template<cl ...
- 设计模式:单例模式(Singleton)
定义:确保一个类仅有一个实例,并提供一个访问它的全局访问点. 优点:在内存中只有一个对象,节省了内存空间 示例: Singleton.cs 写法一:非线程安全 public class Singlet ...
- pointer-events:none;穿透属性
从属性字面上看可以理解为:手势时间无效. 当我们在ios下想复制一段文字是,不知道原因导致一些莫名的怪异现象:总是无法复制文字,却意外的发现无论怎么着复制的始终都是图片时, 当我们在一个半透明遮罩上想 ...
- Magento - Rewrite机制一窥
看一个url例子 http://localhost/magento/index.php/customer/account/login 这里假定http://localhost/magento/ 是ma ...
- Jquery下拉效果
$('#触发元素').hover(function(){ $('#框框').slideDown(); //展开(动画效果)},function(){ $('#框框').slideUp(); //收起( ...
- magento去掉add to cmpre和email to friend
修改:\app\design\frontend\default\blanco\template\catalog\product下list.phtml 和app\design\frontend\defa ...