# Question 30: how to understand cardinality sorting?

Time：2021-11-30

## What is cardinality sorting?

Basic idea: cardinality sorting is sorting first according to the low order, and then collecting; Then sort according to the high order, and then collect; And so on until the highest order

Visual expression: it is to split each number according to its digits, and compare and sort each corresponding digit until all digits are sorted once

The most important thing in basic sorting is the number of digits

Number: 832 can be divided into single digit, ten digit and hundred digit

Letters: SDF can be split into S D F by the number of digits

## Chestnuts

Suppose there is a set of sequences: 329, 457, 657, 839, 436, 720, 355

First, we know that the number of digits of their maximum value (839) has 3 digits (hundreds, tens and single digits), so we can sort and compare the corresponding digits of this group of sequences

First, sort the single digit (the rightmost number) and the result is

720, 355, 436, 457, 657, 329, 839

Then sort the ten digits (the middle number) and the result is

720, 329, 436, 839, 355, 457, 657

Then sort the hundreds (the rightmost number) and the result is

329, 355, 436, 457, 657, 720, 839

Each digit is sorted and compared respectively, so the traversal ends.

Finally, the ordered sequence is obtained

Then someone will ask at this time, what if their digits are different? What if each element is a string of letters instead of numbers?

## How to deal with different digits?

3, 200, 55, 220, 70

Generally, we judge each digit from 0 to 9. If the digits are different, we should judge whether the element has single digit, ten digit and hundred digit in advance. If not, it will be ranked in front of 0

## Element is an English string, not a number?

The size of a single letter can also be judged, A-Z

The implementation of the element as an English string and number is the same, but there is no single digit, ten digit and hundred digit. You can change it to the 0 digit, 1 digit and 2 digit on the right

<img></img>

<img></img>

## Golden nine silver ten: byte three-sided warp at the front end of a year

background Recently, I also wrote an article in naked CIGolden nine silver ten: Interview sharing at the front of the year, after many days,Finally summoned up the courage to resume the interview, the reason why the byte face is written separately,Because you really need to know some big factory interview experience in advance, the problem […]