• Lecture--9 Sorting


    1/排序算法:冒泡排序bubble sort,插入排序 insertion sort,选择排序 selection sort,快速排序 quick sort,归并排序 merge sort;堆排序 heap sort---基于排序

    桶排序bucket sort  一种特殊情况下的排序。

    2/实现

    1)冒泡排序bubble sort:从位置0开始,一次比较到length - 0,前面大交换;再从位置1开始,依次类推。

     1 public void bubbleSort(int[] nums) {
     2        for (int i = 0; i < nums.length; i++) {
     3          for (int j = 1; j < nums.length - i; j++) {
     4              if (num[j - 1] > nums[j]) {
     5                  swap(nums, j - 1, j);
     6              }
     7          }
     8      }
     9 }
    10 
    11 public void swap(int[] nums, int i , int j) {
    12    int temp = nums[i];
    13    nums[i] = nums[j];
    14    nums[j] = temp;
    15 }

    冒泡排序有一个优化算法,就是在一个排好序的数组,一轮比较没有swap,则是排好序的,那么直接返回

     1  1 public void bubbleSort(int[] nums) {
     2  2    for (int i = 0; i < nums.length; i++) {
     3            boolean isSwap = false;
     4  3        for (int j = 1; j < nums.length - i; j++) {
     5  4             if (num[j - 1] > nums[j]) {
     6  5                  swap(nums, j - 1, j);
     7                      isSwap = true;
     8  6              }
     9  7          }
    10              if (!isSwap) {
    11                 return;
    12              }
    13  8      }
    14  9 }
    15 10 
    16 11 public void swap(int[] nums, int i , int j) {
    17 12    int temp = nums[i];
    18 13    nums[i] = nums[j];
    19 14    nums[j] = temp;
    20 15 }

    2)插入排序insertion sort:先把前i个数字排好序,然后再把前i+1个数字排好序。

     1 public void insertSort(int[] nums) {
     2     for (int i = 1; i < nums.length; i++) {
     3         for (int j = i; j > 0; j --) {
     4             if (nums[j - 1] > nums[j ]) {
     5                 swap(nums, j - 1; j);
     6             }
     7         }
     8     }
     9 }
    10 
    11 public void swap(int[] nums, int i, int j) {
    12    int temp = nums[i];
    13    nums[i] = nums[j];
    14    nums[j] = temp;
    15 }

    3)选择排序selection sort:先从N里面选出最小的一个值,与0位置数字交换,再从剩下数字里面选出最小的值跟1位置交换。依次类推。

     1 public void selectionSort(int[] nums) {
     2    for (int i = 0; i < nums.length - 1; i++) {//i < nums.length -1,j要从i + 1开始
     3        int min = nums[i];
     4        int minIndex = i;
     5         for (int j = i + 1; j > nums.length; j++) {
     6             if (num[j] < min) {
     7                 min = num[j];
     8                 minIndex = j;
     9             }
    10         }
    11         num[minIndex] = num[i];
    12         nums[i] = min;
    13    }
    14 }

    4)快速排序quick sort:

     1 public void quickSort(int[] nums) {
     2     sort(nums, 0, nums.length);
     3 }
     4 
     5 publc void sort(int[] nums, int begin, int end) {
     6     if (begin >= end) {
     7         return;
     8     }
     9     int pivotIndex = partition(nums, begin, end);
    10     sort(nums, begin, pivotIndex - 1);//pivot已经就位,所以跳过
    11     sort(nums, pivotIndex + 1, end);
    12 }
    13 
    14 public partition(int[] nums, int begin, int end) {
    15     int pivot = num[begin];
    16     while (begin < end) {
    17         while (begin < end && num[end] > pivot) {
    18              end--;
    19         }
    20         nums[begin] = nums[end];
    21         while (begin < end && nums[begind] <= pivot) {
    22               begin++;
    23         }
    24         nums[end] = nums[begin];
    25     }
    26     nums[begin] = pivot;
    27     return begin;
    28 }        
  • 相关阅读:
    按照指定的字符串拆分字符串,split()方法。
    charAt()取出指定位置的字符 .length()得到一个字符串的长度 indexOf()查找一个指定的字符是否存在并返回其位置 trim()去掉字符串的左右空格 substring()字符串的截取 str.replace(" ", ""); 去掉所有空格,包括首尾、中间
    字符串与字符数组的多种转换方式。
    匿名对象。
    构造方法。
    递归的练习,1.统计文件夹大小 2.删除文件夹及文件夹下的文件
    jquery零散小饼干
    jQuery review
    git解决冲突
    url、href、src
  • 原文地址:https://www.cnblogs.com/shihuvini/p/7468498.html
Copyright © 2020-2023  润新知