zoukankan      html  css  js  c++  java
  • Radix Sort Java Implementation

     1 int main()
     2 {
     3     int arr[] = {53,3,542,748,14,214};
     4 
     5 }
     6 void radixSort(int[] arr)
     7 {    
     8         //Buckets (one bucket = one array)
     9         int[][] bucket = new int[10][arr.length]
    10         //In order to record the # of vals stored in each bucket each time,we use an array to hold the these 10 #. 
    11         int[] bucketElementCounts = new int[10];
    12 
    13         //Iterations (LSN)
    14         for(int i=0,n=1;i<N;i++,n*=10)
    15         {
    16             for(int j = 0; j < arr.length; j++)
    17             {
    18                 int digitOfElement = arr[j]/n%10;
    19 
    20                 //Put them into corrsponding bucket
    21                 bucket[digitOfElement][bucketElementCounts[digitOfElement]] 
    22                 =arr[j];
    23                 bucketElementCounts[digitOfElement]++; 
    24             }
    25             //Traverse every bucket.Put back data into sequence.
    26             int index = 0;
    27             for(int k= 0; k < 10; k++  )
    28             {
    29                 if(bucketElementCounts[k] != 0)
    30                 {
    31                     for(int l=0; l<bucketElementCounts[k];l++)
    32                     {
    33                         arr[index] = bucket[k][l];
    34                         index++;
    35                     }
    36                 }
    37                 //important, bucketElementCounts[k] = 0
    38                 bucketElementCounts[k] = 0;
    39             }
    40             PRINT: (arr)
    41         }
    42 }
  • 相关阅读:
    值传递
    抽象类
    面向对象三大特征(二)--继承
    单例设计模式
    神奇的main方法详解
    面向对象的三大特征 ---- 封装
    变量、方法以及静态和非静态
    面向对象编程-类和对象
    数组
    力扣题库刷题(随时记录)
  • 原文地址:https://www.cnblogs.com/JasperZhao/p/12825233.html
Copyright © 2011-2022 走看看