「JAVA排序demo」java排序的方法有哪些

博主:adminadmin 2023-01-02 17:15:04 802

本篇文章给大家谈谈JAVA排序demo,以及java排序的方法有哪些对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。

本文目录一览:

用java编写程序对三个整数排序

简单的整数排序,可以用选择排序、冒泡排序、插入排序。

code demo:

public class SortDemo {

  public static void main(String[] args) {

    int[] ary = {3,1,5,6,2,6,8,3};

    //ary = selectionSort(ary);

    ary = insertSort(ary);

    //  将数组ary连接为一个字符串: Arrays.toString(ary);

    //如:{1, 2, 3}-"[1, 2, 3]"

    String s = Arrays.toString(ary);

    System.out.println(s);

  }

  /** 选择排序 */

  public static int[] selectionSort(int[] ary){

    for(int i=0; iary.length-1; i++ ){

      for(int j=i+1; jary.length; j++){

        if(ary[i]ary[j]){

          int temp = ary[i];

          ary[i] = ary[j];

          ary[j] = temp;

        }

      }

    }

    return ary;

  }

  /** 冒泡排序 */

  public static int[] bubleSort(int[] ary){

    for(int i=0; iary.length-1; i++){

      for(int j=0; jary.length-(1+i); j++){

        if(ary[j]  ary[j+1]){

          int temp = ary[j];

          ary[j] = ary[j+1];

          ary[j+1] = temp;

        }

      }

    }

    return ary;

  }

  //插入排序

  public static int[] insertSort(int[] ary){

    //int[] ary = {3,1,5,6,2,6,8,3};

    for(int i=1; iary.length; i++){

      int temp = ary[i];

      int j;

      for(j=i-1; j=0  temp  ary[j]; j--){

        //if(temp  ary[j]){

          ary[j+1] = ary[j];

       // }else{

        //  break;//找到插入位置

        //}

      }

      ary[j+1] = temp;//插入操作

    }

    return ary;

  }

}

Java中对象按照属性排序

项目中经常需要用到比如地区列表按照中文的拼音首字母排序 但是有时候获取到的地区列表是对象的形式 有地区id 地区名称两个属性 这里主要涉及到两个技术点

对象按属性排序 中文按拼音首字母排序 下面给出自己整理好的demo:

import java text Collator;

import java util ArrayList;

import java util Collections;

import java util Comparator;

import mons beanutils BeanComparator;

import llections ComparatorUtils;

import parators ComparableComparator;

import parators ComparatorChain;

public class CompareTipA  {

private int  id;

private String  name;

private String age;

@SuppressWarnings( unchecked )

public static void main(String []args) {

ArrayListObject list = new ArrayListObject()

list add(new CompareTipA( 五 ))

list add(new CompareTipA( 六 ))

list add(new CompareTipA( 二 ))

list add(new CompareTipA( 四 ))

list add(new CompareTipA( 四 ))

list add(new CompareTipA( 一 ))

Comparator mycmp = ComparableComparator getInstance()

mycmp = ComparatorUtils nullLowComparator(mycmp)

//允许null

mycmp = ComparatorUtils reversedComparator(mycmp) //逆序

ArrayListObject sortFields = new ArrayListObject()

ComparatorObject parator=Collator getInstance(java util Locale CHINA)

sortFields add(new BeanComparator( name parator))   //name正序 (主)

sortFields add(new BeanComparator( id mycmp))

//id逆序  (副)

ComparatorChain multiSort = new ComparatorChain(sortFields)

Collections sort(list multiSort)

for (int i = ;ilist size() i++) {

System out println(list get(i))

//输出   /*

[id= name=二 age= ]

[id= name=六 age= ]

[id= name=四 age= ]

[id= name=四 age= ]

[id= name=五 age= ]

[id= name=一 age= ]*/

}

}

public CompareTipA(int id String age String name) {

this id = id;

this name = name;

this age = age;

}

public int getId() {

return this id;

}

public void setId(int id) {

this id = id;

}

public String getName() {

return this name;

}

public void setName(String name) {

this name = name;

}

//

public int pareTo(Object o) {   //

return pare(this o)    //    }

public String toString() {

return [id= + this id + name= + this name + age= + this age + ] ;

}

public String getAge() {

return age;

}

public void setAge(String age) {

this age = age;

}   }

注意 需要导入的三个包 mons beanutils jar

mons collections jar

mons logging jar

lishixinzhi/Article/program/Java/hx/201311/25790

求冒泡排序Java的Demo.要有注释,越详细越好。

public class BubbleSort{

public static void main(String[] args){

int score[] = {67, 69, 75, 87, 89, 90, 99, 100};

for (int i = 0; i score.length -1; i++){ //最多做n-1趟排序

for(int j = 0 ;j score.length - i - 1; j++){ //对当前无序区间score[0......length-i-1]进行排序(j的范围很关键,这个范围是在逐步缩小的)

if(score[j] score[j + 1]){ //把小的值交换到后面

int temp = score[j];

score[j] = score[j + 1];

score[j + 1] = temp;

}

}

System.out.print("第" + (i + 1) + "次排序结果:");

for(int a = 0; a score.length; a++){

System.out.print(score[a] + "\t");

}

System.out.println("");

}

System.out.print("最终排序结果:");

for(int a = 0; a score.length; a++){

System.out.print(score[a] + "\t");

}

}

}

关于JAVA排序demo和java排序的方法有哪些的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。