国外优秀网站建设公司淄博优化网站
news/
2025/9/22 20:13:35/
文章来源:
国外优秀网站建设公司,淄博优化网站,seo是什么生肖,网站上做商城可用同一域名默认为递增顺序#xff1b;注#xff1a;一下例子希望自己再次复习时#xff0c;可以用笔在纸上画画内存图。
包括有:
选择排序冒泡排序插入排序
1.选择排序
--------------------------------------选择排序---------------------------------------
1、选择排…默认为递增顺序注一下例子希望自己再次复习时可以用笔在纸上画画内存图。
包括有:
选择排序冒泡排序插入排序
1.选择排序
--------------------------------------选择排序---------------------------------------
1、选择排序(1)
选择排序的思想是每一次从待排序的数据元素中选出最小或最大的一个元素存放在序列的起始位置直到全部待排序的数据元素排完。
选择排序1
private static void selectionSort(int[] a){for(int i 0;ia.length;i){for(int j i1;ja.length;j){if( a[j] a[i]){int temp a[i];a[i] a[j];a[j] temp;}}}
}
选择排序2最优
private static void OptimizeNumSort(int[] a){int k,temp;// k and temp is not need to allocation internal storage everytime.for(int i0;ia.length;i){k i; //use k record minimum number at present ,hypothesis i is minimum./用k记录那个最小的数for(int j k1;ja.length;j){if(a[j]a[k]){k j;}}if(i ! k ){temp a[i];a[i] a[k];a[k] temp;}}
} python实现:
def selectionSort(arr):for i in range(len(arr) - 1):# 记录最小数的索引minIndex ifor j in range(i 1, len(arr)):if arr[j] arr[minIndex]:minIndex j# i 不是最小数时将 i 和最小数进行交换if i ! minIndex:arr[i], arr[minIndex] arr[minIndex], arr[i]return arr ---------------------------------------冒泡排序--------------------------------------------------
2、冒泡排序 基本思想 :
设排序表长为n从后向前或者从前向后两两比较相邻元素的值如果两者的相对次序不对(A[i-1] A[i])则交换它们其结果是将最小的元素交换到待排序序列的第一个位置我们称它为一趟冒泡。下一趟冒泡时前一趟确定的最小元素不再参与比较待排序序列减少一个元素每趟冒泡的结果把序列中最小的元素放到了序列的”最前面”。
比较相邻的元素。如果第一个比第二个大就交换他们两个。
对每一对相邻元素作同样的工作从开始第一对到结尾的最后一对。在这一点最后的元素应该会是最大的数。
针对所有的元素重复以上的步骤除了最后一个。
持续每次对越来越少的元素重复上面的步骤直到没有任何一对数字需要比较。
冒泡排序是一种简单的排序算法。它重复地走访过要排序的数列一次比较两个元素如果它们的顺序错误就把它们交换过来。走访数列的工作是重复地进行直到没有再需要交换也就是说该数列已经排序完成。这个算法的名字由来是因为越小的元素会经由交换慢慢“浮”到数列的顶端。 算法描述
比较相邻的元素。如果第一个比第二个大就交换它们两个对每一对相邻元素作同样的工作从开始第一对到结尾的最后一对这样在最后的元素应该会是最大的数针对所有的元素重复以上的步骤除了最后一个重复步骤1~3直到排序完成。private static void bubbleSort(int[] a){int len a.length;int temp;for(int i len-1; i1;i--){for(int j 0;jlen-1;j){if(a[j]a[j1]){temp a[j];a[j] a[j1];a[j1] temp;}}}
} 来个小练习对某个类的对象进行排序 对日期yeda-month-day进行排序 使用到条件运算符(?:)
public int compare(Date date){return (yeardate.year)?1:yeardate.year?-1:monthdate.month?1:monthdate.month?-1:daydate.day?1:daydate.day?-1:0;
}
详细如下 public class Test{public static void main(String[] args) {Date[] days new Date[5]; days[0] new Date(2006,5,4);days[1] new Date(2006,7,4);days[2] new Date(2008,5,4);days[3] new Date(2004,5,9);days[4] new Date(2004,5,4);for(int i0;idays.length;i ) {System.out.println(days[i]); }System.out.println(\nUp is not sort---------------------------Down after sort:\n);//bubbleSort(days);// call the mothod of bubbleSort().selectionSort(days);// call the mothod of selectionSort().for(int i0;idays.length;i ) {System.out.println(days[i]); }}/*Bubble sort and return sort a type;learn to bubbleSort thinking;every sort will get the maximum.*/public static Date[] bubbleSort(Date[] a){int len a.length;for(int ilen-1;i1;i--){for (int j 0;ji-1 ;j ) {if(a[j].compare(a[j1]) 0){Date temp a[j];a[j] a[j1];a[j1] temp;} } }return a;}/*selection sort */public static Date[] selectionSort(Date[] a){int len a.length;int k;Date temp;for(int i 0 ; ilen;i){k i;for(int j k1;jlen;j){if(a[k].compare(a[j]) 1){k j;}}if(k ! i){temp a[k];a[k] a[i];a[i] temp;}}return a;}}class Date{int year,month,day;Date(int y,int m,int d){year y;month m;day d;}/*method compare() mainly to compare two Date objects size.if ab return 1;else if ab return -1;else a b return 0;according to return value ,whether to exchange objectsquote*/public int compare(Date date){return (yeardate.year)?1:yeardate.year?-1:monthdate.month?1:monthdate.month?-1:daydate.day?1:daydate.day?-1:0;}/*remember: when print an objectsquote ,is equal to call toString() method.so have to overwrite the toString*another:you can query the API/*/public String toString(){return Year:Month:Day-- year - month - day;}
} --------------------------------插入排序--------------------------------
3、插入排序Insertion Sort
插入排序Insertion-Sort的算法描述是一种简单直观的排序算法。它的工作原理是通过构建有序序列对于未排序数据在已排序序列中从后向前扫描找到相应位置并插入。
3.1 算法描述
一般来说插入排序都采用in-place在数组上实现。具体算法描述如下
1、从第一个元素开始该元素可以认为已经被排序2、取出下一个元素在已经排序的元素序列中从后向前扫描3、如果该元素已排序大于新元素将该元素移到下一位置4、重复步骤3直到找到已排序的元素小于或者等于新元素的位置5、将新元素插入到该位置后6、重复步骤2~5。
private static void insertionSort(int[] a){System.out.println(Start....);int preIndex,current;for(int i 1 ;i a.length;i){preIndex i - 1;current a[i];while(preIndex 0 a[preIndex] current){a[preIndex 1] a[preIndex];preIndex --;}a[preIndex 1] current;}
} 参考:
(1)http://www.cnblogs.com/wuxinyan/p/8615127.html(python 十大经典排序算法)
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/910236.shtml
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!