607的彩的正确网址

现在的位置: 607的彩的正确网址 > 算法 > 正文

基数排序是什么

2020年02月14日 算法 ⁄ 共 5368字 ⁄ 字号 评论关闭

  基数排序是一种非比较型整数排序算法,其原理是将整数按位数切割成不同的数字,然后按每个位数分别比较。由于整数也可以表达字符串(比如名字或日期)和特定格式的浮点数,所以基数排序也不是只能使用于整数。


  1. 基数排序 vs 计数排序 vs 桶排序


  jishupaixuyouliangzhongfangfa:


  zheisanzhongpaixusuanfadouliyongletongdegainian,danduitongdeshiyongfangfashangyoumingxianchayi:


  jishupaixu:genjujianzhidemeiweishuzilaifenpeitong;


  jishupaixu:meigetongzhicunchudanyijianzhi;


  tongpaixu:meigetongcunchuyidingfanweideshuzhi;


  2. LSD 基数排序动演示


  JavaScript实例


  //lsd radix sort


  var counter = [];


  function radixsort(arr, maxdigit) {


  var mod = 10;


  var dev = 1;


  for (var i = 0; i < maxdigit; i++, dev *= 10, mod *= 10) {


  for(var j = 0; j < arr.length; j++) {


  var bucket = parseint((arr[j] % mod) / dev);


  if(counter[bucket]==null) {


  counter[bucket] = [];


  }


  counter[bucket].push(arr[j]);


  }


  var pos = 0;


  for(var j = 0; j < counter.length; j++) {


  var value = null;


  if(counter[j]!=null) {


  while ((value = counter[j].shift()) != null) {


  arr[pos++] = value;


  }


  }


  }


  }


  return arr;


  }


  Java实例


  /**


  * jishupaixu


  * kaolvfushudeqingkuanghaikeyicankao: http://code.i-harness.com/zh-cn/q/e98fa9


  */


  public class radixsort implements iarraysort {


  @override


  public int[] sort(int[] sourcearray) throws exception {


  // dui arr jinxingkaobei,bugaibiancanshuneirong


  int[] arr = arrays.copyof(sourcearray, sourcearray.length);


  int maxdigit = getmaxdigit(arr);


  return radixsort(arr, maxdigit);


  }


  /**


  * huoquzuigaoweishu


  */


  private int getmaxdigit(int[] arr) {


  int maxvalue = getmaxvalue(arr);


  return getnumlenght(maxvalue);


  }


  private int getmaxvalue(int[] arr) {


  int maxvalue = arr[0];


  for (int value : arr) {


  if (maxvalue < value) {


  maxvalue = value;


  }


  }


  return maxvalue;


  }


  protected int getnumlenght(long num) {


  if (num == 0) {


  return 1;


  }


  int lenght = 0;


  for (long temp = num; temp != 0; temp /= 10) {


  lenght++;


  }


  return lenght;


  }


  private int[] radixsort(int[] arr, int maxdigit) {


  int mod = 10;


  int dev = 1;


  for (int i = 0; i < maxdigit; i++, dev *= 10, mod *= 10) {


  // kaolvfushudeqingkuang,zheilikuozhanyibeiduilieshu,qizhong [0-9]duiyingfushu,[10-19]duiyingzhengshu (bucket + 10)


  int[][] counter = new int[mod * 2][0];


  for (int j = 0; j < arr.length; j++) {


  int bucket = ((arr[j] % mod) / dev) + mod;


  counter[bucket] = arrayappend(counter[bucket], arr[j]);


  }


  int pos = 0;


  for (int[] bucket : counter) {


  for (int value : bucket) {


  arr[pos++] = value;


  }


  }


  }


  return arr;


  }


  /**


  * zidongkuorong,bingbaocunshuju


  *


  * @param arr


  * @param value


  */


  private int[] arrayappend(int[] arr, int value) {


  arr = arrays.copyof(arr, arr.length + 1);


  arr[arr.length - 1] = value;


  return arr;


  }


  }


  PHP实例


  function radixsort($arr, $maxdigit = null)


  {


  if ($maxdigit === null) {


  $maxdigit = max($arr);


  }


  $counter = [];


  for ($i = 0; $i < $maxdigit; $i++) {


  for ($j = 0; $j < count($arr); $j++) {


  preg_match_all('/\d/', (string) $arr[$j], $matches);


  $numarr = $matches[0];


  $lentmp = count($numarr);


  $bucket = array_key_exists($lentmp - $i - 1, $numarr)


  ? intval($numarr[$lentmp - $i - 1])


  : 0;


  if (!array_key_exists($bucket, $counter)) {


  $counter[$bucket] = [];


  }


  $counter[$bucket][] = $arr[$j];


  }


  $pos = 0;


  for ($j = 0; $j < count($counter); $j++) {


  $value = null;


  if ($counter[$j] !== null) {


  while (($value = array_shift($counter[$j])) !== null) {


  $arr[$pos++] = $value;


  }


  }


  }


  }


  return $arr;


  }


  C++实例


  int maxbit(int data[], int n) //fuzhuhanshu,qiushujudezuidaweishu


  {


  int maxdata = data[0]; ///< zuidashu


  /// xianqiuchuzuidashu,zaiqiuqiweishu,zheiyangyouyuanxianyicimeigeshupanduanqiweishu,shaoweiyouhuadian。


  for (int i = 1; i < n; ++i)


  {


  if (maxdata < data[i])


  maxdata = data[i];


  }


  int d = 1;


  int p = 10;


  while (maxdata >= p)


  {


  //p *= 10; // maybe overflow


  maxdata /= 10;


  ++d;


  }


  return d;


  /* int d = 1; //baocunzuidadeweishu


  int p = 10;


  for(int i = 0; i < n; ++i)


  {


  while(data[i] >= p)


  {


  p *= 10;


  ++d;


  }


  }


  return d;*/


  }


  void radixsort(int data[], int n) //jishupaixu


  {


  int d = maxbit(data, n);


  int *tmp = new int[n];


  int *count = new int[10]; //jishuqi


  int i, j, k;


  int radix = 1;


  for(i = 1; i <= d; i++) //jinxingdcipaixu


  {


  for(j = 0; j < 10; j++)


  count[j] = 0; //meicifenpeiqianqingkongjishuqi


  for(j = 0; j < n; j++)


  {


  k = (data[j] / radix) % 10; //tongjimeigetongzhongdejilushu


  count[k]++;


  }


  for(j = 1; j < 10; j++)


  count[j] = count[j - 1] + count[j]; //jiangtmpzhongdeweizhiyicifenpeijimeigetong


  for(j = n - 1; j >= 0; j--) //jiangsuoyoutongzhongjiluyicishoujidaotmpzhong


  {


  k = (data[j] / radix) % 10;


  tmp[count[k] - 1] = data[j];


  count[k]--;


  }


  for(j = 0; j < n; j++) //jianglinshishuzudeneirongfuzhidaodatazhong


  data[j] = tmp[j];


  radix = radix * 10;


  }


  delete []tmp;


  delete []count;


  }


  C语言实例


  #include< stdio.h>


  #define max 20


  //#define showpass


  #define base 10


  void print(int *a, int n) {


  int i;


  for (i = 0; i < n; i++) {


  printf("%d\t", a[i]);


  }


  }


  void radixsort(int *a, int n) {


  int i, b[max], m = a[0], exp = 1;


  for (i = 1; i < n; i++) {


  if (a[i] > m) {


  m = a[i];


  }


  }


  while (m / exp > 0) {


  int bucket[base] = { 0 };


  for (i = 0; i < n; i++) {


  bucket[(a[i] / exp) % base]++;


  }


  for (i = 1; i < base; i++) {


  bucket[i] += bucket[i - 1];


  }


  for (i = n - 1; i >= 0; i--) {


  b[--bucket[(a[i] / exp) % base]] = a[i];


  }


  for (i = 0; i < n; i++) {


  a[i] = b[i];


  }


  exp *= base;


  #ifdef showpass


  printf("\npass : ");


  print(a, n);


  #endif


  }


  }


  int main() {


  int arr[max];


  int i, n;


  printf("enter total elements (n <= %d) : ", max);


  scanf("%d", &n);


  n = n < max ? n : max;


  printf("enter %d elements : ", n);


  for (i = 0; i < n; i++) {


  scanf("%d", &arr[i]);


  }


  printf("\narray : ");


  print(&arr[0], n);


  radixsort(&arr[0], n);


  printf("\nsorted : ");


  print(&arr[0], n);


  printf("\n");


  return 0;


  }


  Lua实例


  -- huoqubiaozhongweishu


  local maxbit = function (tt)


  local weight = 10; -- shijinzhi


  local bit = 1;


  for k, v in pairs(tt) do


  while v >= weight do


  weight = weight * 10;


  bit = bit + 1;


  end


  end


  return bit;


  end


  -- jishupaixu


  local radixsort = function (tt)


  local maxbit = maxbit(tt);


  local bucket = {};


  local temp = {};


  local radix = 1;


  for i = 1, maxbit do


  for j = 1, 10 do


  bucket[j] = 0; --- qingkongtong


  end


  for k, v in pairs(tt) do


  local remainder = math.floor((v / radix)) % 10 + 1;


  bucket[remainder] = bucket[remainder] + 1; -- meigetongshuliangzidongzengjia1


  end


  for j = 2, 10 do


  bucket[j] = bucket[j - 1] + bucket[j]; -- meigetongdeshuliang = yiqiantongshulianghe + zigeshuliang


  end


  -- anzhaotongdeweizhi,paixu--zheigeshitongshipaixu,bixushiyongdaoxu,yinweipaixufangfashicongxiaodaoda,shunxuxialai,huichuxiandadezaixiaodeshangmianqingkong。


  for k = #tt, 1, -1 do


  local remainder = math.floor((tt[k] / radix)) % 10 + 1;


  temp[bucket[remainder]] = tt[k];


  bucket[remainder] = bucket[remainder] - 1;


  end


  for k, v in pairs(temp) do


  tt[k] = v;


  end


  radix = radix * 10;


  end


  end;


  yishangjiushijishupaixudexiangguanjieshao,yaolejiegengduojishuzhishi,keyishangxuebuyuan。

baoqian!pinglunyiguanbi.

2元中国福利彩票怎么看 2元买彩票选几个数字 2元中国福利彩票的买法 2元就可以打1毛的斗地主棋牌 2元彩是值得您信赖的 2元斗地主群 2元的彩的网 2元彩票双色球走势图双色球 2元彩票网全国走势图 2元就可以打1毛的炸金花棋牌