radix-sort

Radix sort vs Counting sort vs Bucket sort. What's the difference?

北城余情 提交于 2019-12-02 14:02:58
I am reading the definitions of radix, counting and bucket sorts and it seems that all of them are just the code below: public static void sort(int[] a, int maxVal){ int [] bucket=new int[maxVal+1]; for (int i=0; i<bucket.length; i++){ bucket[i]=0; } for (int i=0; i<a.length; i++){ bucket[a[i]]++; } int outPos=0; for (int i=0; i<bucket.length; i++){ for (int j=0; j<bucket[i]; j++){ a[outPos++]=i; } } } I know I can't be right, so what am I missing? Show the code if you think that can help explaining in Java or C. Konstantin Vladimirov Let's start with some rewriting your code in C, because C

Radix Sort Algorithm

蓝咒 提交于 2019-12-01 09:18:49
I have been given some algorithms to reverse engineer. The algorithm below is a radix sort, but I am very confused about what is actually happening in the code. I'm new to algorithms and am unsure how the code sorts elements in an array. I'm not sure what bits have to do with the algorithm and what a mask is. Here is the code: ArrayList<Integer> array = CopyArray(a); Integer[] zerobucket = new Integer[a.size()]; Integer[] onebucket = new Integer[a.size()]; int i, bit; Integer element, mask; for (bit=0; bit<8; ++bit) { int zc = 0; int oc = 0; for(i=0; i<array.size(); ++i) { element = array.get

Radix Sort Algorithm

隐身守侯 提交于 2019-12-01 06:20:45
问题 I have been given some algorithms to reverse engineer. The algorithm below is a radix sort, but I am very confused about what is actually happening in the code. I'm new to algorithms and am unsure how the code sorts elements in an array. I'm not sure what bits have to do with the algorithm and what a mask is. Here is the code: ArrayList<Integer> array = CopyArray(a); Integer[] zerobucket = new Integer[a.size()]; Integer[] onebucket = new Integer[a.size()]; int i, bit; Integer element, mask;

Radix Sort on an Array of Strings?

折月煮酒 提交于 2019-12-01 00:34:43
I've been researching around, and while I've figured out the general idea of using Radix Sort to alphabetize an array of strings, I know I'm going the wrong direction. This is what I have so far: void radixSort(string* sortMe, int l) { queue<string>* sections = new queue<string>[27]; //Have a-z, and also one for strings that are null terminated. for(int i = 0; i < numElements; i++) { if(!(sortMe[i][l] == 32)) sections[sortMe[i][l]-96].push(sortMe[i]); //-96 because the ascii code for a is 97. If, for example a is the character, it will be placed at 1. 0 is left for null characters } for(int i

Radix Sort on an Array of Strings?

☆樱花仙子☆ 提交于 2019-11-30 20:39:01
问题 I've been researching around, and while I've figured out the general idea of using Radix Sort to alphabetize an array of strings, I know I'm going the wrong direction. This is what I have so far: void radixSort(string* sortMe, int l) { queue<string>* sections = new queue<string>[27]; //Have a-z, and also one for strings that are null terminated. for(int i = 0; i < numElements; i++) { if(!(sortMe[i][l] == 32)) sections[sortMe[i][l]-96].push(sortMe[i]); //-96 because the ascii code for a is 97.

Radix Sort Optimization

拟墨画扇 提交于 2019-11-29 08:35:58
I was trying to optimize the Radix Sort code, because I felt there was room for it as traditional codes in books and on web seem a direct copy of one another and also they work very slow as they take an arbitrary number such as 10 for modulo operation. I have optimized the code as far as I could go, maybe I might have missed some optimization techniques. In that case please enlighten me. Motivation for optimization: http://codercorner.com/RadixSortRevisited.htm http://stereopsis.com/radix.html I was unable to implement all the optimizations in the articles, mostly it was beyond my skills and

most significant v.s. least significant radix sort

筅森魡賤 提交于 2019-11-29 02:39:53
If I just need to sort strings composed by ASCII characters, wondering what are the differences between using most significant v.s. least significant radix sorting? I think they should have the same results, but confused by the following statement from below link, and if anyone could help to clarify, it will be great. https://en.wikipedia.org/wiki/Radix_sort A most significant digit (MSD) radix sort can be used to sort keys in lexicographic order. Unlike a least significant digit (LSD) radix sort, a most significant digit radix sort does not necessarily preserve the original order of duplicate

An array of length N can contain values 1,2,3 … N^2. Is it possible to sort in O(n) time?

旧城冷巷雨未停 提交于 2019-11-28 09:27:30
Given an array of length N. It can contain values from ranging from 1 to N^2 (N squared) both inclusive, values are integral. Is it possible to sort this array in O(N) time? If possible how? Edit: This is not a homework. Write each integer in base N, that is each x can be represented as (x1, x2) with x = 1 + x1 + x2*N. Now you can sort it twice with counting sort, once on x1 and once on x2, resulting in the sorted array. Yes, you can, using radix sort with N buckets and two passes. Basically, you treat the numbers as having 2 digits in base N . It is possible to sort any array of integers with

Radix Sort Base 16 (Hexadecimals)

前提是你 提交于 2019-11-28 09:22:54
问题 I have spent more 10hr+ on trying to sort the following(hexadecimals) in LSD radix sort, but no avail. There is very little material on this subject on web. 0 4c7f cd80 41fc 782c 8b74 7eb1 9a03 aa01 73f1 I know I have to mask and perform bitwise operations to process each hex digit (4 bits), but have no idea on how and where. I'm using the code (I understand) from GeeksforGeeks void rsort(int a[], int n) { int max = getMax(a, n); for (int exp = 1; max / exp > 0; exp *= 10) { ccsort(a, n, exp)

Radix Sort, Sorting a float data

断了今生、忘了曾经 提交于 2019-11-28 08:30:48
Is radix sort capable of sorting float data for example 0.5, 0.9, 1.02, etc.? Sylvain Defresne Yes, it is possible. It requires an additional pass to correctly handle negative values. The articles by Pierre Terdiman and Michael Herf discuss in detail how to implement it. In short, you convert the float to unsigned integer, sort them, and then convert them back to float (this is required, otherwise the negatives values would be incorrectly sorted after the positive ones). Their method has the advantage that you do not introduce any error into your data (provided that your processor stores the