三个简单排序算法的java实现

来源:互联网 发布:天猫化妆品销售数据 编辑:程序博客网 时间:2024/05/19 16:34
       排序算法在程序中很常见,这里给出三个简单排序算法的Java实现:冒泡排序、选择排序、插入排序,与各位共享,欢迎交流!

1.冒泡排序

      冒泡排序的核心思想是相邻 的元素比较大小,若前者比后者大,则交换位置,每次都一个在较大的元素排到后面。冒泡排序思路易解,实现简单,但其时间复杂大较大,为o(n*n),冒泡排序的Java实现的代码如下:

     

class ArrayBub {private long [] a;private int elements;int count;public ArrayBub(int max) {  //构造数组a = new long[max];elements = 0;}public void insert(long value) { //往数组中插入数字a[elements] = value;elements++;  //索引值加一}public void display() {for (int i = 0; i <a.length; i++) {System.out.print(a[i] + " ");}System.out.println();} public void bubblesort() {for(int out = elements-1; out > 1; out--) {for(int in = 0; in < out; in++) {if (a[in] > a[in + 1]) {long temp = a[in];a[in] = a[in + 1];a[in+1] = temp;count++;}}}System.out.println("交换次数"+ count);} }public class BubbleSort {public static void main(String[] args) {int maxSize = 8;ArrayBub arrayBub;arrayBub = new ArrayBub(maxSize);arrayBub.insert(55);arrayBub.insert(99);arrayBub.insert(66);arrayBub.insert(11);arrayBub.insert(44);arrayBub.insert(88);arrayBub.insert(0);arrayBub.insert(33);arrayBub.display();arrayBub.bubblesort();arrayBub.display();}}

运行结果为:

55 99 66 11 44 88 0 33
交换次数19
0 11 33 44 55 66 88 99

2.选择排序

  选择排序的核心思想是每次比较时先假定参与比较的元素中最左边的为最小值,然后拿这个不一定是最小值的最小值与后面的各个元素比较,找到最小值,并把最小值放到参与比较的元素的最左边。选择排序以左边的最小值作为排序比较的出发点,其时间复杂度和冒泡排序相同,即O(n*n), 但是选择排序交换的次数比冒泡排序少,从O(n*n)减小到O(n),选择排序的速度比冒泡快。选择排序的java实现代码如下:

  

public class SelectionSort {private long [] a;private int elements;int count;public SelectionSort() {  //构造数组a = new long[10];elements = 0;} public void insert(long value) { //往数组中插入数字a[elements] = value;elements++;  //索引值加一递增}public void display() {for (int i = 0; i <a.length; i++) {System.out.print(a[i] + " ");}System.out.println();} public void selectionsort() {int min; for(int out = 0; out < elements;out++) {min=out;for(int in = out + 1;in < elements;in++) {if (a[in] < a[min]) {min = in;}}long temp = a[out];a[out] = a[min];a[min] = temp;}}public static void main(String[] args) {SelectionSort selectionSort = new SelectionSort();selectionSort.insert(15);selectionSort.insert(89);selectionSort.insert(45);selectionSort.insert(33);selectionSort.insert(12);selectionSort.insert(55);selectionSort.insert(156);selectionSort.insert(2);selectionSort.insert(66);selectionSort.insert(-5);selectionSort.display();selectionSort.selectionsort();selectionSort.display();}}

  运行结果如下:

15 89 45 33 12 55 156 2 66 -5
-5 2 12 15 33 45 55 66 89 156

3.插入排序

    插入排序的核心思想是局部有序,非局部有序的元素与局部有序的元素比较,并排序,直到全部有序,这个过程局部有序序列不断变大。空间复杂度为O(n*n),但是比冒泡和选择排序速度快,其java实现代码如下:

public class InsertSort {private long [] a;private int elements;int count;public InsertSort() {  //构造数组a = new long[6];elements = 0;} public void insert(long value) { //往数组中插入数字a[elements] = value;elements++;  //索引值加一递增}public void display() {for (int i = 0; i <a.length; i++) {System.out.print(a[i] + " ");}System.out.println();} public void sort() {int in, out;for(out = 1;out < elements;out++) {long temp = a[out];in = out;while(in > 0 && a[in-1] >= temp) {a[in] = a[in - 1]; //大者前移--in; //小者后退}a[in] = temp;  //不能退却处既是安身地}}public static void main(String[] args) {InsertSort insertSort = new InsertSort();insertSort.insert(44);insertSort.insert(99);insertSort.insert(88);insertSort.insert(7);insertSort.insert(66);insertSort.insert(100);insertSort.display();insertSort.sort();insertSort.display();}}


运行结果如下:

44 99 88 7 66 100
7 44 66 88 99 100

 
4.三种排序算法的比较

      冒泡排序的效率很低,只有当数据量较小时有一定的应用价值。选择排序把元素交换次数降到了最低,但是比较的次数依然很大,当数据量较小,且交换数据相对于比较数据更加耗时,可以应用选择排序。在大多数情况下,当数据量较小或者基本有序时,插入排序算法是三种简单排序算法中最好的选择。至于大量数据,则快速排序通常是最快的方法。

 
原创粉丝点击