指尖世界

分享的乐趣

java数据结构学习之旅-简单排序总结

类别: 观点 更新时间: 2016-09-28
[摘要:经常使用简略排序总结: package arraySort; public class arrayBub { private long[] arr; int nElems; public arrayBub(int max) { ]

常用简单排序总结:

package arraySort;  public class arrayBub {     private long[] arr;     int nElems;      public arrayBub(int max) {         arr = new long[max];         nElems = 0;     }      public void insert(long value) {         arr[nElems] = value;         nElems++;     }      public void display() {         for (int i = 0; i < nElems; i++) {             System.out.print(arr[i] + " ");         }         System.out.println("");      }      // 冒泡排序     public void bubbleSort() {         for (int out = nElems - 1; out >= 1; out--) {             for (int in = 0; in < out; in++) {                 if (arr[in] > arr[in + 1]) {                     swap(in, in + 1);// 交换顺序                 }             }         }     }      // 插入排序     public void insertSort() {         int in, out;         for (out = 1; out < nElems; out++) {             long temp = arr[out];             in = out;             while (in > 0 && arr[in - 1] > temp) {                 arr[in] = arr[in - 1];                 --in;             }             arr[in]=temp;         }     }      // 选择排序     public void selectSort() {         for (int out = 0; out < nElems - 1; out++) {             int min = out;             for (int in = out + 1; in < nElems; in++) {                 if (arr[in] < arr[min]) {                     swap(in, min);                 }             }         }     } //交换顺序     public void swap(int one, int two) {         long temp = arr[one];         arr[one] = arr[two];         arr[two] = temp;     } } 
感谢关注 V8指尖世界Java频道,v8en.com是专门为互联网人打造的学习交流平台,全面满足互联网人工作与学习需求,更多互联网资讯尽在 V8指尖世界!