1. 首先是如何sort一个只有01的数组,要求inplace. follow up是告诉一个range,如何在O(N)时间内sort

两个pointer可解

  1. package Sorting;
  2. import java.util.*;
  3.  
  4. public class InplaceSorting {
  5. public void sorting(int[] arr) {
  6. if (arr==null || arr.length==0) return;
  7. int l=0, r=arr.length-1;
  8. while (l < r) {
  9. while (l<r && arr[l]==0) {
  10. l++;
  11. }
  12. while (l<r && arr[r]==1) {
  13. r--;
  14. }
  15. if (l == r) return;
  16. swap(arr, l, r);
  17. }
  18. }
  19.  
  20. public void swap(int[] arr, int l, int r) {
  21. int temp = arr[l];
  22. arr[l] = arr[r];
  23. arr[r] = temp;
  24. }
  25.  
  26. /**
  27. * @param args
  28. */
  29. public static void main(String[] args) {
  30. // TODO Auto-generated method stub
  31. InplaceSorting sol = new InplaceSorting();
  32. int[] arr = new int[]{0,1,1,0,1,0,0,1};
  33. sol.sorting(arr);
  34. System.out.println(Arrays.toString(arr));
  35. }
  36.  
  37. }

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.

  1. package Sorting;
  2.  
  3. import java.util.Arrays;
  4.  
  5. public class Solution {
  6. public void bucketSort(int[] arr, int min, int max) {
  7. int[] bucket = new int[max-min+1];
  8. for (int elem : arr) {
  9. bucket[elem-min]++;
  10. }
  11. int cur = 0;
  12. for (int i=0; i<bucket.length; i++) {
  13. while (bucket[i] > 0) {
  14. arr[cur++] = i+min;
  15. bucket[i]--;
  16. }
  17. }
  18. }
  19.  
  20. /**
  21. * @param args
  22. */
  23. public static void main(String[] args) {
  24. // TODO Auto-generated method stub
  25. Solution sol = new Solution();
  26. int[] arr = new int[]{5,6,9,10,4,11,5,7,6,11};
  27. sol.bucketSort(arr, 4, 11);
  28. System.out.println(Arrays.toString(arr));
  29. }
  30.  
  31. }

Groupon面经Prepare: Sort given a range && Summary: Bucket Sort的更多相关文章

  1. Bucket Sort

    (referrence: GeekforGeeks) Bucket sort is mainly useful when input is uniformly distributed over a r ...

  2. Bucket Sort - leetcode [桶排序]

    桶排序(Bucket sort)或所谓的箱排序,是一个排序算法,工作的原理是将数组分到有限数量的桶里.每个桶再个别排序(有可能再使用别的排序算法或是以递归方式继续使用桶排序进行排序).桶排序是鸽巢排序 ...

  3. 计数排序与桶排序(bucket sort)

    Bucket Sort is a sorting method that subdivides the given data into various buckets depending on cer ...

  4. 桶排序(bucket sort)

    Bucket Sort is a sorting method that subdivides the given data into various buckets depending on cer ...

  5. 桶排序bucket sort

    桶排序 (Bucket sort)或所谓的箱排序的原理是将数组分到有限数量的桶子里,然后对每个桶子再分别排序(有可能再使用别的排序算法或是以递归方式继续使用桶排序进行排序),最后将各个桶中的数据有序的 ...

  6. SDUT OJ 数据结构实验之排序三:bucket sort

    数据结构实验之排序三:bucket sort Time Limit: 250 ms Memory Limit: 65536 KiB Submit Statistic Discuss Problem D ...

  7. SDUT 3400 数据结构实验之排序三:bucket sort

    数据结构实验之排序三:bucket sort Time Limit: 150MS Memory Limit: 65536KB Submit Statistic Problem Description ...

  8. Algorithms - Bucket sort

    印象 图1 将元素分布在桶中 图2 元素在每个桶中排序 思想 桶排序将数组分到有限数量的桶子里.每个桶子再个别排序(有可能再使用别的排序算法或是以递归方式继续使用桶排序进行排序). 分析 时间复杂度: ...

  9. SDUT-3400_数据结构实验之排序三:bucket sort

    数据结构实验之排序三:bucket sort Time Limit: 250 ms Memory Limit: 65536 KiB Problem Description 根据人口普查结果,知道目前淄 ...

随机推荐

  1. GenderGuesser

    http://www.hackerfactor.com/GenderGuesser.php#Analyze

  2. Java - HttpURLConnection

    JDK中的URLConnection参数详解 1:> URL请求的类别: 分为二类,GET与POST请求.二者的区别在于: a:) get请求可以获取静态页面,也可以把参数放在URL字串后面,传 ...

  3. vbox共享文件 挂载

    环境:主机操作系统是Windows 7,虚拟机是open suse 12.0,虚拟机是VirtualBox 4.2.1. 1. 安装增强功能包(Guest Additions) 安装好open sus ...

  4. Qt中sleep()的实现(耳目一新的两种方法)

    在Qt中并没有Sleep函数可以调用,在程序编写时往往需要休眠几秒,这里举出两个方法,不知道是否啥不良隐患没~~ 方法一: class SleeperThread : public QThread{p ...

  5. C++用数组实现的静态队列

    #ifndef _STATIC_QUEUE_H_ #define _STATIC_QUEUE_H_ // 静态queue模板,用数组实现的队列,在初始化的时候需要指定长度 template<cl ...

  6. 设计模式:单例模式(Singleton)

    定义:确保一个类仅有一个实例,并提供一个访问它的全局访问点. 优点:在内存中只有一个对象,节省了内存空间 示例: Singleton.cs 写法一:非线程安全 public class Singlet ...

  7. pointer-events:none;穿透属性

    从属性字面上看可以理解为:手势时间无效. 当我们在ios下想复制一段文字是,不知道原因导致一些莫名的怪异现象:总是无法复制文字,却意外的发现无论怎么着复制的始终都是图片时, 当我们在一个半透明遮罩上想 ...

  8. Magento - Rewrite机制一窥

    看一个url例子 http://localhost/magento/index.php/customer/account/login 这里假定http://localhost/magento/ 是ma ...

  9. Jquery下拉效果

    $('#触发元素').hover(function(){ $('#框框').slideDown(); //展开(动画效果)},function(){ $('#框框').slideUp(); //收起( ...

  10. magento去掉add to cmpre和email to friend

    修改:\app\design\frontend\default\blanco\template\catalog\product下list.phtml 和app\design\frontend\defa ...