counting sort

public class counting_sort {           //O(n)   it is stable(numbers with the same value appear in the output array in the same order as they do in the input                                                      //array)
public static void sort(int[] a ){
int n = a.length;
int[] b = new int[n];
int max = a[0];
for(int i= 0;i < n;i++){
if(a[i] > max){
max = a[i];
}
}
int[] c = new int[max + 1];
for(int i= 0;i <= max ;i++){
c[i] = 0;
}
for(int i= 0;i < n;i++){
c[a[i]] = c[a[i]] + 1;
}
for(int i= 1;i <= max ;i++){
c[i] = c[i] + c[i - 1];
}
for(int i= n-1;i >= 0;i--){
b[c[a[i]] - 1] = a[i];
c[a[i]] = c[a[i]] - 1;
}
for(int i = 0;i < b.length; i++){
System.out.println(b[i] + " ");}

}

}

时间: 2024-10-07 03:17:21

counting sort的相关文章

counting sort 计数排序

//counting sort 计数排序 //参考算法导论8.2节 #include<cstdio> #include<cstring> #include<algorithm> #include<cassert> using namespace std; const int k=5; const int n=7; int a[n]={5, 5, 1, 2 , 5, 4, 1}; int b[n]; int c[k+1]; int main() { int m

【HackerRank】 The Full Counting Sort

In this challenge you need to print the data that accompanies each integer in a list. In addition, if two strings have the same integers, you need to print the strings in their original order. Hence, your sorting algorithm should be stable, i.e. the

find out the neighbouring max D_value by counting sort in stack

1 #include <stdio.h> 2 #include <malloc.h> 3 #define MAX_STACK 10 4 5 int COUNT = 0; 6 // define the node of stack 7 typedef struct node { 8 int data; 9 node *next; 10 }*Snode; 11 12 // define the stack 13 typedef struct Stack{ 14 Snode top; 1

HDU 1718 Rank counting sort解法

本题是利用counting sort的思想去解题. 注意本题,好像利用直接排序,然后查找rank是会直接被判WA的,奇怪的判断系统. 因为分数值的范围是0到100,非常小,而student 号码又非常大,故此天然的需要利用counting sort的情况. #include <stdio.h> #include <string.h> const int MAX_N = 101; int arr[MAX_N]; int main() { int Jackson, JackScore,

经典排序算法 - 计数排序Counting sort

经典排序算法 - 计数排序Counting sort 注意与基数排序区分,这是两个不同的排序 计数排序的过程类似小学选班干部的过程,如某某人10票,作者9票,那某某人是班长,作者是副班长 大体分两部分,第一部分是拉选票和投票,第二部分是根据你的票数入桶 看下具体的过程,一共需要三个数组,分别是待排数组,票箱数组,和桶数组 var unsorted = new int[] { 6, 2, 4, 1, 5, 9 };  //待排数组 var ballot = new int[unsorted.Len

[Algorithms] Counting Sort

For a nice introduction to counting sort, please refer to Introduction to Alogrithms, 3rd edition. The following code is basically a translation of the pseudo code above. 1 #include <iostream> 2 #include <vector> 3 #include <ctime> 4 5 u

计数排序Counting sort

注意与基数排序区分,这是两个不同的排序 计数排序的过程类似小学选班干部的过程,如某某人10票,作者9票,那某某人是班长,作者是副班长 大体分两部分,第一部分是拉选票和投票,第二部分是根据你的票数入桶 看下具体的过程,一共需要三个数组,分别是待排数组,票箱数组,和桶数组 var unsorted = new int[] { 6, 2, 4, 1, 5, 9 };  //待排数组 var ballot = new int[unsorted.Length];          //票箱数组 var b

【LeetCode】Sort Colors

LeetCode OJ Given an array with n objects colored red, white or blue, sort them so that objects of the same color are adjacent, with the colors in the order red, white and blue. Here, we will use the integers 0, 1, and 2 to represent the color red, w

【数组】Sort Colors

题目: Given an array with n objects colored red, white or blue, sort them so that objects of the same color are adjacent, with the colors in the order red, white and blue. Here, we will use the integers 0, 1, and 2 to represent the color red, white, an