常见排序——Java实现

 1 package struct;
 2  
 3 /**
 4  * 
 5  * @作者:dyy
 6  * @公司:陕西科技大学
 7  * @修改日期:
 8  * @邮箱:1101632375@qq.com
 9  * @描述:Java实现几种常见排序
10  */
11  
12  
13 //选择排序类
14 class SelectSort{
15     public void selectSort(int[] arr){
16         for(int i = 0;i < arr.length; i++){
17             int currentMax = i;//记录当前的最大值下标
18             for(int j = i + 1;j < arr.length;j++){
19                 //找到最大值下标
20                 if(arr[j] > arr[currentMax]){
21                     currentMax = j;
22                 }
23             }
24             int temp = arr[i];
25             arr[i] = arr[currentMax];
26             arr[currentMax] = temp;
27         }
28     }
29     public void print(int[] arr){
30         for(int i = 0;i<arr.length;i++){
31             System.out.print(arr[i]+" ");
32         }
33     }
34 }
35  
36  
37 //冒泡排序类
38 class BubbleSort{
39         //冒泡排序的实现
40         public void bubbleSort(int[] arr){
41             for(int i = 0;i < arr.length - 1;i++){
42                 for(int j = 0;j < arr.length - 1 - i;j++){
43                     if(arr[j] < arr[j+1]){
44                         int temp = arr[j];
45                         arr[j] = arr[j+1];
46                         arr[j+1] = temp;
47                     }
48                 }
49             }
50         }
51         //打印数组
52         public void print(int[] a){
53             for(int i =0 ;i<a.length;i++){
54                 System.out.print(a[i]+" ");
55             }
56         }
57 }
58  
59  
60 //插入排序
61 class InsertSort{
62     public void insertSort(int[] arr){
63         for(int i = 0; i < arr.length - 1;i++){
64             //将第一个元素当作排好序的
65             int j;
66             int insert = arr[i];
67             for(j = i;j > 0 && insert>arr[j-1];j--){
68                 arr[j] = arr[j-1];
69             }
70             arr[j] = insert;
71         }
72     }
73     
74     //打印数组
75             public void print(int[] a){
76                 for(int i =0 ;i<a.length;i++){
77                     System.out.print(a[i]+" ");
78                 }
79             }
80 }
View Code
 1 public class TestVeriousSort {
 2     public static void main(String[] args) {
 3         int[] arr1 = {13,2,6,34,1,4,9,7,5};
 4         //选择排序
 5         System.out.println("选择排序"+"\n");
 6         SelectSort obj = new SelectSort();
 7         System.out.println("初始的数组:");
 8         obj.print(arr1);
 9         System.out.println("\n"+"排序后的数组:");
10         obj.selectSort(arr1);
11         obj.print(arr1);
12         
13         //冒泡排序
14         System.out.println("\n"+"冒泡排序"+"\n");
15         BubbleSort obj1 = new BubbleSort();
16         System.out.println("初始的数组:");
17         obj1.print(arr1);
18         System.out.println("\n"+"排序后的数组:");
19         obj1.bubbleSort(arr1);
20         obj1.print(arr1);
21         
22         //插入排序
23         System.out.println("\n"+"冒泡排序"+"\n");
24         InsertSort obj2 = new InsertSort();
25         System.out.println("初始的数组:");
26         obj2.print(arr1);
27         System.out.println("\n"+"排序后的数组:");
28         obj2.insertSort(arr1);
29         obj2.print(arr1);
30     }
31 }
View Code
原文地址:https://www.cnblogs.com/edda/p/12595050.html