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

    一、冒泡排序

    1. package sort.bubble;   
    2.   
    3. import java.util.Random;   
    4. /**  
    5.  * 依次比较相邻的两个数,将小数放在前面,大数放在后面  
    6.  * 冒泡排序,具有稳定性  
    7.  * 时间复杂度为O(n^2)  
    8.  * 不及堆排序,快速排序O(nlogn,底数为2)  
    9.  * @author liangge  
    10.  *  
    11.  */  
    12. public class Main {   
    13.     public static void main(String[] args) {   
    14.         Random ran = new Random();   
    15.         int[] sort = new int[10];   
    16.         for(int i = 0 ; i < 10 ; i++){   
    17.             sort[i] = ran.nextInt(50);   
    18.         }   
    19.         System.out.print(“排序前的数组为”);   
    20.         for(int i : sort){   
    21.             System.out.print(i+“ ”);   
    22.         }   
    23.         buddleSort(sort);   
    24.         System.out.println();   
    25.         System.out.print(“排序后的数组为”);   
    26.         for(int i : sort){   
    27.             System.out.print(i+“ ”);   
    28.         }   
    29.     }   
    30.        
    31.     /**  
    32.      * 冒泡排序  
    33.      * @param sort  
    34.      */  
    35.     private static void buddleSort(int[] sort){   
    36.         for(int i=1;i<sort.length;i++){   
    37.             for(int j=0;j<sort.length-i;j++){   
    38.                 if(sort[j]>sort[j+1]){   
    39.                     int temp = sort[j+1];   
    40.                     sort[j+1] = sort[j];   
    41.                     sort[j] = temp;   
    42.                 }   
    43.             }   
    44.         }   
    45.     }   
    46. }  
    package sort.bubble; import java.util.Random; /** * 依次比较相邻的两个数,将小数放在前面,大数放在后面 * 冒泡排序,具有稳定性 * 时间复杂度为O(n^2) * 不及堆排序,快速排序O(nlogn,底数为2) * @author liangge * */ public class Main { public static void main(String[] args) { Random ran = new Random(); int[] sort = new int[10]; for(int i = 0 ; i < 10 ; i++){ sort[i] = ran.nextInt(50); } System.out.print("排序前的数组为"); for(int i : sort){ System.out.print(i+" "); } buddleSort(sort); System.out.println(); System.out.print("排序后的数组为"); for(int i : sort){ System.out.print(i+" "); } } /** * 冒泡排序 * @param sort */ private static void buddleSort(int[] sort){ for(int i=1;i<sort.length;i++){ for(int j=0;j<sort.length-i;j++){ if(sort[j]>sort[j+1]){ int temp = sort[j+1]; sort[j+1] = sort[j]; sort[j] = temp; } } } } } 

    二、选择排序

    1. package sort.select;   
    2.   
    3. import java.util.Random;   
    4.   
    5. /**  
    6.  * 选择排序  
    7.  * 每一趟从待排序的数据元素中选出最小(或最大)的一个元素,  
    8.  * 顺序放在已排好序的数列的最后,直到全部待排序的数据元素排完。   
    9.  * 选择排序是不稳定的排序方法。  
    10.  * @author liangge  
    11.  *   
    12.  */  
    13. public class Main {   
    14.     public static void main(String[] args) {   
    15.         Random ran = new Random();   
    16.         int[] sort = new int[10];   
    17.         for (int i = 0; i < 10; i++) {   
    18.             sort[i] = ran.nextInt(50);   
    19.         }   
    20.         System.out.print(“排序前的数组为”);   
    21.         for (int i : sort) {   
    22.             System.out.print(i + “ ”);   
    23.         }   
    24.         selectSort(sort);   
    25.         System.out.println();   
    26.         System.out.print(“排序后的数组为”);   
    27.         for (int i : sort) {   
    28.             System.out.print(i + “ ”);   
    29.         }   
    30.     }   
    31.   
    32.     /**  
    33.      * 选择排序  
    34.      * @param sort  
    35.      */  
    36.     private static void selectSort(int[] sort){   
    37.         for(int i =0;i<sort.length-1;i++){   
    38.             for(int j = i+1;j<sort.length;j++){   
    39.                 if(sort[j]<sort[i]){   
    40.                     int temp = sort[j];   
    41.                     sort[j] = sort[i];   
    42.                     sort[i] = temp;   
    43.                 }   
    44.             }   
    45.         }   
    46.     }   
    47. }  
    package sort.select; import java.util.Random; /** * 选择排序 * 每一趟从待排序的数据元素中选出最小(或最大)的一个元素, * 顺序放在已排好序的数列的最后,直到全部待排序的数据元素排完。 * 选择排序是不稳定的排序方法。 * @author liangge * */ public class Main { public static void main(String[] args) { Random ran = new Random(); int[] sort = new int[10]; for (int i = 0; i < 10; i++) { sort[i] = ran.nextInt(50); } System.out.print("排序前的数组为"); for (int i : sort) { System.out.print(i + " "); } selectSort(sort); System.out.println(); System.out.print("排序后的数组为"); for (int i : sort) { System.out.print(i + " "); } } /** * 选择排序 * @param sort */ private static void selectSort(int[] sort){ for(int i =0;i<sort.length-1;i++){ for(int j = i+1;j<sort.length;j++){ if(sort[j]<sort[i]){ int temp = sort[j]; sort[j] = sort[i]; sort[i] = temp; } } } } } 

    三、快速排序

    1. package sort.quick;   
    2.   
    3. /**  
    4.  * 快速排序 通过一趟排序将要排序的数据分割成独立的两部分, 其中一部分的所有数据都比另外一部分的所有数据都要小,  
    5.  * 然后再按此方法对这两部分数据分别进行快速排序, 整个排序过程可以递归进行,以此达到整个数据变成有序序列。  
    6.  * @author liangge  
    7.  *   
    8.  */  
    9. public class Main {   
    10.     public static void main(String[] args) {   
    11.         int[] sort = { 5431893366126820 };   
    12.         System.out.print(“排序前的数组为:”);   
    13.         for (int data : sort) {   
    14.             System.out.print(data + “ ”);   
    15.         }   
    16.         System.out.println();   
    17.         quickSort(sort, 0, sort.length - 1);   
    18.         System.out.print(“排序后的数组为:”);   
    19.         for (int data : sort) {   
    20.             System.out.print(data + “ ”);   
    21.         }   
    22.     }   
    23.   
    24.     /**  
    25.      * 快速排序  
    26.      * @param sort 要排序的数组  
    27.      * @param start 排序的开始座标  
    28.      * @param end 排序的结束座标  
    29.      */  
    30.     public static void quickSort(int[] sort, int start, int end) {   
    31.         // 设置关键数据key为要排序数组的第一个元素,   
    32.         // 即第一趟排序后,key右边的数全部比key大,key左边的数全部比key小   
    33.         int key = sort[start];   
    34.         // 设置数组左边的索引,往右移动判断比key大的数   
    35.         int i = start;   
    36.         // 设置数组右边的索引,往左移动判断比key小的数   
    37.         int j = end;   
    38.         // 如果左边索引比右边索引小,则还有数据没有排序   
    39.         while (i < j) {   
    40.             while (sort[j] > key && j > start) {   
    41.                 j–;   
    42.             }   
    43.             while (sort[i] < key && i < end) {   
    44.                 i++;   
    45.             }   
    46.             if (i < j) {   
    47.                 int temp = sort[i];   
    48.                 sort[i] = sort[j];   
    49.                 sort[j] = temp;   
    50.             }   
    51.         }   
    52.         // 如果左边索引比右边索引要大,说明第一次排序完成,将sort[j]与key对换,   
    53.         // 即保持了key左边的数比key小,key右边的数比key大   
    54.         if (i > j) {   
    55.             int temp = sort[j];   
    56.             sort[j] = sort[start];   
    57.             sort[start] = temp;   
    58.         }   
    59.         //递归调用   
    60.         if (j > start && j < end) {   
    61.             quickSort(sort, start, j - 1);   
    62.             quickSort(sort, j + 1, end);   
    63.         }   
    64.     }   
    65. }  
    package sort.quick; /** * 快速排序 通过一趟排序将要排序的数据分割成独立的两部分, 其中一部分的所有数据都比另外一部分的所有数据都要小, * 然后再按此方法对这两部分数据分别进行快速排序, 整个排序过程可以递归进行,以此达到整个数据变成有序序列。 * @author liangge * */ public class Main { public static void main(String[] args) { int[] sort = { 54, 31, 89, 33, 66, 12, 68, 20 }; System.out.print("排序前的数组为:"); for (int data : sort) { System.out.print(data + " "); } System.out.println(); quickSort(sort, 0, sort.length - 1); System.out.print("排序后的数组为:"); for (int data : sort) { System.out.print(data + " "); } } /** * 快速排序 * @param sort 要排序的数组 * @param start 排序的开始座标 * @param end 排序的结束座标 */ public static void quickSort(int[] sort, int start, int end) { // 设置关键数据key为要排序数组的第一个元素, // 即第一趟排序后,key右边的数全部比key大,key左边的数全部比key小 int key = sort[start]; // 设置数组左边的索引,往右移动判断比key大的数 int i = start; // 设置数组右边的索引,往左移动判断比key小的数 int j = end; // 如果左边索引比右边索引小,则还有数据没有排序 while (i < j) { while (sort[j] > key && j > start) { j--; } while (sort[i] < key && i < end) { i++; } if (i < j) { int temp = sort[i]; sort[i] = sort[j]; sort[j] = temp; } } // 如果左边索引比右边索引要大,说明第一次排序完成,将sort[j]与key对换, // 即保持了key左边的数比key小,key右边的数比key大 if (i > j) { int temp = sort[j]; sort[j] = sort[start]; sort[start] = temp; } //递归调用 if (j > start && j < end) { quickSort(sort, start, j - 1); quickSort(sort, j + 1, end); } } } 

    四、插入排序

    1. package sort.insert;   
    2.   
    3. /**  
    4.  * 直接插入排序  
    5.  * 将一个数据插入到已经排好序的有序数据中,从而得到一个新的、个数加一的有序数据  
    6.  * 算法适用于少量数据的排序,时间复杂度为O(n^2)。是稳定的排序方法。  
    7.  */  
    8. import java.util.Random;   
    9.   
    10. public class DirectMain {   
    11.     public static void main(String[] args) {   
    12.         Random ran = new Random();   
    13.         int[] sort = new int[10];   
    14.         for (int i = 0; i < 10; i++) {   
    15.             sort[i] = ran.nextInt(50);   
    16.         }   
    17.         System.out.print(“排序前的数组为”);   
    18.         for (int i : sort) {   
    19.             System.out.print(i + “ ”);   
    20.         }   
    21.         directInsertSort(sort);   
    22.         System.out.println();   
    23.         System.out.print(“排序后的数组为”);   
    24.         for (int i : sort) {   
    25.             System.out.print(i + “ ”);   
    26.         }   
    27.     }   
    28.   
    29.     /**  
    30.      * 直接插入排序  
    31.      *   
    32.      * @param sort  
    33.      */  
    34.     private static void directInsertSort(int[] sort) {   
    35.         for (int i = 1; i < sort.length; i++) {   
    36.             int index = i - 1;   
    37.             int temp = sort[i];   
    38.             while (index >= 0 && sort[index] > temp) {   
    39.                 sort[index + 1] = sort[index];   
    40.                 index–;   
    41.             }   
    42.             sort[index + 1] = temp;   
    43.   
    44.         }   
    45.     }   
    46. }  
    package sort.insert; /** * 直接插入排序 * 将一个数据插入到已经排好序的有序数据中,从而得到一个新的、个数加一的有序数据 * 算法适用于少量数据的排序,时间复杂度为O(n^2)。是稳定的排序方法。 */ import java.util.Random; public class DirectMain { public static void main(String[] args) { Random ran = new Random(); int[] sort = new int[10]; for (int i = 0; i < 10; i++) { sort[i] = ran.nextInt(50); } System.out.print("排序前的数组为"); for (int i : sort) { System.out.print(i + " "); } directInsertSort(sort); System.out.println(); System.out.print("排序后的数组为"); for (int i : sort) { System.out.print(i + " "); } } /** * 直接插入排序 * * @param sort */ private static void directInsertSort(int[] sort) { for (int i = 1; i < sort.length; i++) { int index = i - 1; int temp = sort[i]; while (index >= 0 && sort[index] > temp) { sort[index + 1] = sort[index]; index--; } sort[index + 1] = temp; } } } 

    五、顺便贴个二分搜索法

    1. package search.binary;   
    2.   
    3. public class Main {   
    4.     public static void main(String[] args) {   
    5.         int[] sort = {1,2,3,4,5,6,7,8,9,10};   
    6.         int mask = binarySearch(sort,6);   
    7.         System.out.println(mask);   
    8.            
    9.     }   
    10.        
    11.        
    12.     /**  
    13.      * 二分搜索法,返回座标,不存在返回-1  
    14.      * @param sort  
    15.      * @return  
    16.      */  
    17.     private static int binarySearch(int[] sort,int data){   
    18.         if(data<sort[0] || data>sort[sort.length-1]){   
    19.             return -1;   
    20.         }   
    21.         int begin = 0;   
    22.         int end = sort.length;   
    23.         int mid = (begin+end)/2;   
    24.         while(begin <= end){   
    25.             mid = (begin+end)/2;   
    26.             if(data > sort[mid]){   
    27.                 begin = mid + 1;   
    28.             }else if(data < sort[mid]){   
    29.                 end = mid - 1;   
    30.             }else{   
    31.                 return mid;   
    32.             }   
    33.         }   
    34.         return -1;   
    35.            
    36.     }   
    37. }  

    尤其是冒泡算法,我建议面试者都能背下来,因为面试中被提到的概率是比较多的。

    本文链接:http://www.jfox.info/459, 转载请保留.

  • 相关阅读:
    怎样理解HTMLCollection接口
    怎样单独遍历NodeList的键、值和键值对
    怎样获取NodeList某位置上的节点
    怎样遍历NodeList对象
    怎样理解NodeList的动态集合与静态集合
    怎样将类似数组的对象转换为数组
    怎样理解 instanceof
    怎样清理当前节点下的所有文本节点
    怎样移除当前节点
    怎样判断一个节点是否相等 / 是否相同
  • 原文地址:https://www.cnblogs.com/wuxiang/p/3541348.html
Copyright © 2011-2022 走看看