counting-sort

Two ways of doing Counting Sort

风格不统一 提交于 2020-01-24 15:42:05
问题 Here are my two implementations of Counting Sort In this implementation which is a very simple one, all I do is count the number of occurrences of the element, and insert as many times as the occurrences in the output array. Implementation 1 public class Simple { static int[] a = {5,6,6,4,4,4,8,8,8,9,4,4,3,3,4}; public static void main(String[] args) { fun(a); print(a); } static void fun(int[] a) { int max = findMax(a); int[] temp = new int[max+1]; for(int i = 0;i<a.length;i++) { temp[a[i]]++

Why we can not apply counting sort to general arrays?

▼魔方 西西 提交于 2020-01-10 02:03:09
问题 Counting sort is known with linear time if we know that all elements in the array are upper bounded by a given number. If we take a general array, cant we just scan the array in linear time, to find the maximum value in the array and then to apply counting sort? 回答1: It is not enough to know the upper bound to run a counting sort: you need to have enough memory to fit all the counters. Consider a situation when you go through an array of 64-bit integers, and find out that the largest element

Hackerearth bubbleSort

只愿长相守 提交于 2019-12-11 14:08:41
问题 In Hackerearth i tried solving bubble sort swap counting. and my output always different from correct output.for example; my output is 2475 and correct output is 2788 #include <iostream> using namespace std; int main() { int *A,tm,times=0; cin >> tm; A = new int[tm]; for(int i = 0; i<tm;i++) {cin >> A[i];} int temp; for(int i = 0; i<tm;i++){ for(int j = 0; j < tm-i-1;j++){ if(A[j] > A[j+1]){ times++;; temp = A[j]; A[j] = A[j+1]; A[j] = temp; } } } cout << times; return 0; } Am i doing

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

为君一笑 提交于 2019-12-03 01:50:49
问题 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

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

Why we can not apply counting sort to general arrays?

自闭症网瘾萝莉.ら 提交于 2019-11-29 08:01:59
Counting sort is known with linear time if we know that all elements in the array are upper bounded by a given number. If we take a general array, cant we just scan the array in linear time, to find the maximum value in the array and then to apply counting sort? It is not enough to know the upper bound to run a counting sort: you need to have enough memory to fit all the counters. Consider a situation when you go through an array of 64-bit integers, and find out that the largest element is 2^60. This would mean two things: You need an O(2^60) memory, and It is going to take O(2^60) to complete