首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

排序简略总结

2012-10-11 
排序简单总结public class Paixu {?public static void main(String[] args) {??int[] array { 4, 2, 7,

排序简单总结

public class Paixu {
?public static void main(String[] args) {
??int[] array = { 4, 2, 7, 8, 0 };
??
//??int[] a=bubbleSort(array);
??int[] a=selectSort(array);
//??int[] a=insertSort(array);
??for(int k:array){
???System.out.print(k);
??}
?}
?
?/*
? * 插入排序的基本方法是:每步将一个待排序的对象,按其关键码大小,插入到前面已经排好序的一组对象的适当位置上,直到对象全部插入为止。
? */
?public static int[] insertSort(int[] a){
??int len=a.length;
??for(int i=0;i<len;i++){
???for(int j=i;j>0;j--){
????if(a[j]<a[j-1]){
?????int temp=a[j-1];
?????a[j-1]=a[j];
?????a[j]=temp;
????}else{
?????break;
????}
???}
??}
??return a;
?}

?/*

? *冒泡排序
? * 1、第一趟:首先将第一个记录的关键字和第二个记录的关键字进行比较,若为逆序,则将两个记录交换之,然后比较第二个记录和第三个记录的关键字。直至第n-1个记录和第n个记录的关键字进行过比较为止。
然后进行第二趟起泡排序,对前n-1个记录进行同样操作。
...直到在某趟排序过程中没有进行过交换记录的操作为止。

? */
?public static int[] bubbleSort(int[] a) {
??int len = a.length;
??for (int i = 0; i < len-1; i++) {
???for (int j = 0; j < len - 1-i; j++) {
????if (a[j] > a[j + 1]) {
?????int temp = a[j + 1];
?????a[j + 1] = a[j];
?????a[j] = temp;
????}
???}
??}
??return a;
?}
?
?/*选择排序:选择排序的基本思想是:每一趟 (例如第 i 趟,i = 0, 1, …, n-2) 在后面 n-i 个待排序对象中选出关键码最小的对象,
?作为有序对象序列的第 i 个对象。待到第 n-2 趟作完,待排序对象只剩下1个,就不用再选了。
?*/
?public static int[] selectSort(int [] a){
??int len=a.length;
??for(int i=0;i<len-1;i++){
???for(int j=i;j<len;j++){
????if(a[j]<a[i]){
?????int temp=a[j];
?????a[j]=a[i];
?????a[i]=temp;
????}
???}
??}
??return a;
?}
}

热点排行