注意,本文不是字符串排序,是字符串数组的排序。

方法分别是:

  • 1、低位优先键索引排序
  • 2、高位优先建索引排序
  • 3、Java自带排序(经过调优的归并排序)
  • 4、冒泡排序
  • 5、快速排序
  • 6、三向快速排序

时间复杂度:

  • 最慢的肯定是冒泡,O(n的平方)
  • 最快的是快速排序,平均 O(nlogn)
  • 低位优先,O(nW),W是字符串长度,在字符串长度较短情况下和快速排序时间应该很接近
  • 高位优先,O(n) - O(nW)
  • 三向快速排序,O(n) - O(nW)

本文中使用的例子是一个5757行的随机字符串数组文本TXT,实际测试结果:

低位优先键索引排序:5 ms
高位优先键索引排序:8 ms
JAVA自带排序:9 ms
冒泡排序:284 ms
快速排序:8 ms
三向快速排序:12 ms

稳定的排序是:

  • 低位优先键索引排序
  • 高位优先建索引排序
  • 归并排序(Java自带的排序算法),速度还行,关键是保持循环情况下的顺序稳定

低位优先:

public static void sort(String[] a, int w) {int n = a.length;int R = 256;   // extend ASCII alphabet sizeString[] aux = new String[n];for (int d = w-1; d >= 0; d--) {int[] count = new int[R+1];for (int i = 0; i < n; i++)count[a[i].charAt(d) + 1]++;for (int r = 0; r < R; r++)count[r+1] += count[r];for (int i = 0; i < n; i++)aux[count[a[i].charAt(d)]++] = a[i];for (int i = 0; i < n; i++)a[i] = aux[i];}}

高位优先:

https://algs4.cs.princeton.edu/code/edu/princeton/cs/algs4/MSD.java.html

JAVA自带排序:

Arrays.sort(arr);

冒泡:

    public static void bubblingSort(String[] arr) {int size = arr.length;for(int i = 0; i<size-1; i++) {for (int j = i+1; j<arr.length; j++) {if(arr[i].compareTo(arr[j])>0) {String temp = arr[i];arr[i] = arr[j];arr[j] = temp;}}}}

快速:

static void quickSort(String[] arr,int left,int right)            //快速排序算法
    {String  f,t;int rtemp,ltemp;ltemp=left;rtemp=right;f=arr[(left+right)/2];                        //分界值while(ltemp<rtemp){while(arr[ltemp].compareTo(f)<0){++ltemp;}while(arr[rtemp].compareTo(f)>0) {--rtemp;}if(ltemp<=rtemp){t=arr[ltemp];arr[ltemp]=arr[rtemp];arr[rtemp]=t;--rtemp;++ltemp;}}if(ltemp==rtemp) {ltemp++;}if(left<rtemp) {quickSort(arr,left,ltemp-1);            //递归调用
        }if(ltemp<right) {quickSort(arr,rtemp+1,right);            //递归调用
        }}

三向快速:

https://algs4.cs.princeton.edu/code/edu/princeton/cs/algs4/Quick3string.java.html

验证代码:

public static void main(String[] args) {URL path = SortWords.class.getResource("");        //不定长随机单词1000个//File file = new File(path.getPath()+"/1000words.txt");//长度为5的单词,5757个File file = new File(path.getPath()+"/words5.txt");File file1 = new File(path.getPath()+"/words5.txt");File file2 = new File(path.getPath()+"/words5.txt");File file3 = new File(path.getPath()+"/words5.txt");File file4 = new File(path.getPath()+"/words5.txt");File file5 = new File(path.getPath()+"/words5.txt");String[] arr = (String[])ReadFiledata.txt2List(file).toArray(new String[0]);//排序前for(String s : arr) {//System.out.println(s.toString());
        }//##############低位优先
        TimeMillis.setStart();LSD.sort(arr,5);TimeMillis.setEnd("低位优先键索引排序:");//排序后for(String s : arr) {//System.out.println(s.toString());
        }//##############高位优先String[] arr1 = (String[])ReadFiledata.txt2List(file1).toArray(new String[0]);TimeMillis.setStart();MSD.sort(arr1);TimeMillis.setEnd("高位优先键索引排序:");//排序后for(String s : arr1) {//System.out.println(s.toString());
        }//##############JAVA自带排序String[] arr2 = (String[])ReadFiledata.txt2List(file2).toArray(new String[0]);TimeMillis.setStart();Arrays.sort(arr2);TimeMillis.setEnd("JAVA自带排序:");//排序后for(Object s : arr2) {//System.out.println(s.toString());
        }//##############冒泡排序
String[] arr3 = (String[])ReadFiledata.txt2List(file3).toArray(new String[0]);TimeMillis.setStart();bubblingSort(arr3);TimeMillis.setEnd("冒泡排序:");//排序后for(String s : arr3) {//System.out.println(s.toString());
        }//##############快速排序String[] arr4 = (String[])ReadFiledata.txt2List(file4).toArray(new String[0]);TimeMillis.setStart();quickSort(arr4,0,5756);TimeMillis.setEnd("快速排序:");//排序后for(String s : arr4) {//System.out.println(s.toString());
        }//##############三向快速排序String[] arr5 = (String[])ReadFiledata.txt2List(file5).toArray(new String[0]);TimeMillis.setStart();Quick3string.sort(arr5);TimeMillis.setEnd("三向快速排序:");//排序后for(String s : arr5) {//System.out.println(s.toString());
        } }

运行多次结果相近:

