BucketSort Implementation

Imagine you have an array like this (note that the array has duplicates, and includes 0 and k):

a = [ 4, 64, 200, 42, 56, 22, 1, 64, 0, 161, 200, 0, 42 ]

What you know about this array is

Values are integers

Values are non-negative and less than some number, for example:

0 <= value <= k, where k is an integer

To start, can you please write some code to sort this array using any algorithm you want and not using the library functions. Let’s assume there are no space/time requirements. This part of the coding interview is not about the algorithms, our goal is to write some code and get it running.

 1 public void bucketSort(int[] arr) {
 2         if (arr == null || arr.length < 1) return;
 3         Map<Integer, Integer> itemToCountMapping = new HashMap<>();
 4         int min = arr[0];
 5         int max = arr[0];
 6
 7         for (int value : arr) {
 8             min = Math.min(min, value);
 9             max = Math.max(max, value);
10             itemToCountMapping.put(value, itemToCountMapping.getOrDefault(value, 0) + 1);
11         }
12
13         int index = 0;
14         for (int i = min; i <= max; i++) {
15             int count = itemToCountMapping.getOrDefault(i, 0);
16             for (int j = 1; j <= count; j++) {
17                 arr[index] = i;
18                 index++;
19             }
20         }
21     }

原文地址:https://www.cnblogs.com/beiyeqingteng/p/12324686.html

时间: 2024-10-11 07:13:01

BucketSort Implementation的相关文章

Python integer objects implementation

http://www.laurentluce.com/posts/python-integer-objects-implementation/ Python integer objects implementation May 15, 2011 This article describes how integer objects are managed by Python internally. An integer object in Python is represented interna

An Implementation of Double-Array Trie

Contents What is Trie? What Does It Take to Implement a Trie? Tripple-Array Trie Double-Array Trie Suffix Compression Key Insertion Key Deletion Double-Array Pool Allocation An Implementation Download Other Implementations References What is Trie? Tr

Python string objects implementation

http://www.laurentluce.com/posts/python-string-objects-implementation/ Python string objects implementation June 19, 2011 This article describes how string objects are managed by Python internally and how string search is done. PyStringObject structu

Python dictionary implementation

Python dictionary implementation http://www.laurentluce.com/posts/python-dictionary-implementation/ August 29, 2011 This post describes how dictionaries are implemented in the Python language. Dictionaries are indexed by keys and they can be seen as

Kernel logging: APIs and implementation

Kernel API Logging within the kernel is performed using the printk function int printk( const char * fmt, ... ); The kernel code simply defines the log level as the first argument of the message, as illustrated in the following example for a critical

Objective C Protocol implementation

protocol 类似于接口,可以实现函数的回调 @protocol MyDelegate<NSObject> -(void)myCallbackFunction; @end //Caller 添加一个delegate 的property: //.h文件 @property (nonatomic,weak) id <MyDelegate> mDelegate; //caller的函数中就可以调用 //.m文件 @synthesize mDelegate; [self.mDelega

未能正确加载“Microsoft.VisualStudio.Editor.Implementation.EditorPackage”

未能正确加载“Microsoft.VisualStudio.Editor.Implementation.EditorPackage” VS2012启动/加载项目出问题 未能正确加载“Microsoft.VisualStudio.Editor.Implementation.EditorPackage, Microsoft.VisualStudio.Editor.Implementation, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03

未能正确加载“Microsoft.VisualStudio.Editor.Implementation.EditorPackage”包 - - 博客频道 - CSDN.NET

原文:http://www.cnblogs.com/autumn/p/3452369.html --------------------------- Microsoft Visual Studio --------------------------- 未能正确加载"Microsoft.VisualStudio.Editor.Implementation.EditorPackage"包. 此问题可能是由配置更改或安装另一个扩展导致的.可以通过查看文件"C:\Users\用户

Notes on the Asynchronous I/O implementation

Fixed Thread Pool An asynchronous channel group associated with a fixed thread pool of size N, submits N tasks that wait on I/O or completion events from the kernel. Each task simply dequeues an event, does any necessary I/O completion, and then disp