zoukankan      html  css  js  c++  java
  • 排序算法总结

    1.冒泡排序法

    比较相邻的元素,把最小的排在前面。时间复杂度为O(n^2)

     1 package com.xiao.day01;
     2 
     3 /**
     4  * 冒泡排序法
     5  * 
     6  *
     7  */
     8 public class BubbleSort {
     9 
    10     public static void bubbleSort(int[] arr){
    11         if(arr == null || arr.length == 0){
    12             return ;
    13         }
    14         for(int i=0; i<arr.length-1; i++){
    15             for(int j=arr.length-1; j>i; j--){//从数组的最后一个元素往前比较
    16                 if(arr[j] < arr[j-1]){
    17                     swap(arr, j-1, j);
    18                 }
    19             }
    20             /*for(int j=0;j<=arr.length-1-i; j++){//从数组的第一个元素向后比较
    21                 if(arr[j] > arr[j+1]){
    22                     swap(arr, j, j+1);
    23                 }
    24             }*/
    25         }
    26     }
    27 
    28     private static void swap(int[] arr, int i, int j) {
    29         int temp = arr[i];
    30         arr[i] = arr[j];
    31         arr[j] = temp;
    32     }
    33 }

     2.选择排序

    冒泡排序是通过相邻的比较和交换,而选择排序是通过对整体的选择,可能交换的次数少。

     1 package com.xiao.day01;
     2 
     3 public class SelectSort {
     4 
     5     public static void selectSort(int[] arr){
     6         if(arr == null || arr.length == 0){
     7             return ;
     8         }
     9         int minIndex = 0;
    10         for(int i=0; i<arr.length-1; i++){
    11             minIndex = i;
    12             for(int j=i+1; j<arr.length; j++){
    13                 if(arr[j] < arr[minIndex]){
    14                     minIndex = j;//下标之间的交换
    15                 }
    16             }
    17             if(minIndex != i){
    18                 swap(arr, i, minIndex);
    19             }
    20         }
    21     }
    22     
    23     private static void swap(int[] arr, int i, int j) {
    24         int temp = arr[i];
    25         arr[i] = arr[j];
    26         arr[j] = temp;
    27     }
    28 }

    3.插入排序(参考网址

    通过构建有序序列,对于未排序数据,在已排序序列中从后向前扫描,找到相应的位置并插入

    4.快速排序

    采用“分而治之”,把大的拆分为小的,小的拆分为更小的

    对于给定的一组记录,选择一个基准元素,通常选择第一个元素或者最后一个元素,通过一趟扫描,将待排序列分成两部分,一部分比基准元素小,一部分大于等于基准元素,此时基准元素在其排好序后的正确位置,然后再用同样的方法递归地排序划分的两部分,直到序列中的所有记录均有序为止。

     1 /**
     2  * 快速排序法
     3  *
     4  */
     5 public class QuickSort {
     6 
     7     public static void sort(int[] a , int low, int hight){
     8         int i, j, index;
     9         if( low > hight){
    10             return ;
    11         }
    12         i = low;
    13         j = hight;
    14         index = a[i];//用子表的第一个作为基准
    15         while(i < j){//从表的两端交替向中间扫描
    16             while(i < j && a[j] >= index){
    17                 j--;
    18             }
    19             if(i < j){
    20                 a[i++] = a[j];
    21             }
    22             while(i < j && a[i] < index){
    23                 i++;
    24             }
    25             if(i < j){
    26                 a[j--] = a[i];
    27             }
    28         }
    29         a[i] = index;
    30         sort(a, low, i-1);
    31         sort(a, i+1, hight);
    32     }
    33     
    34     public static void quickSort(int[] a){
    35         sort(a, 0, a.length-1);
    36     }
    37     
    38     public static void main(String[] args) {
    39         int a[] = { 49, 38, 65, 97, 76, 13, 27, 49};
    40         quickSort(a);
    41         System.out.println(Arrays.toString(a));
    42     }
    43 }
  • 相关阅读:
    Bufferedreader和BufferedWriter(带缓存的输入输出)
    FileReader和FileWriter
    Map接口的类实现
    Map接口
    Set集合
    List接口的实现类
    获取文本框或密码框中的内容
    ADTS (zz)
    初级美语 L003:My Family 解析
    初级美语 L001:Self Introduction 解析
  • 原文地址:https://www.cnblogs.com/xiaozhijing/p/8485048.html
Copyright © 2011-2022 走看看