码迷,mamicode.com
首页 > 编程语言 > 详细

几种排序算法

时间:2016-07-15 00:41:41      阅读:215      评论:0      收藏:0      [点我收藏+]

标签:

 1 package other;
 2 
 3 /*
 4  * 几种排序算法
 5  */
 6 public class Sort {
 7     /* 冒泡排序 */
 8     public void maoPao(int[] arr) {
 9 
10         int tem = 0;
11         for (int i = 0; i < arr.length; i++) {
12             for (int j = 0; j < arr.length - i - 1; j++) {
13                 if (arr[j] > arr[j + 1]) {
14                     tem = arr[j];
15                     arr[j] = arr[j + 1];
16                     arr[j + 1] = tem;
17                 }
18             }
19         }
20         System.out.print("冒泡排序:");
21         for (int i = 0; i < arr.length; i++) {
22             System.out.print(arr[i] + " ");
23         }
24     }
25 
26     /* 选择排序 */
27     public void xuanZe(int[] arr) {
28 
29         for (int i = 0; i < arr.length; i++) {
30             int min = arr[i];
31             int n = i; // 最小数的索引
32             for (int j = i + 1; j < arr.length; j++) {
33                 if (arr[j] < min) { // 找出最小的数
34                     min = arr[j];
35                     n = j;
36                 }
37             }
38             arr[n] = arr[i];
39             arr[i] = min;
40 
41         }
42         System.out.print("选择排序:");
43         for (int i = 0; i < arr.length; i++) {
44             System.out.print(arr[i] + " ");
45         }
46     }
47 
48     /* 插入排序 */
49     public void chaRu(int[] arr) {
50 
51         for (int i = 1; i < arr.length; i++) {
52             int currentValue = arr[i];
53             int position = i;
54             for (int j = i - 1; j >= 0; j--) {
55                 if (arr[j] > currentValue) {
56                     arr[j + 1] = arr[j];
57                     position -= 1;
58                 } else {
59                     break;
60                 }
61             }
62 
63             arr[position] = currentValue;
64         }
65         System.out.print("插入排序:");
66         for (int i = 0; i < arr.length; i++) {
67             System.out.print(arr[i] + " ");
68         }
69     }
70 
71     public static void main(String[] args) {
72 
73         int[] array = { 43, 26, 22, 19, 74, 5, 31, 8 };
74         Sort sort = new Sort();
75 
76         sort.maoPao(array);
77         System.out.println(" ");
78 
79         sort.xuanZe(array);
80         System.out.println(" ");
81 
82         sort.chaRu(array);
83         System.out.println(" ");
84 
85     }
86 }

1.大致原理

(1)冒泡排序:相邻的两两比较,把最大的放到下面

(2)选择排序:拿一个比,把最小的放到最前面

(3)插入排序:拿一个插入已排好的序列

(4)快速排序:把大的放右边,小的放左边,依次排序

2.JAVA语言提供的排序函数

  (1)Arrays类中的sort()使用的是“经过调优的快速排序法”;

  (2)比如int[],double[],char[]等基数据类型的数组,Arrays类之只是提供了默认的升序排列,没有提供相应的降序排列方法。

  (3)要对基础类型的数组进行降序排序,需要将这些数组转化为对应的封装类数组,如Integer[],Double[],Character[]等,对这些类数组进行排序。(其实还不如先进行升序排序,自己在转为将序)。

    用默认的升序对数组排序

   函数原型:static void sort(int[] a)   对指定的 int 型数组按数字升序进行排序。

       static void sort(int[] a, int fromIndex, int toIndex)  对指定 int 型数组的指定范围按数字升序进行排序 

 

几种排序算法

标签:

原文地址:http://www.cnblogs.com/yoyohong/p/5671980.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!