• 程序员必知的8大排序(三)-------冒泡排序,快速排序(java实现)


    5.冒泡排序

    (1)基本思想:在要排序的一组数中,对当前还未排好序的范围内的全部数,自上而下对相邻的两个数依次进行比较和调整,让较大的数往下沉,较小的往上冒。即:每当两相邻的数比较后发现它们的排序与排序要求相反时,就将它们互换。

    (2)实例:

    (3)用java实现

    1. public class bubbleSort {  
    2. public  bubbleSort(){  
    3.      int a[]={49,38,65,97,76,13,27,49,78,34,12,64,5,4,62,99,98,54,56,17,18,23,34,15,35,25,53,51};  
    4.     int temp=0;  
    5.     for(int i=0;i<a.length-1;i++){  
    6.         for(int j=0;j<a.length-1-i;j++){  
    7.         if(a[j]>a[j+1]){  
    8.             temp=a[j];  
    9.             a[j]=a[j+1];  
    10.             a[j+1]=temp;  
    11.         }  
    12.         }  
    13.     }  
    14.     for(int i=0;i<a.length;i++)  
    15.         System.out.println(a[i]);     
    16. }  

    6.快速排序

    (1)基本思想:选择一个基准元素,通常选择第一个元素或者最后一个元素,通过一趟扫描,将待排序列分成两部分,一部分比基准元素小,一部分大于等于基准元素,此时基准元素在其排好序后的正确位置,然后再用同样的方法递归地排序划分的两部分。

    (2)实例:

    (3)用java实现

    [java] view plain copy
     
    1. public class quickSort {  
    2.   
    3.   inta[]={49,38,65,97,76,13,27,49,78,34,12,64,5,4,62,99,98,54,56,17,18,23,34,15,35,25,53,51};  
    4.   
    5. public quickSort(){  
    6.   
    7.     quick(a);  
    8.   
    9.     for(int i=0;i<a.length;i++)  
    10.   
    11.        System.out.println(a[i]);  
    12.   
    13. }  
    14.   
    15. public int getMiddle(int[] list, int low, int high) {     
    16.   
    17.             int tmp = list[low];    //数组的第一个作为中轴     
    18.   
    19.             while (low < high) {     
    20.   
    21.                 while (low < high && list[high] >= tmp) {     
    22.   
    23.                     high--;     
    24.   
    25.                 }     
    26.   
    27.                 list[low] = list[high];   //比中轴小的记录移到低端     
    28.   
    29.                 while (low < high && list[low] <= tmp) {     
    30.   
    31.                     low++;     
    32.   
    33.                 }     
    34.   
    35.                 list[high] = list[low];   //比中轴大的记录移到高端     
    36.   
    37.             }     
    38.   
    39.            list[low] = tmp;              //中轴记录到尾     
    40.   
    41.             return low;                   //返回中轴的位置     
    42.   
    43.         }    
    44.   
    45. public void _quickSort(int[] list, int low, int high) {     
    46.   
    47.             if (low < high) {     
    48.   
    49.                int middle = getMiddle(list, low, high);  //将list数组进行一分为二     
    50.   
    51.                 _quickSort(list, low, middle - 1);        //对低字表进行递归排序     
    52.   
    53.                _quickSort(list, middle + 1, high);       //对高字表进行递归排序     
    54.   
    55.             }     
    56.   
    57.         }   
    58.   
    59. public void quick(int[] a2) {     
    60.   
    61.             if (a2.length > 0) {    //查看数组是否为空     
    62.   
    63.                 _quickSort(a2, 0, a2.length - 1);     
    64.   
    65.         }     
    66.   
    67.        }   
    68.   
    69. }  
  • 相关阅读:
    SQL Server需要监控哪些计数器
    将表里的数据批量生成INSERT语句的存储过程 继续增强版
    [Java]
    [Linux] 安装JBoss
    [Spring]
    [Spring]
    [Maven]
    [Maven]
    [Spring MVC]
    [Spring MVC]
  • 原文地址:https://www.cnblogs.com/cyl048/p/8613677.html
Copyright © 2020-2023  润新知