Introduction to Algorithms third Edition study questions 8-2.E

Source: Internet
Author: User

1counting-SORT2 (a,k)2 Let C[0..K] is a new array3      fori =0 to K4c[i]=05      fori = 1To A.length6C[a[i]] = c[a[i]]+17      fori = 1to K8C[i] = C[i] + c[i-1]9      fori = a.length Downto 1Ten          whileI < (j =C[a[i]) One Exchagnge A[i] with A[j] AC[A[J]] = c[a[j]]-1

#include <stdio.h> #include <stdlib.h> #include <time.h> #include <memory.h>void radix2 (int* arr, int len, int k) {k++;int* Count_arr = (int*) malloc (sizeof (int) *k), for (int i = 0; i < K; i++) {count_arr[i] = 0;} for (int i = 0; i < len; i++) {count_arr[arr[i]]++;} for (int i = 1; i < K; i++) {Count_arr[i] + = count_arr[i-1];} for (int i = len-1; I >= 0; i--) {//a[i+1 ... len-1] element is the element that has determined the final position of elements//find Arr[i] The final position of the element is J, if i<j, then swap (A[i],a[j]),// Continue to find the final position of the new Arr[i] element until I>=jint j = count_arr[arr[i] "-1;while (i < (j = Count_arr[arr[i]]-1)) {int temp = Arr[j];arr [j] = arr[i];arr[i] = temp;count_arr[arr[j]]--;}} Free (Count_arr);} int rand_int (int max) {return (rand ()% max);} void Main () {int a[100] = {0};int max = 20;srand (Time (0)), for (int i = 0; i <; i++) {A[i] = Rand_int (max);} Radix2 (A, n, max); for (int i = 0; i <; i++) {printf ("%d", A[i]);} GetChar ();}

  

Introduction to Algorithms third Edition study questions 8-2.E

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.