各种排序算法整理
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了各种排序算法整理相关的知识,希望对你有一定的参考价值。
1.插入排序
void InsertSort(int *a, int n) { for(i = 1; i < n; i++) { k = a[i]; for(j = i; k < a[j-1] && j > 0; j--) a[j] = a[j - 1]; a[j] = k; } }
2.冒泡排序
void BubbleSort(int *a, int n) { for(i = 0; i < n; i++) { for(j = 0; j < n - i; j++) { if(a[j] > a[j+1]) swap(a[j], a[j+1]); } } }
3.选择排序
void SelectSort(int *a, int n) { for(i = 0; i < n-1; i++) { k = i; for(j = i+1; j < n; j++) { if(a[j] < a[k]) k = j; } swap(a[i], a[k]); } }
4.希尔排序
void ShellSort(int *a, int n) { int inc = n; while(inc > 1) { inc = inc/3 + 1; //插入排序排分组中元素 for(int start = 0; start < inc; start++) { for(i = start + inc; i < n; i += inc) { k = a[i]; for(j=i; k<a[j-inc]&&j>0; j-=inc) a[j] = a[j - inc]; a[j] = k; } } } }
5.基数排序
#include<queue> void radixSort(int *a, int n, int d) { int digit = 1; queue<int> digitQueue[10]; for ( i = 0; i < d; ++i) { for (j = 0; j < n; j++) digitQueue[(a[j] / digit) % 10].push(a[j]); j = 0; for (int digitVal=0; digitVal<10; ++digitVal) while (!digitQueue[digitVal].empty()) { a[j] = digitQueue[digitVal].front(); digitQueue[digitVal].pop(); j++; } cout << "number " << i << ":" << endl; for ( j = 0; j < n; ++j) cout << a[j] << " "; cout << endl; digit *= 10;//从个位数开始,直到第d位数 } }
6.堆排序(优先队列)
#define MAXN 100 class array { private: int elem[MAXN]; public: int &operator[](int i) { return elem[i]; } }; class heap { private: int n; array h; public: void clear() { n = 0; } int top() { return h[1]; } int size() { return n; } void push(int); void pop(); }; void heap::push(int x) {//siftup n = n + 1; h[n] = x; int tmp = n; while(tmp / 2 != 0) { if(h[tmp/2] > h[tmp]) swap(h[tmp/2], h[tmp]); else break; tmp = tmp/2; } } void heap::pop() {//siftdown swap(h[1],h[n]); n = n - 1; i = 1; while(2 * i <= n) { int parent = i, child = 2 * i; int small = parent; if(h[small] > h[child]) small = child; if(child + 1 <= n && h[child+1] < h[small]) small = child + 1; if(small == parent) break; else { swap(h[parent],h[small]); i = small; } } }
7.归并排序
void MergeSort(int *a, int x, int y) // initial x = 0, y = n { if(y - x <= 1) return; //Divide int m = (y - x) / 2 + x; //Conquer MergeSort(a, x, m); MergeSort(a, m, y); //Combine int p = x, q = m, k = 0; int t[y - x + 1]; while( p < m || q < y) { if(q >= y || (p < m && a[p] <= a[q])) t[k++] = a[p++]; else { t[k++] = a[q++]; //逆序对个数 //inverse_number = inverse_number + m - p; } } for(p = x, k = 0; p < y; p++) a[p] = t[k++]; }
链表实现
struct linkedlist{ int data; linkedlist *next; }; //sort the list by mergesort and return the head of it void Mergesort(linkedlist *&head, int len){ if(len <= 1) return; //Divide int m = len / 2; linkedlist *mid = head; linkedlist *rear = head, *new_head = NULL; for(int i = 0; i < m ; i++) { mid = mid->next; if(i == m - 2) rear = mid; } rear->next = NULL; //Conquer Mergesort(head, m);//sort the left side Mergesort(mid, len - m);//sort the right side //Combine two ordered list linkedlist *tmp1 = head, *tmp2 = mid; linkedlist *new_rear = NULL; while(tmp1 != NULL || tmp2 != NULL) { if(tmp2 == NULL || (tmp1 != NULL && tmp1->data <= tmp2->data)) { if(new_head == NULL) new_head = tmp1; else new_rear->next = tmp1; new_rear = tmp1; tmp1 = tmp1->next; } else { if(new_head == NULL) new_head = tmp2; else new_rear->next = tmp2; new_rear = tmp2; tmp2 = tmp2->next; } } head = new_head; }
8.快速排序
int partition( int *a, int low, int high ) { int left, right; int pivot_item = a[low]; left = low; right = high; while ( left < right ) { /* Move left while item <= pivot */ while( a[left] <= pivot_item && left < high) left++; /* Move right while item > pivot */ while( a[right] >= pivot_item && low < right) right--; if ( left < right ) swap(a[left], a[right]); } /* right is final position for the pivot */ swap(a[low], a[right]); return right; } void QuickSort(int *a, int low, int high) { int pivot_position; if(low < high) { pivot_position = partition(a, low, high); QuickSort(a, low, pivot_position - 1); QuickSort(a, pivot_position + 1, high); } }
快排思想找第k小数
int first[1000001], second[1000001]; int select(int a[], int n, int k) { int pivot = a[0]; int st = 0, nd = 0; for(int i = 1; i < n; i++) { if(a[i] <= pivot) first[st++] = a[i]; else second[nd++] = a[i]; } if(st == k) return pivot; else if(st > k) return select(first, st, k); else return select(second, nd, k - st - 1); }
各排序算法对比(注意稳定性)
以上是关于各种排序算法整理的主要内容,如果未能解决你的问题,请参考以下文章