数值排序问题-shell
来源:互联网 发布:pinnacle软件 编辑:程序博客网 时间:2024/06/03 12:28
数值排序问题 - 超大数比较
问题
文本a.txt中有20行随机产生的数列,如下:
2928326128601232462131283250710027308938740594716691200992050511576
5352129649530193383124730478244772348721985707222557212265817305
217141333532296179938475175265792931789219830308392472584606305
2371620291160322081050531817416284225477019123161801285941026814244
283929972304551060318886921731765136928849135391662294051194618754
1809165929787147057932949630411324311737224509104016550662932273
27396236084901303873154718299242931819623155304661177528921164510335
254221462410491137971033914630292752245114969186002809930190939425
1085287492160525651862932475207612387312368408826675135332406418337
2567810118246621010283281198810903279355871571118961177731143829148
23727111515524141721964179351992331180134926914198081871053303186
378579502856625703213542353218420835730692264021219729654278515442
30215186011014395001656818458819061824708536511543271701327524725
223702764213159156022932717903282522044350522584222768193271431422354
3079720530119542370417125702274761144023302102641160114921224469221
15642298214000242538839193816839550322381321993212316517861828002
13042178002978222022331319116624809338275899045263351248023569
11252165681825711849278422768716060438517976169102391532289954712000
105084292396529699311371735329685626410510259482788519645152723476
29674179062831103792824121564178225289202161443911094228581583531951
要求
通过纯批处理按照数列的大小顺序,正序输出如下
PS:每行的数值都远远超过了cmd所能计算的最大数值。
13042178002978222022331319116624809338275899045263351248023569
217141333532296179938475175265792931789219830308392472584606305
1809165929787147057932949630411324311737224509104016550662932273
5352129649530193383124730478244772348721985707222557212265817305
15642298214000242538839193816839550322381321993212316517861828002
23727111515524141721964179351992331180134926914198081871053303186
30215186011014395001656818458819061824708536511543271701327524725
105084292396529699311371735329685626410510259482788519645152723476
254221462410491137971033914630292752245114969186002809930190939425
283929972304551060318886921731765136928849135391662294051194618754
378579502856625703213542353218420835730692264021219729654278515442
1085287492160525651862932475207612387312368408826675135332406418337
2371620291160322081050531817416284225477019123161801285941026814244
2567810118246621010283281198810903279355871571118961177731143829148
2928326128601232462131283250710027308938740594716691200992050511576
3079720530119542370417125702274761144023302102641160114921224469221
11252165681825711849278422768716060438517976169102391532289954712000
27396236084901303873154718299242931819623155304661177528921164510335
29674179062831103792824121564178225289202161443911094228581583531951
223702764213159156022932717903282522044350522584222768193271431422354
编写代码
编写一个比较大数的函数,创建文件
compare.sh
#! /bin/bash## 比较两个数大小## ## 返回值#### 1 第一个数字大于第二个数字## 0 第一个数字等于第二个数字## -1 第一个数字小于第二个数字function compare(){ str1=$1 str2=$2 param=$# ## 这里不做判断是否为数字处理 flag= if [ ${param} -ne 2 ] then echo "参数个数: ${param},不为2,不能计算,请核实" exit fi ## 比较两个数字位数,位数大则数字大 len1=${#str1} len2=${#str2} if [ ${len1} -gt ${len2} ] then flag=1 elif [ ${len1} -lt ${len2} ] then flag=-1 else ## 两个数字位数一样,依次比较两个数各个位大小 i=0 while [ ${i} -lt ${len1} ] do c1=${str1:${i}:1} c2=${str2:${i}:1} if [ ${c1} -gt ${c2} ] then flag=1 break elif [ ${c1} -lt ${c2} ] then flag=-1 break else flag=0 fi let i++ done fi echo ${flag}}
排序文件
run.sh
#! /bin/bash## 引入其他文件 - 使用 compare 函数. ./compare.shfile=./a.txt## 将文件转化为一维数组i=0while read linedo lines[${i}]=${line} let i++done < ${file}## 一位数组个数len=${#lines[*]}## 使用简单选择排序i=0while [ ${i} -lt ${len} ]do index=${i} minindex=${i} let j=${i}+1 while [ ${j} -lt ${len} ] do ## if [ ${lines[${index}]} -gt ${line[${j}]} ] 特大数字不能直接参与运算,使用我们自定义函数 ret=`compare ${lines[${minindex}]} ${lines[${j}]}` if [ ${ret} -eq 1 ] then minindex=${j} fi let j++ done ## 交换index与minindex的值 tmp=${lines[${index}]} lines[${index}]=${lines[${minindex}]} lines[${minindex}]=${tmp} let i++done##输出i=0while [ ${i} -lt ${len} ]do echo ${lines[${i}]} let i++done
测试
- 数值排序问题-shell
- mysql字符串数值按数值排序问题
- 选择排序算法中的交换数值问题
- shell sort 希尔排序问题
- 数组和哈希的数值排序问题(sort)
- Shell数值、字符串比较
- bash shell 数值比较
- shell中的数值计算
- shell中的数值计算
- Shell数值、字符串比较
- shell 数值 字符串比较
- linux shell 数值运算
- Shell数值、字符串比较
- Shell数值、字符串比较
- Shell数值操作
- shell测试数值
- Shell数值、字符串比较
- shell 数值计算
- HDU 3333 Turing Tree(线段树)
- linux下安装运行LoadrGenerator
- 基于MNIST数据集实现车牌识别--初步演示版
- maximal-rectangle
- 堆排序Heap sort
- 数值排序问题-shell
- 使用Maven部署构件至私服
- 什么是架构师
- libcurl中的回调函数
- 2017 ACM-ICPC 亚洲区(南宁赛区)网络赛 A Weather Patterns(阅读理解)
- spring mvc表单中集合类型的参数绑定,含集合下标动态改变
- 防火墙的简介
- CSS(五)— 颜色
- Matterport3D:室内环境RGB-D数据的深度学习