一、优化后的冒泡排序
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | package com.yzh.sort; /* 冒泡排序 */ @SuppressWarnings ({ "all" }) public class BubbleSort { public static void BubbleSort( int []a){ for ( int i = 0 ; i <a.length- 1 i= "" boolean = "" flag= "true;" for = "" j= "0;" if = "" >a[j+ 1 ]){ int temp=a[j]; a[j]=a[j+ 1 ]; a[j+ 1 ]=temp; flag= false ; } } if (flag) break ; } } } </a.length- 1 > |
二、选择排序
1 2 3 4 5 6 | package com.yzh.sort; @SuppressWarnings ({ "all" }) public class SelectSort { public static void SelectSort( int []a) { for ( int i = 0 ; i |
三、插入排序
1 2 3 4 5 6 7 8 9 | package com.yzh.sort; @SuppressWarnings ({ "all" }) /* 插入排序 */ public class InsertSort { public static void InsertSort( int []a){ for ( int i = 0 ; i = 0 && insertValue <a a= "" insertindex--= "" ></a> |
四、希尔排序
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | package com.yzh.sort; /* 希尔排序 */ @SuppressWarnings ({ "all" }) public class ShellSort { public static void ShellSort( int [] a){ for ( int gap=a.length / 2 ; gap > 0 ; gap = gap / 2 ) { //将整个数组分为若干个子数组 for ( int i = gap; i = 0 ; j=j-gap) { //交换元素 if (a[j]>a[j+gap]) { int temp=a[j]; a[j]=a[j+gap]; a[j+gap]=temp; } } } } } } |
五、快速排序
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | package com.yzh.sort; @SuppressWarnings ({ "all" }) /* 随机化快速排序 */ public class QuickSort { public static void QuickSort( int [] arr, int low, int high){ int i,j,temp,t; if (low>=high){ return ; } i=low; j=high; //temp就是基准位 temp = arr[low]; while (i<j while = "" j--= "" >=arr[i]&&i<j i= "" if = "" t= "arr[j];" arr= "" temp= "" quicksort= "" low= "" j- 1 = "" j= "" high= "" ></j></j> |
六、随机化快速排序
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | package com.yzh.sort; import java.util.Random; import java.util.Scanner; @SuppressWarnings ({ "all" }) public class RandQuickSort { public static void randsort( int [] arr, int left, int right) { if (left>=right) return ; Random random = new Random(); int randIndex = random.nextInt(right-left)+left; //选取随机主元 //把随机主元放到数组尾部 int temp = arr[randIndex]; arr[randIndex] = arr[right]; arr[right] = temp; //数组中元素与主元比较 int i = left- 1 ; //注意 for ( int j = left;j |
七、归并排序
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | package com.yzh.sort; @SuppressWarnings ({ "all" }) /* 归并排序 */ public class MergeSort { private static void mergesort( int [] a, int left, int right, int [] temp) { //分解 if (left<right int = "" mid= "((right-left)" >> 1 )+left; //向左递归进行分解 mergesort(a, left, mid, temp); //向右递归进行分解 mergesort(a, mid+ 1 , right, temp); //每分解一次便合并一次 merge(a,left,right,mid,temp); } } private static void merge( int [] a, int left, int right, int mid, int [] temp) { int i=left; //初始i,左边有序序列的初始索引 int j=mid+ 1 ; //初始化j,右边有序序列的初始索引(右边有序序列的初始位置即中间位置的后一位置) int t= 0 ; //指向temp数组的当前索引,初始为0 //先把左右两边的数据(已经有序)按规则填充到temp数组 //直到左右两边的有序序列,有一边处理完成为止 while (i</right> |
八、可处理负数的基数排序
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | package com.yzh.sort; public class RadixSort{ public static void main(String[] args) { int []a={- 2 ,- 1 ,- 6 , 3 , 5 , 1 , 2 , 88 ,- 1 , 99 , 100 , 21 }; RadixSort(a); for ( int x : a) { System.out.print(x+ " " ); } System.out.println(); } public static int [] RadixSort( int [] arr){ //最大值,用来计算需要找多少次 int max = Integer.MIN_VALUE; //用来判断是否是负数 int min = Integer.MAX_VALUE; //从该数组中找到最大最小值 for ( int i = 0 ; i |
以上就是Java实现常见的排序算法的示例代码的详细内容,更多关于Java排序算法的资料请关注IT俱乐部其它相关文章!