初识三种常见的排序算法,选择,冒泡,快速。

  由于刚学Java不久,所学知识有限,所以我只能谈谈我自己对于这三种算法的初步见解,不足之处请各位大佬不吝指教。

  在这最近的学习中我了解到了选择,冒泡,快速三种排序算法。在这三种算法中我认为比较简单易懂的是选择排序和冒泡排序。所以首先我先来讲一下我对这两种排序算法的一点理解。

  1.选择排序:

  选择排序(Selection sort)是一种简单直观的排序算法。它的工作原理是每一次从待排序的数据元素中选出最小(或最大)的一个元素,存放在序列的起始位置,直到全部待排序的数据元素排完。如下图所示:

  

从第一个数组元素开始,分别与后面的数组元素相比较,当一轮比完后将最值(最大值或最小值)放在0号角标的位置,然后第二轮从1号角标开始,重复第一轮的动作。之后的步骤与之类似。关于选择排序我写了一个示例:

  

 1 package algorithms;
 2 /**
 3  * 选择排序
 4  * @author 晓煜
 5  *
 6  */
 7 public class select {
 8 
 9     public static void main(String[] args) {
10         // TODO Auto-generated method stub
11         int []number=new int[10];//定义一个整型数组长度为10
12         for (int i = 0; i < 10; i++) {//循环10次
13             number[i]=(int)(Math.random()*100);//产生10个0~100的随机数依次放入数组
14             System.out.println(number[i]);//打印出没排序之前的数组
15         }
16         for (int i = 0; i < number.length; i++) {
17             for (int j = i+1; j < number.length; j++) {
18                 if (number[i]>number[j]) {//比较数组元素
19                     int max=number[i];//从小到大排序
20                     number[i]=number[j];
21                     number[j]=max;
22                 }
23             }
24         }
25         for (int i = 0; i < number.length; i++) {
26             System.out.print(number[i]+"  ");//打印排序后的数组
27         }
28     }
29 
30 }

运行结果如下:

 

 2.冒泡排序:

  

冒泡排序(Bubble Sort),是一种计算机科学领域的较简单的排序算法。

它重复地走访过要排序的数列,一次比较两个元素,如果他们的顺序错误就把他们交换过来。走访数列的工作是重复地进行直到没有再需要交换,也就是说该数列已经排序完成。

这个算法的名字由来是因为越大的元素会经由交换慢慢“浮”到数列的顶端,故名。

  如下图所示:

  

    代码示例:

  

 1 public class bubbling {
 2 
 3     public static void main(String[] args) {
 4         // TODO Auto-generated method stub
 5         int []number=new int[10];
 6         for (int i = 0; i < 10; i++) {
 7             number[i]=(int)(Math.random()*100);
 8             System.out.println(number[i]);
 9         }
10         for (int i = 0; i < number.length; i++) {
11             for (int j = 0; j < number.length-i-1; j++) {
12                 if (number[j]>number[j+1]) {
13                     int max=number[j];
14                     number[j]=number[j+1];
15                     number[j+1]=max;
16                 }
17             }
18         }
19         for (int i = 0; i < number.length; i++) {
20             System.out.print(number[i]+"  ");
21         }
22     }
23 
24 }

  因为这个代码的形式和上面的选择排序没有多大区别,所以就不详细介绍注释了。

  运行结果如下

  3.快速排序:

  快速排序因为其比较方式与上面的两种算法要复杂许多,所以不是那么的好理解,先放上基本的概念:

  快速排序(Quicksort)是对冒泡排序的一种改进。

  它的基本思想是:通过一趟排序将要排序的数据分割成独立的两部分,其中一部分的所有数据都比另外一部分的所有数据都要小,然后再按此方法对这两部分数据分别进行快速排序,整个排序过程可以递归进行,以此达到整个数据变成有序序列。

  概念还是不如图好理解所以我抠了张比较好理解的图过来:

  

  代码示例:

  

 1 package algorithms;
 2 
 3 public class QuickSort { 
 4       public static void main(String[] args) { 
 5 //          long start=System.currentTimeMillis();
 6           int []nums=new int[10];
 7             for (int i = 0; i < nums.length; i++) {
 8                 nums[i]=(int)(Math.random()*100);
 9                 System.out.print(nums[i]+"  ");
10             }
11         
12         System.out.println("  ");
13             //应用快速排序方法 
14         quickSort(nums, 0, nums.length-1); 
15 //        显示排序后的数组 
16         for(int i = 0; i < nums.length; ++i) { 
17           System.out.print(nums[i] + ","); 
18         } 
19         System.out.println(""); 
20         System.out.println("  ");
21 //        long end=System.currentTimeMillis();//结束时间
22 //        System.out.println("毫秒时间:"+(end-start));//运行时间
23       } 
24       
25       /**快速排序方法*/ 
26       public static void  quickSort(int[] a, int lo0, int hi0) { 
27         int lo = lo0;    //相当于i,左 
28         int hi = hi0;    //相当于j,  右 
29         if (lo >= hi)   // 判断是否到中间了 
30           return; 
31         //确定指针方向的逻辑变量,也就是从左搜索还是向右搜索 
32         boolean transfer=true; 
33         
34         while (lo != hi) { 
35           if (a[lo] > a[hi]) { 
36             //交换数字 
37             int temp = a[lo]; 
38             a[lo] = a[hi]; 
39             a[hi] = temp; 
40             //决定下标移动,还是上标移动 
41             transfer = (transfer == true) ? false : true; 
42           } 
43           //将指针向前或者向后移动 
44           if(transfer) 
45             hi--; 
46           else 
47             lo++; 
48           //显示每一次指针移动的数组数字的变化 
49          
50           for(int i = 0; i < a.length; ++i) { 
51             System.out.print(a[i] + ","); 
52           } 
53           System.out.print(" (lo,hi) = " + "(" + lo + "," + hi + ")"); 
54           System.out.println("");
55         } 
56         //将数组分开两半,确定每个数字的正确位置 
57         lo--; 
58         hi++; 
59         quickSort(a, lo0, lo); 
60         quickSort(a, hi, hi0);
61       } 
62     } 

  代码运行效果:

以上是整个排序的过程,lo,hi代表的是两个角标,每次都取第一位数作为基数。

如果需要了解这三种算法的排序的效率,并自己亲自实验一下可以使用以下简单的测量运行时间的小程序:

 1 package algorithms;
 2 
 3 public class getTime {
 4 
 5     public static void main(String[] args) {
 6         // TODO Auto-generated method stub
 7         
 8     }
 9 
10 }
11 abstract  class time{//获取程序运行时间
12     public  final void GetTime(){
13         long start=System.currentTimeMillis();//开始时间
14         Run();
15         long end=System.currentTimeMillis();//结束时间
16         System.out.println("毫秒时间:"+(start-end));//运行时间
17     }
18     public abstract void Run();
19         
20 }

以上是我第一次水博客,请各位原谅我的失误,与不足。

学习java时间不长,多多包涵。

原文地址:https://www.cnblogs.com/Correspondents/p/8059879.html