排序总结(源代码)(增加单链表快排)

来源:互联网 发布:手机会计记账软件 编辑:程序博客网 时间:2024/06/05 10:13

插入排序:

#include <iostream>using namespace std;void InsertSort(int *a, int n){if (a == NULL || n <= 1)return;for (int i = 1; i < n; i++){int temp = a[i];for (int j = i - 1; j >= 0; j--){if (temp < a[j]){a[j+1] = a[j];if (j == 0){a[j] = temp;break;}}else{a[j+1] = temp;break;}}}}void Display(int *a, int n){for (int i = 0; i < n; i++){cout << *(a + i) << " ";}cout << endl;}int main(){int a[10] = {9,8,11,7,5,4,3,2,1,0};InsertSort(a, 10);Display(a, 10);return 0;}




冒泡排序:

#include <iostream>using namespace std;void BubbleSort(int *a, int n){if (a == NULL || n <= 1)return;for (int i = n-1; i > 0; i--){bool changed = false;for (int j = 0; j < i; j++){if (a[j] > a[j+1]){changed = true;int temp = a[j];a[j] = a[j+1];a[j+1] = temp;}}if (!changed)return;}}void Display(int *a, int n){for (int i = 0; i < n; i++){cout << *(a + i) << " ";}cout << endl;}int main(){int a[10] = {9,8,7,6,5,4,3,2,1,0};BubbleSort(a, 10);Display(a, 10);return 0;}


选择排序:

#include <iostream>using namespace std;void SelectSort(int *a, int n){if (a == NULL || n <= 1)return;for (int i = 0; i < n-1; i++){int minIndex = i;for (int j = i + 1; j < n; j++){if (a[j] < a[minIndex])minIndex = j;}int temp = a[i];a[i] = a[minIndex];a[minIndex] = temp;}}void Display(int *a, int n){for (int i = 0; i < n; i++){cout << *(a + i) << " ";}cout << endl;}int main(){int a[10] = {9,8,7,6,5,4,3,2,1,0};SelectSort(a, 10);Display(a, 10);return 0;}


快速排序:

#include <iostream>using namespace std;static int Partition(int *a, int start, int end){int temp = a[start];while (start < end){while (a[end] >= temp && start < end)--end;a[start] = a[end];while (a[start] <= temp && start < end)++start;a[end] = a[start];}a[start] = temp;return start;}void QuickSort(int *a, int start, int end){if (start < end){int pivot = Partition(a, start, end);QuickSort(a, start, pivot-1);QuickSort(a, pivot+1, end);}}int main(){int a[10] = {9,8,7,6,5,1,2,4,3,0};quickSort(a, 0, 9);for (int i = 0; i < 10; i++){cout << a[i] << ", ";}cout << endl;return 0;}

对于快速排序,如果数据结构改为单链表,则代码如下:

#include <iostream>#include <cstdlib>using std::cout;using std::endl;struct Node{    int data;    Node *next;};static Node *Partition(Node *&start, Node *&end){    if (start == end || start->next == end)        return start;    Node *pivot = start;    Node *p = start, *q = start->next;    while (q != end)    {        if (q->data >= pivot->data)        {            p = q;            q = q->next;        }        else        {            p->next = q->next;            q->next = start;            start = q;            q = p->next;        }    }    return pivot;}static void QuickSort(Node *&start, Node *&end){    if (start == end)        return;    Node *pivot = Partition(start, end);    QuickSort(start, pivot);    QuickSort(pivot->next, end);}void QuickSort(Node *&start){    Node *p = NULL;    QuickSort(start, p); //此处不能直接传递NULL,因为end参数不接收右值引用}Node *CreateList(){    Node *a = new Node[10]; // just for test    srand(time(NULL));    for (int i = 0; i < 10; i++)    {        a[i].data = rand() % 10;        if (i == 9)            a[i].next = NULL;        else            a[i].next = &a[i+1];    }    return &a[0];}void PrintList(const Node *p){    while (p)    {        cout << p->data << " ";        p = p->next;    }    cout << endl;}int main(){    Node *p = CreateList();    PrintList(p);    QuickSort(p);        PrintList(p);    return 0;}



堆排序:

#include <iostream>using namespace std;void Display(int *a, int n){for (int i = 0; i < n; i++){cout << a[i] << " ";}cout << endl;}static void HeapAdjust(int *a, int n, int i){if (i < n/2){int left = 2 * i + 1;int right = 2 * i + 2;int big = left;if (right < n && a[right] > a[big])big = right;if (a[i] < a[big]){int temp = a[i];a[i] = a[big];a[big] = temp;HeapAdjust(a, n, big);}}}static void BuildHeap(int *a, int n){for (int i = n / 2 - 1; i >= 0; i--)HeapAdjust(a, n, i);}void HeapSort(int *a, int n){if (a == NULL || n <= 1)return;BuildHeap(a, n);for (int i = n - 1; i > 0; i--){int temp = a[0];a[0] = a[i];a[i] = temp;HeapAdjust(a, i, 0); // 第二个参数不是n,而是i,非常重要!!!//Display(a, n);}}int main(){int a[10] = {9,8,7,6,5,4,3,2,1,0};HeapSort(a, 10);Display(a, 10);return 0;}

归并排序:

#include <iostream>using namespace std;static void Merge(int *a, int start, int mid, int end, int *temp){int k = 0;int i = start, j = mid + 1;while (i <= mid && j <= end){if (a[i] < a[j])temp[k++] = a[i++];elsetemp[k++] = a[j++];}while (i <= mid)temp[k++] = a[i++];while (j <= end)temp[k++] = a[j++];for (int i = 0; i < k; i++){a[start + i] = temp[i];}}static void MergeSort(int *a, int start, int end, int *temp){if (start < end){int mid = start + (end - start) / 2;MergeSort(a, start, mid, temp);MergeSort(a, mid+1, end, temp);Merge(a, start, mid, end, temp);}}void MergeSort(int *a, int n){if (a == NULL || n <= 1)return;int *temp = new int[n];MergeSort(a, 0, n-1, temp);}void Display(int *a, int n){for (int i = 0; i < n; i++){cout << *(a + i) << " ";}cout << endl;}int main(){int a[10] = {9,8,7,6,23,1,9,2,1,0};MergeSort(a, 10);Display(a, 10);return 0;}


原创粉丝点击