[Introduction to algorithms-015] Radix sort)

Source: Internet
Author: User
1. section 8.3 of p197 of Introduction to algorithms Radix sort2 and Java only modify the parameters of [Introduction to algorithms-014] counting and sorting, and modify only one line of code.

/*** Creation Time: 4:05:48, January 1, August 17, 2014 * Project name: test * @ author Cao yanfeng * @ since JDK 1.6.0 _ 21 * class description: Use count sorting to sort the base * condition: the number of all digits to be sorted is the same. Note that, even if they are different, they can be considered as the maximum number of digits, the following example can be considered as three-digit */public class radixsorttest {/*** @ Param ARGs */public static void main (string [] ARGs) {// todo auto-generated method stub int [] array = {73, 22, 93, 43, 55, 14, 28, 65, 39, 81, 33,100 }; int [] result = radixsort (array, 3); For (INT I = 0; I <result. length; I ++) {system. out. println (result [I]) ;}} public static int [] radixsort (INT [] array, int d) {int length = array. length; int [] digits = new int [length]; for (INT I = 1; I <= D; I ++) {for (Int J = 0; j <length; j ++) {int A = (INT) math. pow (10, I); int B = (INT) math. pow (10, I-1); digits [J] = array [J] % A/B ;} <PRE name = "code" class = "Java" style = "font-size: 16px; line-Height: 24px; ">/* sort the original array by the value of each bit */
Array = countingsort (digits, 9, array);} return array;}/* count sorting. Note that the Java array subscript starts from 0, so the temp length is k + 1, */public static int [] countingsort (INT [] array, int K, Int [] originalarray) {Int [] temp = new int [k + 1]; int [] result = new int [array. length]; for (INT I = 0; I <K; I ++) {temp [I] = 0;} For (INT I = 0; I <array. length; I ++) {temp [array [I] = temp [array [I] + 1;} For (INT I = 1; I <temp. length; I ++) {temp [I] + = temp [I-1];}/* temp [array [I] indicates the count of all numbers, that is, the length of the array, note that the subscript is Temp [array [I]-1 */For (INT I = array. length-1; I> = 0; I --) {/** key point: elements not greater than array [I] have temp [array [I, the sorting position of array [I] Is temp [array [I]-1 * // result [temp [array [I]-1] = array [I]; Result [temp [array [I]-1] = originalarray [I];Temp [array [I] = temp [array [I]-1;} return result ;}}

 


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.