当前位置:   article > 正文

Java实现十种排序算法_算法冒泡排序java实现

算法冒泡排序java实现

一、冒泡排序

  1. public static void main(String[] args) {
  2. // TODO Auto-generated method stub
  3. boolean flag = false;
  4. Date date = new Date();
  5. SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  6. String dateStr = simpleDateFormat.format(date);
  7. System.out.println(dateStr);
  8. int[] arr = { 3, -1, 10, 9, 20 };
  9. for (int i = 0; i < arr.length - 1; i++) {
  10. for (int j = 0; j < arr.length - i - 1; j++) {
  11. if (arr[j] > arr[j + 1]) {
  12. flag = true;
  13. int temp = arr[j];
  14. arr[j] = arr[j + 1];
  15. arr[j + 1] = temp;
  16. }
  17. }
  18. if (!flag) {
  19. break;
  20. } else {
  21. flag = false;
  22. }
  23. }
  24. System.out.println(Arrays.toString(arr));
  25. Date date1 = new Date();
  26. SimpleDateFormat simpleDateFormat1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  27. String dateStr1 = simpleDateFormat1.format(date);
  28. System.out.println(dateStr1);
  29. }

二、选择排序

  1. public static void main(String[] args) {
  2. // TODO Auto-generated method stub
  3. int[] arr = { 3, -1, 10, 9, -2 };
  4. for (int i = 0; i < arr.length - 1; i++) {
  5. int min = arr[i];
  6. int index = i;
  7. for (int j = i + 1; j < arr.length; j++) {
  8. if (min > arr[j]) {
  9. min = arr[j];
  10. index = j;
  11. }
  12. }
  13. if (index != i) {
  14. arr[index] = arr[i];
  15. arr[i] = min;
  16. }
  17. }
  18. System.out.println(Arrays.toString(arr));
  19. }

三、插入排序

  1. public static void main(String[] args) {
  2. // TODO Auto-generated method stub
  3. int[] arr = { 3, -1, 10, 9, 2 };
  4. for (int i = 1; i < arr.length; i++) {
  5. int insertVal = arr[i];
  6. int insertIndex = i - 1;
  7. while (insertIndex >= 0 && arr[insertIndex] > insertVal) {
  8. arr[insertIndex + 1] = arr[insertIndex];
  9. insertIndex--;
  10. }
  11. arr[insertIndex + 1] = insertVal;
  12. }
  13. System.out.println(Arrays.toString(arr));
  14. }

四、希尔排序

  1. public static void main(String[] args) {
  2. // TODO Auto-generated method stub
  3. int[] arr = { 3, -1, 10, 9, 20 };
  4. for (int i = arr.length / 2; i > 0; i /= 2) {
  5. for (int j = i; j < arr.length; j++) {
  6. for (int k = j - i; k >= 0; k -= i) {
  7. if (arr[k] > arr[k + i]) {
  8. int temp = arr[k];
  9. arr[k] = arr[k + i];
  10. arr[k + i] = temp;
  11. }
  12. }
  13. }
  14. }
  15. System.out.println(Arrays.toString(arr));
  16. }

五、归并排序

  1. public static void main(String[] args) {
  2. // TODO Auto-generated method stub
  3. int[] arr = { 3, -1, 10, 9, 20 };
  4. int[] temp = new int[arr.length];
  5. mergesort(arr, 0, arr.length-1, temp);
  6. System.out.println(Arrays.toString(temp));
  7. }
  8. public static void mergesort(int[] arr, int left, int right, int[] temp) {
  9. if (left < right) {
  10. int mid = (left + right) / 2;
  11. mergesort(arr, left, mid, temp);
  12. mergesort(arr, mid + 1, right, temp);
  13. mergeSort(arr, left, mid, right, temp);
  14. }
  15. }
  16. public static void mergeSort(int[] arr, int left, int mid, int right, int[] temp) {
  17. int i = left;
  18. int j = mid + 1;
  19. int t = 0;
  20. while (i <= mid && j <= right) {
  21. if (arr[i] <= arr[j]) {
  22. temp[t] = arr[i];
  23. i += 1;
  24. t += 1;
  25. } else {
  26. temp[t] = arr[j];
  27. j += 1;
  28. t += 1;
  29. }
  30. }
  31. while (i <= mid) {
  32. temp[t] = arr[i];
  33. t += 1;
  34. i += 1;
  35. }
  36. while (j <= right) {
  37. temp[t] = arr[j];
  38. t += 1;
  39. j += 1;
  40. }
  41. t = 0;
  42. int tempLeft = left;
  43. while (tempLeft <= right) {
  44. arr[tempLeft] = temp[t];
  45. tempLeft += 1;
  46. t += 1;
  47. }
  48. }

六、快速排序

  1. public static void main(String[] args) {
  2. Scanner sc = new Scanner(System.in);
  3. int n = sc.nextInt();
  4. int[] arr = new int[n];
  5. for (int i = 0; i < n; i++) {
  6. arr[i] = sc.nextInt();
  7. }
  8. quickSort(arr, 0, arr.length - 1);
  9. System.out.println(Arrays.toString(arr));
  10. }
  11. public static void quickSort(int[] arr, int left, int right) {
  12. int l = left;
  13. int r = right;
  14. int pivot = arr[(left + right) / 2];
  15. while (l < r) {
  16. while (arr[l] < pivot) {
  17. l += 1;
  18. }
  19. while (arr[r] > pivot) {
  20. r -= 1;
  21. }
  22. if (l >= r) {
  23. break;
  24. }
  25. int temp = arr[l];
  26. arr[l] = arr[r];
  27. arr[r] = temp;
  28. if (arr[l] == pivot) {
  29. r -= 1;
  30. }
  31. if (arr[r] == pivot) {
  32. l += 1;
  33. }
  34. }
  35. if (l == r) {
  36. l += 1;
  37. r -= 1;
  38. }
  39. if (left < r) {
  40. quickSort(arr, left, r);
  41. }
  42. if (right > l) {
  43. quickSort(arr, l, right);
  44. }
  45. }

