简单排序的三种方式
简单排序
1,冒泡排序
package com.aluo.structure.three; public class ArrayBub { private long[] a; private int numberElements; public ArrayBub(int max) { a = new long[max]; numberElements = 0; } public void insert(long value) { a[numberElements] = value; numberElements++; } public void display() { for (int i = 0; i < numberElements; i++) { System.out.print(a[i]); System.out.print(" "); } } //冒泡算法 public void bubbleSort() { int outer, in; for (outer = numberElements - 1; outer > 1; outer--) { for (in = 0; in < outer; in++) { if (a[in] > a[in + 1]) { swap(in, in + 1); } } } } private void swap(int one, int two) { long temp; temp = a[one]; a[one] = a[two]; a[two] = temp; } }
2,选择排序
package com.aluo.structure.three; public class ArraySelect { private long[] a; private int numberElements; public ArraySelect(int max) { a = new long[max]; numberElements = 0; } public void insert(long value) { a[numberElements] = value; numberElements++; } public void display() { for (int i = 0; i < numberElements; i++) { System.out.print(a[i]); System.out.print(" "); } } //选择算法 public void selectSort() { int first, second ,min; for (first = 0; first < numberElements - 1; first++ ){ min = first; for (second = first + 1; second < numberElements; second++){ if (a[second] < a[min]){ min = second; } } swap(first,min); } } private void swap(int one, int two) { long temp; temp = a[one]; a[one] = a[two]; a[two] = temp; } }
3,插入排序
package com.aluo.structure.three; public class ArrayInsert { private long[] a; private int numberElements; public ArrayInsert(int max) { a = new long[max]; numberElements = 0; } public void insert(long value) { a[numberElements] = value; numberElements++; } public void display() { for (int i = 0; i < numberElements; i++) { System.out.print(a[i]); System.out.print(" "); } } //插入排序算法 public void insertSort() { int in , out; for (out = 1; out < numberElements; out++){ long temp = a[out]; in = out; while (in > 0 && a[in -1] >= temp){ a[in] = a[in-1]; --in; } a[in] = temp; // this.display(); // System.out.println(); } } private void swap(int one, int two) { long temp; temp = a[one]; a[one] = a[two]; a[two] = temp; } }
4,测试类,测试几种算法的运行情况
package com.aluo.structure.three; public class Test { public static void main(String[] args) { int initial = 100000; ArrayBub arrayBub = new ArrayBub(initial); ArraySelect arraySelect = new ArraySelect(initial); ArrayInsert arrayInsert = new ArrayInsert(initial); for (int i = 0; i < initial; i++) { arrayBub.insert((int) (Math.random() * 100)); arraySelect.insert((int) (Math.random() * 100)); arrayInsert.insert((int) (Math.random() * 100)); } long start = System.currentTimeMillis(); arrayBub.bubbleSort(); long end = System.currentTimeMillis(); System.out.println("bubble :" + (end - start) + "ms"); start = System.currentTimeMillis(); arraySelect.selectSort(); end = System.currentTimeMillis(); System.out.println("select:" + (end - start) + "ms"); start = System.currentTimeMillis(); arrayInsert.insertSort(); end = System.currentTimeMillis(); System.out.println("insert:" + (end - start) + "ms"); } }

initial 改为300000

bubble的执行时间是其他两种方式的五倍长。select和insert时间上差不多。

浙公网安备 33010602011771号