4.ASCII sorting-Learn to insert sort
ASCII-code Ordering
Time limit: Ms | Memory Limit: 65535 KB
Difficulty: 2
Describe
After entering three characters (which can be repeated), the ASCII code of each character is printed in the order of three characters from small to large.
Input
The first line enters a number n, which indicates that there are n sets of test data. The next n rows enter multiple sets of data, each with a row of three characters and no spaces between them.
Output
For each set of input data, the output line is separated by a space between the characters.
Sample input
3
Qwe
Asd
Zxc
Sample output
E Q W
A D S
c x Z
#include <stdio.h> #include <string.h>/** * @brief Insert sort * @param array Array to sort * @param len length of the array * @return */int sortinsert ( char * elem_array, int len ) { if ( null == elem_ array | | 0 >= len ) { return -1; } int i = 1; int j = 0 ; for ( ; i < len; i++ ) { if ( elem_array[i] < elem_array[i-1]) { char tmp = elem_array[i]; for ( j = i - 1; j >= 0 && elem_array[j] > tmp; j--) { elem_array[j+1] = elem_array[j]; } elem_array[j+1] = tmp; } } return 0;} Int main ( void ) { int n = 0; scanf ("%d", &n); char charr[4]; while (n--) { scanf ("%s", Charr); sortinsert (charr,3); printf ("%c %c %c\n", charr[0],charr[1],charr[2]); } return 0;}
Introduction to Nanyang oj--Language--from small code