七、堆排序

  1. public static void main(String[] args) {
  2. int[] arr = { 2, 1, 7, 6, -1, 5 };
  3. Date date = new Date();
  4. SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  5. String dateStr = simpleDateFormat.format(date);
  6. System.out.println(dateStr);
  7. heapSort(arr);
  8. Date date1 = new Date();
  9. SimpleDateFormat simpleDateFormat1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  10. String dateStr1 = simpleDateFormat1.format(date);
  11. System.out.println(dateStr1);
  12. System.out.println(Arrays.toString(arr));
  13. }
  14. public static void heapSort(int[] arr) {
  15. for (int i = arr.length / 2 - 1; i >= 0; i--) {
  16. adjustHeap(arr, i, arr.length);
  17. }
  18. for (int j = arr.length - 1; j > 0; j--) {
  19. int temp = arr[j];
  20. arr[j] = arr[0];
  21. arr[0] = temp;
  22. adjustHeap(arr, 0, j);
  23. }
  24. }
  25. public static void adjustHeap(int[] arr, int i, int length) {
  26. int temp = arr[i];
  27. for (int j = i * 2 + 1; j < length; j = j * 2 + 1) {
  28. if (j + 1 < length && arr[j] < arr[j + 1]) {
  29. j++;
  30. }
  31. if (arr[j] > temp) {
  32. arr[i] = arr[j];
  33. i = j;
  34. } else {
  35. break;
  36. }
  37. }
  38. arr[i] = temp;
  39. }

八、计数排序

  1. public static void main(String[] args) {
  2. int a[] = { 100, 93, 97, 92, 96, 99, 92, 89, 93, 97, 90, 94, 92, 95 };
  3. int b[] = countSort(a);
  4. for (int i : b) {
  5. System.out.print(i + " ");
  6. }
  7. System.out.println();
  8. }
  9. public static int[] countSort(int[] a) {
  10. int b[] = new int[a.length];
  11. int max = a[0], min = a[0];
  12. for (int i : a) {
  13. if (i > max) {
  14. max = i;
  15. }
  16. if (i < min) {
  17. min = i;
  18. }
  19. }
  20. int k = max - min + 1;
  21. int c[] = new int[k];
  22. for (int i = 0; i < a.length; ++i) {
  23. c[a[i] - min] += 1;
  24. }
  25. for (int i = 1; i < c.length; ++i) {
  26. c[i] = c[i] + c[i - 1];
  27. }
  28. for (int i = a.length - 1; i >= 0; --i) {
  29. b[--c[a[i] - min]] = a[i];
  30. }
  31. return b;
  32. }

九、桶排序

  1. public static void main(String[] args) {
  2. int[] arr = { 3, 1, 10, 9, 20 };
  3. bucketSort(arr);
  4. for (int i : arr) {
  5. System.out.print(i + " ");
  6. }
  7. }
  8. public static void bucketSort(int arr[])
  9. {
  10. int n = arr.length;
  11. int bask[][] = new int[10][n];
  12. int index[] = new int[10];
  13. int max = Integer.MIN_VALUE;
  14. for (int i = 0; i < n; i++) {
  15. max = max > (Integer.toString(arr[i]).length()) ? max : (Integer.toString(arr[i]).length());
  16. }
  17. String str;
  18. for (int i = max - 1; i >= 0; i--) {
  19. for (int j = 0; j < n; j++) {
  20. str = "";
  21. if (Integer.toString(arr[j]).length() < max) {
  22. for (int k = 0; k < max - Integer.toString(arr[j]).length(); k++)
  23. str += "0";
  24. }
  25. str += Integer.toString(arr[j]);
  26. bask[str.charAt(i) - '0'][index[str.charAt(i) - '0']++] = arr[j];
  27. }
  28. int pos = 0;
  29. for (int j = 0; j < 10; j++) {
  30. for (int k = 0; k < index[j]; k++) {
  31. arr[pos++] = bask[j][k];
  32. }
  33. index[j] = 0;
  34. }
  35. }
  36. }

十、基数排序

  1. public static void main(String[] args) {
  2. // TODO Auto-generated method stub
  3. int[] arr = { 3, 1, 10, 9, 20 };
  4. radixSort(arr);
  5. System.out.println(Arrays.toString(arr));
  6. }
  7. public static void radixSort(int[] arr) {
  8. int max = arr[0];
  9. for (int i = 1; i < arr.length; i++) {
  10. if (arr[i] > max) {
  11. max = arr[i];
  12. }
  13. }
  14. int maxLength = (max + "").length();
  15. int[][] bucket = new int[10][arr.length];
  16. int[] bucketElementCounts = new int[10];
  17. for (int i = 0, n = 1; i < maxLength; i++, n *= 10) {
  18. for (int j = 0; j < arr.length; j++) {
  19. int digitOfElement = arr[j] / n % 10;
  20. bucket[digitOfElement ][bucketElementCounts[digitOfElement]] = arr[j];
  21. bucketElementCounts[digitOfElement]++;
  22. }
  23. int index = 0;
  24. for (int k = 0; k < bucketElementCounts.length; k++) {
  25. if (bucketElementCounts[k] != 0) {
  26. for (int l = 0; l < bucketElementCounts[k]; l++) {
  27. arr[index++] = bucket[k][l];
  28. }
  29. }
  30. bucketElementCounts[k] = 0;
  31. }
  32. }
  33. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/在线问答5/article/detail/941474
推荐阅读
相关标签
  

闽ICP备14008679号