三大基础排序算法java实现

来源:互联网 发布:外籍模特知乎 编辑:程序博客网 时间:2024/05/17 20:59

冒泡排序——(BubbleSort)


基本思想:
    在要排序的一组数里,对当前还没有排好序的范围内的全部数,自上而下对相邻的两个数依次进行比较和调整,让较大的数往下沉,较小的往上冒。即每当两相邻的数比较后发现他们的排序与排序要求相反时,就将他们互换。
图解:
代码:
  1. package sort.change;
  2. public class BubbleSort {
  3. public static void main(String[] args) {
  4. int[] arr = {9,8,7,6,5,4,3,2,1};
  5. print(arr);
  6. bubbleSort(arr);
  7. print(arr);
  8. }
  9. private static void bubbleSort(int[] arr) {
  10. // TODO Auto-generated method stub
  11. for(int i =0;i<arr.length-1;i++){
  12. for(int j = 0;j<arr.length-1-i;j++){
  13. if(arr[j]>arr[j+1]){
  14. int temp = arr[j];
  15. arr[j] = arr[j+1];
  16. arr[j+1]=temp;
  17. }
  18. }
  19. }
  20. }
  21. private static void print(int[] arr) {
  22. // TODO Auto-generated method stub
  23. for(int o:arr){
  24. System.out.print(o+" ");
  25. }
  26. System.out.println();
  27. }
  28. }
时间复杂度:O(n^2).

插入排序——直接插入排序(Straight Insertion Sort)

基本思想:

将一个记录 插入到已经排序好的有序列表中,从而得到一个新,记录数增1的有序表。即:先将序列的第一个记录看成是一个有序的子序列,然后 从第二个记录逐个进行插入,直到整个序列有序为止。

Java代码:

  1. private static void insertSort(int[] a, int length) {
  2. // TODO Auto-generated method stub
  3. for(int i = 1;i<length;i++){
  4. if(a[i]<a[i-1]){
  5. int j = i-1;//0 3
  6. int temp = a[i];//1 2
  7. a[i] = a[i-1];// 7
  8. while(temp<a[j]){
  9. a[j+1]=a[j];
  10. j--;
  11. if(j<0)
  12. break;
  13. }
  14. a[j+1]=temp;
  15. }
  16. }
  17. for(int i =0;i<length;i++){
  18. System.out.print(a[i]+" ");
  19. }
  20. }
  1. public class InsertionSort {
  2. public static void main(String[] args) {
  3. int[] arr = {9,8,7,6,-1,0};
  4. print(arr);
  5. insortSort(arr);
  6. print(arr);
  7. }
  8. private static void insortSort(int[] arr) {
  9. // TODO Auto-generated method stub
  10. for(int i = 1;i<arr.length;i++){
  11. int temp = arr[i];
  12. int position = i;
  13. for(int j = i;j>=1&&temp<arr[j-1];j--){
  14. arr[j] = arr[j-1];
  15. position = j-1;
  16. }
  17. arr[position]= temp;
  18. }
  19. }
  20. private static void print(int[] arr) {
  21. // TODO Auto-generated method stub
  22. for(int o:arr){
  23. System.out.print(o+" ");
  24. }
  25. System.out.println();
  26. }
  27. }

如果碰到一个和插入元素相等的,那么插入元素把想插入的元素放到相等元素后面。所以,相等元素前后顺序是不变的,从原无序序列出去的顺序就是排好序后的顺序,所以插入排序是稳定的

时间复杂度:O(n^2).

简单选择排序(Select Sort)

简介:
基本思想:
    在要排序的一组数中,选出最小或者最大的一个数与第一个位置的数进行交换;然后再剩下的数当中再找出最小或者最大的与第二个位置的数交换,依次类推,直到第n-1个元素(倒数第二个数)和第n个数比较为止。
算法:
    
  1. package sort.select;
  2. /**
  3. * 选择排序
  4. * @author 帅
  5. *
  6. */
  7. public class SelectSort {
  8. public static void main(String[] args) {
  9. int[] arr = {9,8,7,6,-1,0};
  10. print(arr);
  11. selectSort(arr);
  12. print(arr);
  13. }
  14. private static void selectSort(int[] arr) {
  15. // TODO Auto-generated method stub
  16. for(int i =0;i<arr.length-1;i++){
  17. int position = i;
  18. for(int j = i+1;j<arr.length;j++){
  19. if(arr[j]<arr[position]){
  20. position=j;
  21. }
  22. }
  23. int temp = arr[position];
  24. arr[position]=arr[i];
  25. arr[i] = temp;
  26. }
  27. }
  28. private static void print(int[] arr) {
  29. // TODO Auto-generated method stub
  30. for(int o:arr){
  31. System.out.print(o+" ");
  32. }
  33. System.out.println();
  34. }
  35. }


0 0
原创粉丝点击