今天无聊的写了几行简单排序算法,可以参考一下。。。
简单介绍一下简单排序算法:
1.冒泡排序:0~N-1一次比较,如果左边的>右边那么Swap。然后依次向右移动一次继续进行比较。
第一趟最多比较N-1,第二趟最多比较N-2以此类推最后最多比较1次结束。
对于10个数据就是9+8+7+6+5+4+3+2+1=45次。Big O=N的2次方。
2.选择排序:选择一个最小的数字,与最左端的交换位置。
第一趟选择最小的放到索引为0的位置,第二趟放到索引为1的位置以此类推直到最后一个。
交换次数Big O=N,比较次数Big O=N的2次方。
3.插入排序:一般用于局部有序的情况。选择一个位置(1),一次向左比较是否大于选择的数字并且满足当前索引大于0,
在比较时满足条件那么向右移动补空洞。然后把选择的数字插入合适的位置。具体如下:
以下是main方法
MyArrary array = new MyArrary(10); array.unorderedInsert(10); array.unorderedInsert(9); array.unorderedInsert(8); array.unorderedInsert(7); array.unorderedInsert(6); array.display(); System.out.println("-------Bubble Sort"); array.bubbleSort(); array.display(); System.out.println("-------Ordered Insert"); int value = 0; while (!array.isPull()) { array.orderedInsert(++value); } array.display(); System.out.println("-------Delete By Index 4"); System.out.println("Delete:" + array.deleteByIndex(4)); array.display(); System.out.println("-------Delete By Value 1"); System.out.println("Delete:" + array.deleteByValue(1)); array.display(); System.out.println("-------Unordered Insert value 1"); array.unorderedInsert(1); array.display(); System.out.println("-------Insertion Sort"); array.insertionSort(); array.display(); System.out.println("-------Unordered Insert value 5"); array.unorderedInsert(5); array.display(); System.out.println("-------Selection Sort"); array.selectionSort(); array.display(); System.out.println("-------Find Value 3 By Binary"); System.out.println("Find Index: "+array.findByBinary(3)); System.out.println("-------Find Value 3 By Normal"); System.out.println("Find Index: "+array.findByBinary(3)); System.out.println("-------Delete ALL"); while (!array.isEmpty()) { array.deleteByIndex(0); } System.out.println("Is Empty: " + array.isEmpty());
以下是MyArrary 类
public class MyArrary { private long[] arry; private int elems; public MyArrary(int size) { arry = new long[size]; elems = 0; } public void unorderedInsert(long value) { arry[elems++] = value; } public void orderedInsert(long value) { int i = 0; for (; i < elems; i++) if (arry[i] > value) break; for (int j = elems; j > i; j--) arry[j] = arry[j - 1]; arry[i] = value; elems++; } public boolean deleteByIndex(int index) { if (index < 0 || index == elems) return false; for (int i = index; i < elems - 1; i++) { arry[i] = arry[i + 1]; } elems--; return true; } public boolean deleteByValue(long value) { int index = findByBinary(value); if (index == -1) return false; for (int i = index; i < elems - 1; i++) { arry[i] = arry[i + 1]; } elems--; return true; } public long getElementByIndex(int index) { return arry[index]; } public int findByNormal(long value) { int i = 0; for (; i < elems; i++) if (arry[i] == value) break; if (i == elems) return -1; return i; } public int findByBinary(long value) { int upper = elems; int lower = 0; int cur; while (true) { cur = (upper + lower) / 2; if (arry[cur] == value) return cur; else if (lower > upper) return -1; else { if (arry[cur] < value) lower = cur + 1; else upper = cur - 1; } } } public void bubbleSort() { for (int i = 0; i < elems - 1; i++) for (int j = 0; j < elems - i - 1; j++) if (arry[j] > arry[j + 1]) { swap(j, j + 1); } } public void selectionSort() { int min; for (int i = 0; i < elems - 1; i++) { min = i; for (int j = i + 1; j < elems; j++) if (arry[min] > arry[j]) min = j; swap(i, min); } } public void insertionSort() { long temp; int swapIndex; for (int i = 1; i < elems; i++) { temp = arry[i]; for (swapIndex = i; swapIndex > 0 && arry[swapIndex - 1] > temp; swapIndex--) arry[swapIndex] = arry[swapIndex - 1]; arry[swapIndex] = temp; } } public void swap(int from, int to) { long temp = arry[from]; arry[from] = arry[to]; arry[to] = temp; } public void display() { for (int i = 0; i < elems; i++) { System.out.print(arry[i] + " "); } System.out.println(); } public boolean isEmpty() { return elems == 0; } public boolean isPull() { return elems == arry.length; } }
以下是运行结果: