九大排序算法(C++实现)

Quciksort

void quick_sort(vector<int> &nums, int l, int r) {
  if (l + 1 >= r) {
    return;
  }
  int first = l, last = r - 1, key = nums[first];
  while (first < last) {
    while (first < last && nums[last] >= key) {
      --last;
    }
    nums[first] = nums[last];
    while (first < last && nums[first] <= key) {
      ++first;
    }
    nums[last] = nums[first];
  }
  nums[first] = key;
  quick_sort(nums, l, first);
  quick_sort(nums, first + 1, r);
}

Mergesort

void merge_sort(vector<int> &nums, int l, int r, vector<int> &temp) {
  if (l + 1 >= r) {
    return;
  }
  // divide
  int m = l + (r - l) / 2;
  merge_sort(nums, l, m, temp);
  merge_sort(nums, m, r, temp);
  // conquer
  int p = l, q = m, i = l;
  while (p < m || q < r) {
    if (q >= r || (p < m && nums[p] <= nums[q])) {
      temp[i++] = nums[p++];
    } else {
      temp[i++] = nums[q++];
    }
  }
  for (i = l; i < r; ++i) {
    nums[i] = temp[i];
  }
}

Insertionsort

void insertion_sort<vector<int> &nums, int n){
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < nums[j]; ++j) {
      swap(nums[j], nums[j - 1]);
    }
  }
}

Bubblesort

void bubble_sort(vector<int> &nums, int n) {
  bool swapped;
  for (int i = 1; i < n; ++i) {
    swapped = false;
    for (int j = 1; j < n - i + 1; ++j) {
      if (nums[j] < nums[j - 1]) {
        swap(nums[j], nums[j - 1]);
        swapped = true;
      }
    }
    if (!swapped) {
      break;
    }
  }
}

Selectionsort

void selection_sort(vector<int> &nums, int n) {
  int mid;
  for (int i = 0; i < n - 1; ++i) {
    mid = i;
    for (int j = 0; j < n; ++j) {
      if (nums[j] < nums[mid]) mid = j;
    }
    swap(nums[mid], nums[i]);
  }
}

Shellsort

void shellSort(vector<int> &q) {
  int gap = q.size() / 2;
  while (gap) {
    for (int i = gap; i < q.size(); i += gap) {
      int t = q[i], j;
      for (j = i - gap; j >= 0; j -= gap) {
        if (q[j] > t)
          q[j + gap] = q[j];
        else
          break;
      }
      q[j + gap] = t;
    }
    gap /= 2;
  }
}

Heapsort

void push_down(vector<int> &heap, int size, int u) {
  int t = u, left = u * 2, right = u * 2 + 1;
  if (left <= size && heap[left] > heap[t]) t = left;
  if (right <= size && heap[right] > heap[t]) t = right;
  if (u != t) {
    swap(heap[u], heap[t]);
    push_down(heap, size, t);
  }
}

void push_up(vector<int> &heap, int u) {
  while (u / 2 && heap[u / 2] < heap[u]) {
    swap(heap[u / 2], heap[u]);
    u /= 2;
  }
}

void heapSort(vector<int> &q, int n) {
  int size = n;
  for (int i = 1; i <= n; i++) push_up(q, i);
  for (int i = 1; i <= n; i++) {
    swap(q[1], q[size]);
    size--;
    push_down(q, size, 1);
  }
}

Countsort

void countingSort(vector<int> &q, int n) {
  vector<int> cnt(101, 0);
  for (int i = 0; i < n; i++) cnt[q[i]]++;
  for (int i = 0, k = 0; i <= 100; i++) {
    while (cnt[i]) {
      q[k++] = i;
      cnt[i]--;
    }
  }
}

Radixsort

不超过999

int get(int x, int i) {
  while (i--) x /= 10;
  return x % 10;
}

void radixSort(vector<int> &q, int n) {
  vector<vector<int>> cnt(10);
  for (int i = 0; i < 3; i++) {
    for (int j = 0; j < 10; j++) cnt[j].clear();
    for (int j = 0; j < n; j++) cnt[get(q[j], i)].push_back(q[j]);
    for (int j = 0, k = 0; j < 10; j++) {
      for (int x : cnt[j]) q[k++] = x;
    }
  }
}

Summary

Algorithm Complexity Auxiliary Space Stability
Bubble Sort \(O(n^2)\) \(O(1)\) Yes
Selection Sort \(O(n^2)\) \(O(1)\) No
Insertion Sort \(O(n^2)\) \(O(1)\) Yes
Merge Sort \(O(n\log{n})\) \(O(n)\) Yes
Qucik Sort \(O(n\log{n})\) \(O(n\log{n})\) No
Heap Sort \(O(n\log{n})\) \(O(1)\) No
Shell Sort \(O(n\log{n})\) \(O(n\log{n})\) No
Count Sort \(O(n+k)\) \(O(n+k)\) Yes
Bucket Sort \(O(n+k)\) \(O(n+k)\) Yes
Radix Sort \(O(n*k)\) \(O(n+k)\) Yes
上一篇:FreeRTOS堆内存管理策略


下一篇:#1147. Heaps【完全二叉树 + 堆】