排序总结

排序算法总结

排序总结_第1张图片

插入排序 O ( n 2 ) O(n^2) O(n2)

void insertSort(int a[], int n)
{
    for(int i = 1; i < n; i++){
        int tmp = a[i], j;
        for(j = i; j > 0 && tmp < a[j-1];j--)
            a[j] = a[j-1];
        a[j] = tmp;
    }
}

冒泡排序 O ( n 2 ) O(n^2) O(n2)

void bubbleSort(int a[], int n)
{
    for(int i = n - 1; i > 0; i--)
        for(int j = 0; j < i; j++)
            if(a[j] > a[j+1]) 
                swap(a[j], a[j+1]);
}

选择排序 O ( n 2 ) O(n^2) O(n2)

void selectSort(int a[], int n)
{
    for(int i = 0; i < n - 1; i++)
        for(int j = i + 1; j < n; j++)
            if(a[i] > a[j]) 
                swap(a[i], a[j]);
}
# Finds the smallest value in an array
def findSmallest(arr):
  smallest = arr[0]
  smallest_index = 0
  for i in range(1, len(arr)):
    if arr[i] < smallest:
      smallest = arr[i]
      smallest_index = i
  return smallest_index

def selectionSort(arr):
  newArr = []
  for i in range(len(arr)):
      smallest = findSmallest(arr)
      newArr.append(arr.pop(smallest))
  return newArr

print(selectionSort([5, 3, 6, 2, 10]))

快排模板 O ( n l o g n ) O(nlogn) O(nlogn)——分治

  • 确定分界点
  • 调整区间:左边所有值<=x, 右边所有值>=x
  • 递归处理左右两段

特点:不稳定,若要变成稳定,需要变成pair双排序即可.

#include 
using namespace std;
const int N = 1e6 + 10;
int n;
int q[N];

void quick_sort(int q[], int l, int r)
{
    if (l >= r) return;
    int x = q[l], i = l -1, j = r + 1; // 定义两个指针
    while (i < j){
        do i++; while (q[i] < x);
        do j--; while (q[j] > x);
        if (i < j) swap(q[i], q[j]);
    }
    quick_sort(q, l, j);  // 上取整不能取到l的边界
    quick_sort(q, j + 1, r);
}

int main()
{
    scanf("%d", &n);
    for (int i = 0; i < n; i++) scanf("%d", &q[i]);
    quick_sort(q, 0, n - 1);
    for (int i = 0; i < n; i++) printf("%d ", q[i]);
    return 0;
}
def quicksort(array):
  if len(array) < 2:
    return array
  else:
    pivot = array[0]
    less = [i for i in array[1:] if i <= pivot]
    greater = [i for i in array[1:] if i > pivot]
    return quicksort(less) + [pivot] + quicksort(greater)
print(quicksort([10, 5, 2, 3]))

归并排序 $O(nlogn) $

  1. 确定分界点,mid = (1 + r) / 2
  2. 递归排序left, right
  3. 归并——合二为一
#include 
using namespace std;

const int N = 1e6 + 10;
int n;
int q[N], tmp[N];//需要一个额外辅助数组

void merge_sort(int q[], int l, int r){
    if (l >= r) return;
    int mid = l + r >> 1;
    //递归
    merge_sort(q, l, mid), merge_sort(q, mid + 1, r);
	// 归并
    int k = 0, i = l, j = mid + 1; // k表示临时数组中有几个数
    while (i <= mid && j <= r)
        if (q[i] <= q[j]) tmp[k++] = q[i++];
        else tmp[k++] = q[j++];
    while (i <= mid) tmp[k++] = q[i++]; // 只要左边没循环完,就执行此操作
    while (j <= r) tmp[k++] = q[j++]; // same as left
    for (i = l, j = 0; i <= r; i++, j++) 
        q[i] = tmp[j]; // 再将临时数组放回原数组
    
}

int main()
{
    scanf("%d", &n);
    for (int i = 0; i < n; i++) scanf("%d", &q[i]);
    merge_sort(q, 0, n - 1);
    for (int i = 0; i < n; i++) printf("%d ", q[i]);
    return 0;
}

堆排序 O ( n l o g n ) O(nlogn) O(nlogn)

void adjust_heap(int a[], int x, int n)
{
    int l = x * 2 + 1;
    int r = x * 2 + 2;
    int max = x;

    if(l < n && a[l] > a[max]) max = l;
    if(r < n && a[r] > a[max]) max = r;

    if(max != x)
    {
        swap(a[x], a[max]);
        adjust_heap(a,max,n);
    }
}

void heapSort(int a[], int n)
{
    for(int i = n/2-1; i >= 0; i--)
        adjust_heap(a, i, n);
    for(int i = n-1; i > 0; i--)
    {
        swap(a[0], a[i]);
        adjust_heap(a,0,i);
    }
}

你可能感兴趣的:(Data,Structure,and,Algorithms)