低位优先键索引排序:8 ms
高位优先键索引排序:10 ms
JAVA自带排序:15 ms
冒泡排序:315 ms
快速排序:9 ms
三向快速排序:13 ms

用到的数据txt文件下载:

https://files.cnblogs.com/files/starcrm/words5.zip

ReadFiledata帮助类:

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.List;public class ReadFiledata {public static String txt2String(File file){StringBuilder result = new StringBuilder();try{BufferedReader br = new BufferedReader(new FileReader(file));String s = null;while((s = br.readLine())!=null){result.append(System.lineSeparator()+s);}br.close();    }catch(Exception e){e.printStackTrace();}return result.toString();}public static List<String> txt2List(File file){try{BufferedReader br = new BufferedReader(new FileReader(file));List<String> list = new ArrayList<String>();String s;while((s = br.readLine())!=null){list.add(s);}br.close(); return list;}catch(Exception e){e.printStackTrace();}return null;}public static Object[] txt2Array(File file){return  txt2List(file).toArray();}}

View Code

参考书目:《算法 4th》

转载于:https://www.cnblogs.com/starcrm/p/10736749.html

6种字符串数组的java排序 (String array sort)相关推荐

  1. 、有以下两组数据: 整型数组:1789,2035,1899,1456,2013,1458,2458,1254,1472,2365 字符串数组:“Java”,”Python”,”PHP”,”CProgr

    .有以下两组数据:整型数组:1789,2035,1899,1456,2013,1458,2458,1254,1472,2365字符串数组:"Java","Python&q ...

  2. 有以下两组数据: 整型数组:1789,2035,1899,1456,2013,1458,2458,1254,1472,2365 字符串数组:“Java”,”Python”,”PHP”,”CProg

    有以下两组数据: 整型数组:1789,2035,1899,1456,2013,1458,2458,1254,1472,2365 字符串数组:"Java","Python& ...

  3. Bailian2752 字符串数组排序问题【排序】

    2752:字符串数组排序问题 总时间限制: 1000ms 内存限制: 65536kB 描述 给定一组字符串,按指定的排序方式输出这些字符串.排序可是自然顺序(inc).自然逆序(dec).忽略大小写顺 ...

  4. java字符串转对象数组_将字符串数组转为java对象

    最近在工作中,遇到一个场景:接受到一个字符串数组,需要将其转为一个对象,反射实现的方法如下: /** * 将字符串数组转成对象:支持double int boolean string * @param ...

  5. js中数组反向、排序reverse、sort

    全栈工程师开发手册 (作者:栾鹏) js系列教程1-数组操作全解 js中数组反向.排序 数组反向使用reverse函数,数组排序使用sort函数,排序函数可以传入比较函数,也可以修改数组圆形,自定义添 ...

  6. java 字符串数组定义_「string数组」string 数组怎么定义 - seo实验室

    string数组 string数组的定义有三种: String arr[] = new String[10]; //创建一个长度为10的String 类型数组. String arr[] = {&qu ...

  7. Java 字符串数组定义_「string数组」string 数组怎么定义

    前些天发现了一个巨牛的人工智能学习网站,通俗易懂,风趣幽默,忍不住分享一下给大家.点击跳转到网站 点击跳转浏览. string数组的定义有三种: String arr[] = new String[1 ...

  8. java字符串转url,Java - 将String转换为有效的URI对象

    回答(11) 2 years ago 像这样(见URIUtil): URIUtil.encodeQuery("http://www.google.com?q=a b") 会变成: ...

  9. intent几种传值数组、对象、集合(Array,Object,List)

    1.Array private ArrayList<String> checkList=new ArrayList<String>();Intent intent=new In ...

最新文章

  1. mysql udf 性能_适当的mysql udf
  2. 人工智能的挑战远未到来
  3. python元组类型_什么是python元组数据类型
  4. 基于PHP的图片共享网站设计,基于php实现的web图片共享系统(论文+程序)
  5. [Python从零到壹] 三十七.图像处理基础篇之图像融合处理和ROI区域绘制
  6. JavaScript初阶(二)
  7. Oracle收购Sun
  8. MyBatis学习总结[5]-动态 SQL
  9. 分享两个软件,listary和Snipaste,以及Listary的配置文件
  10. cat3 utp是不是网线_五类网线(CAT 5E/CAT 3 UTP)
  11. 从7654浏览器卸载到安装360安全卫士
  12. Java从遗忘到入门——Day06
  13. 求学信计算机专业英语,英语求学信模板
  14. 2021中国低/无代码平台投融资趋势报告: 融资规模近15亿,估值近70亿,马太效应将愈演愈烈...
  15. vbs脚本实现qq定时发消息(初级)
  16. ChatGPT官宣数学能力再升级,网友:终于精通十以内加减法了
  17. MySql 全文检索
  18. ADO.NET Entity Framework 入门示例
  19. html隐藏超链接下划线
  20. uipath 执行 insert 语句报 “语法错误“ 的问题解决

热门文章

  1. Python min() 方法
  2. UVa 1583 Digit Generator(枚举+打表)
  3. js中的日期控件My97 DatePicker
  4. NOIP 2014 Day1 T3飞扬的小鸟
  5. 2004我曾经最喜欢的歌(一)
  6. Flutter Listener 监听手指的滑动方向、监听手指上下滑动
  7. Flutter AnimatedSwitcher 动画切换组件的基本使用
  8. 精通Android自定义View(十四)绘制水平向右加载的进度条
  9. 在Codewars刷题时常用的正则表达式
  10. 报错Cannot determine embedded database driver class for database type NONE解决方法