黑马程序员—学习笔记—各种排序法

来源:互联网 发布:云计算与电子政务 编辑:程序博客网 时间:2024/06/06 00:00

- -----<a href="http://www.itheima.com" target="blank">Java培训、Android培训、iOS培训、.Net培训</a>、期待与您交流! -------

JAVA中的排序算法一般主要有四种方法:快速排序法、冒泡法、选择排序法、插入排序法。

快速排序法主要是运用了Arrays中的一个方法Arrays.sort()实现。

冒泡法是运用遍历数组进行比较,通过不断的比较将最小值或者最大值一个一个的遍历出来。

选择排序法是将数组的第一个数据作为最大或者最小的值,然后通过比较循环,输出有序的数组。

插入排序是选择一个数组中的数据,通过不断的插入比较最后进行排序。

快速排序法、

原理:
选择数组中的一个元素作为标准,将所有比标准小的元素放到左边,所有比标准大的元素放到右边。 并对左边和右边的元素做一样的快速排序过程。

[java] view plaincopy
  1. public int[] quickSort(int[] result) {  
  2. quick(result, 0, result.length - 1);  
  3. return result;  
  4. }  




选择数组中的一个元素作为标准,将所有比标准小的元素放到左边,所有比标准大的元素放到右边并对左边和右边的元素做一样的快速排序过程。
[java] view plaincopy
  1. <pre name="code" class="java">private void quick(int[] array, int startIndex, int endIndex) {  
  2. int pIndex = startIndex;  
  3. for (int i = startIndex + 1; i <= endIndex; i ++) {  
  4. if (array[i] < array[pIndex]) {  
  5. int temp = array[i];  
  6. for (int j = i; j > pIndex; j --) {  
  7. array[j] = array[j - 1];  
  8. }  
  9. array[pIndex] = temp;  
  10. pIndex ++;  
  11. }  
  12.   
  13. }  
  14. if (pIndex - startIndex > 1) {  
  15. quick(array, startIndex, pIndex - 1);  
  16. }  
  17. if (endIndex - pIndex > 1) {  
  18. quick(array, pIndex + 1, endIndex);  
  19. }  
  20. }  


冒泡排序法

比较n轮,每一轮都把最大元素移动到数组后端。

[java] view plaincopy
  1. public int[] bubbleSort(int[] result) {  
  2.   
  3.   
  4. for (int i = 0; i < ARRAYSIZE; i ++) {  
  5. for (int j = i + 1; j < ARRAYSIZE; j ++) {  
  6. if (result[i] > result[j]) {  
  7. swap(result, i, j);  
  8. }  
  9.   
  10. }  
  11. }  
  12.   
  13. return result;  
  14. }  


选择排序法

每遍历未排序部分一次都选出一个最小值,并将最小值元素移动到数组前端

[java] view plaincopy
  1. public int[] simpleSelectionSort(int[] result) {  
  2.   
  3. int minIndex = 0;  
  4. for (int i = 0; i < result.length; i ++) {  
  5. minIndex = i;  
  6. for (int j = i + 1; j < result.length; j ++) {  
  7. if (result[j] < result[minIndex]) {  
  8. minIndex = j;  
  9. }  
  10.   
  11. }  
  12. swap(result, minIndex, i);  
  13. }  
  14.   
  15. return result;  
  16. }  
  17.   
  18. private void swap(int[] arr, int i, int j) {  
  19. int temp = arr[i];  
  20. arr[i] = arr[j];  
  21. arr[j] = temp;  
  22.   
  23. }  
  24.   
  25. }  


插入排序法

原理与插入排序类似,不同点在于寻找插入位置的时候,采取的是折半查找方法

[java] view plaincopy
  1. public int[] binsertSort(int[] result) {  
  2.   
  3. for (int i = 1; i < ARRAYSIZE; i ++) {  
  4. if (result[i] < result[0]) {  
  5. int temp = result[i];  
  6. for (int j = i - 1; j >= 0; j --) {  
  7. result[j + 1] = result[j];  
  8.   
  9. }  
  10. result[0] = temp;  
  11.   
  12. else if (result[i] < result[i - 1]) {  
  13. int larrange = 0;  
  14. int rarrange = i - 1;  
  15. while (rarrange - larrange > 1) {  
  16. int p = (rarrange + larrange + 1)/2;  
  17. if (result[i] < result[p]) {  
  18. rarrange = p;  
  19. else {  
  20. larrange = p;  
  21. }  
  22.   
  23. }  
  24. int temp = result[i];  
  25. for (int j = i - 1; j >= larrange + 1; j --) {  
  26. result[j + 1] = result[j];  
  27.   
  28. }  
  29. result[larrange + 1] = temp;  
  30.   
  31. }  
  32. }  
  33.   
  34. return result;  
  35. }  
0 0
原创粉丝点击