溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶(hù)服務(wù)條款》

排序算法合集

發(fā)布時(shí)間:2020-07-11 13:04:11 來(lái)源:網(wǎng)絡(luò) 閱讀:297 作者:xiexiankun 欄目:編程語(yǔ)言

#define _CRT_SECURE_NO_WARNINGS

#include<iostream>

#include<assert.h>

#include<cstdlib>

using namespace std;

//直接插入排序

//思路:保留第一個(gè),取第二個(gè)和第一個(gè)進(jìn)行比較,如果第二個(gè)大于第一個(gè)直接插入數(shù)組第二個(gè)位置,否則

//將第一個(gè)向后移動(dòng)一位,將第二個(gè)數(shù)據(jù)插入第一個(gè)位置,再取第三個(gè)數(shù)據(jù)與第二個(gè)進(jìn)行比較以此類(lèi)推……

void Insertsort(int *a, size_t size)

{

assert(a);

for (size_t i = 1; i < size; i++)

{

int index = i;

int temp = a[index];

int end = index - 1;

while (end >= 0 && temp<a[end])

{

a[end+1] = a[end];//向后移動(dòng);

end--;//調(diào)試一下你就知道怎么回事

}

a[end+1] = temp;//插入

}

}

//希爾排序

void ShellSort(int *a, size_t size)

{

assert(a);

int gap = (size / 3) + 1;

while (gap >0)

{

for (int i = gap; i < size; i++)

{

int index = i;

int tmp = a[index];

int end = index - gap;

while (end >= 0 && tmp < a[end])

{

a[end + gap] = a[end];

end -= gap;

}

a[end + gap] = tmp;

}

           gap /=3;

}

}

//void ShellSort(int a[], int n)  

//{

// int j, gap;

// gap = n/2;

// while (gap > 0)

// {

// //for (gap = n / 2; gap > 0; gap /= 2)

// for (j = gap; j < n; j++)//從數(shù)組第gap個(gè)元素開(kāi)始  

// if (a[j] < a[j - gap])//每個(gè)元素與自己組內(nèi)的數(shù)據(jù)進(jìn)行直接插入排序  

// {

// int temp = a[j];

// int k = j - gap;

// while (k >= 0 && a[k] > temp)

// {

// a[k + gap] = a[k];

// k -= gap;

// }

// a[k + gap] = temp;

// }

// gap /= 2;

// }

//

//}


//堆排

//向下調(diào)整

void AdjustDown(int *a, size_t size, int root)

{

int child = 2 * root + 1;

while (child < size)

{

if (child + 1 < size&&a[child + 1] > a[child])

{

++child;

}

if (a[child] > a[root])

{

swap(a[child], a[root]);

root = child;

child = 2 * root + 1;

}

else

{

break;

}

}

}

void HeapSort(int *a, size_t size)

{

assert(a);

for (int i = (size - 2) / 2; i >= 0; i--)

{

AdjustDown(a, size, i);

}

for (size_t i = 0; i < size; ++i)

{

swap(a[0], a[size -1- i]);

AdjustDown(a, size -1- i, 0);

}

}

//選擇排序

void SelectionSort(int*a, size_t size)

{

for (int i = 0; i < size; i++)

{

int index=i;//保存最小的數(shù)

for (int j = i+1; j < size; j++)//j=i+1 ?因?yàn)榍懊嬉呀?jīng)有序,所以不用送1開(kāi)始而是從1+i開(kāi)始的;

{

if (a[j] < a[index])

{

index = j;//保存下最小的數(shù)的下標(biāo)

}

}

if (index != i)//排之前已經(jīng)是一個(gè)序列,所以需要進(jìn)行交換。

{

int tmp = a[index];

a[index] = a[i];

a[i] = tmp;

}

}

}

//選擇排序的優(yōu)化

void SelectSort(int *a, size_t size)

{

int left = 0;

int right = size - 1;

for (; left < right; left++, right--)

{

int max = left;

int min = right;

for (int i = left; i<=right;i++)//一次循環(huán)找出其中的最大值和最小值,

{

if (a[min]>a[i])

{

min = i;

}

else if (a[max] < a[i])

{

max = i;

}

   }

if (left != min)

{

int temp = a[min];

a[min] = a[left];

a[left] = temp;

if (max == left)

{

max = min;

}

}

if (right != max)

{

int tmp = a[max];

a[max] = a[right];

a[right] = tmp;

if (min == right)

{

min = max;

}

}

}

}

//冒泡排序

void BubbleSort(int *a, size_t size)

{

for (int i = 1; i < size; i++)

{

for (int j = 0; j < size-i; j++)

{

if (a[j]>a[j + 1])

{

int tmp = a[j];

a[j] = a[j + 1];

a[j + 1] = tmp;

}

}

}

}

//快速排序


int PartSort(int *a, int left,int right)

{

int MidOfThree(int *a, int left, int right);


int begin = left;

int end = right-1;

int key = MidOfThree(a,left,right);

while (begin < end)

{

while (begin < end && a[begin] <= key)

{

++begin;

}

while (end > begin && a[end] >= key)

{

--end;

}

swap(a[begin], a[end]);

}

if (a[begin]>a[right])

{

swap(a[begin], a[right]);

return begin;

}

else

{

return right;

}

}

void QuickSort(int*a,int left,int right)

{

assert(a);

if (left < right)

{

int boundary = PartSort(a, left, right);

QuickSort(a, left, boundary - 1);

QuickSort(a, boundary + 1, right);

}

}

//快速排序之挖坑填數(shù)

void QuickSort1(int*a, int left, int right)

{

assert(a);

if (left < right)

{

int begin = left;

int end = right;

int key = a[left];

while (begin < end)

{

while (begin < end&&a[end] >= key)

{

--end;

}

a[begin] = a[end];

while (begin < end&&a[begin] <= key)

{

++begin;

}

a[end] = a[begin];

}


a[begin] = key;

QuickSort1(a, left, begin - 1);

QuickSort1(a, begin + 1, right);

}

}

//優(yōu)化快速排序

//快速排序的優(yōu)化-三數(shù)取中法

int MidOfThree(int *a, int left, int right)

{

int mid = left + (right - left) / 2;

if (a[mid] < a[left])

{

swap(a[mid], a[left]);

}

if (a[left]>a[right])

{

swap(a[left], a[right]);

}

if (a[mid] > a[right])

{

swap(a[mid], a[right]);

}

return a[mid];

//a[left]<a[mid]<a[right]

}



//歸并排序法

//{begin.....mid,mid+1.....end}

void MergeArray(int *a, int begin, int mid, int end, int*tmp)//使兩個(gè)數(shù)組有序然后合并

{

int i = begin;

int m = mid;

int j = mid + 1;

int k = end;

int n = 0;

while (i <= m && k >= j)

{

if (a[i] <= a[j])

{

tmp[n++] = a[i++];

}

else

{

tmp[n++] = a[j++];

}

}

while (i <= m)

{

tmp[n++] = a[i++];

}

while (j <= k)

{

tmp[n++] = a[j++];

}

for (int i = 0; i < n; i++)

{

a[begin+i] = tmp[i];

}


void _MergeSort(int *a, int left, int right,int *tmp)

{

if (left < right)

{

int mid = left + (right - left) / 2;//將數(shù)列分成兩半,再將每一半排序

_MergeSort(a, left, mid, tmp);       //有點(diǎn)像將兩個(gè)有序的單鏈表合并后依然有序

_MergeSort(a, mid + 1, right, tmp);

MergeArray(a, left, mid, right, tmp);

}

}

void MergeSort(int *a, size_t size)

{

int *tmp = new int[size];

if (tmp != NULL) 

{

_MergeSort(a, 0, size - 1, tmp);

}

delete[]tmp;

}

//計(jì)數(shù)排序

//int a[10] = { 2, 0, 4, 9, 3, 6, 8, 7, 1, 5 };


void CountSort(int *a, size_t size)

{

assert(a);

int max = a[0];

int min = a[0];

for (size_t i = 0; i < size; i++)

{

if (a[i]>max)

{

max = a[i];

}

if (a[i] < min)

{

min = a[i];

}

}

int range = max - min + 1;

int *countarray = new int[range];

memset(countarray, 0, sizeof(int)*range);

for (size_t i = 0; i < size; i++)

{

countarray[a[i] - min]++;

}

size_t index = 0;

for (int i = 0; i <= range; i++)

{

while (countarray[i]-->0)

{

a[index++] = min + i;

}

}

//delete[] countarray;

//countarray = NULL;

}

//基數(shù)排序

int GetMaxDigit(int *a, size_t size)

{

int digit = 1;

int max = 10;

for (size_t i = 0; i < size; i++)

{

while (a[i]>=max)

{

++digit;

max *= 10;

}

}

return digit;

}

void DigitSortLSD(int* a, size_t size)

{

assert(a);

int MaxBit = GetMaxDigit(a, size);   //獲取最大位數(shù)

int* bucket = new int[size];     //為bucket開(kāi)辟空間

int count[10];

int start[10];

int bit = 1;

int digit = 1;

while (bit <= MaxBit)

{

memset(count, 0, sizeof(int)* 10);

memset(start, 0, sizeof(int)* 10);

//統(tǒng)計(jì)0-9號(hào)桶中共有多少個(gè)數(shù)據(jù)

for (size_t i = 0; i < size; ++i)

{

int num = (a[i] / digit) % 10;  //每個(gè)數(shù)字模10,取余數(shù),即為個(gè)位數(shù)字的值,存入相應(yīng)的位置,個(gè)數(shù)加1

count[num]++;

}


//計(jì)算個(gè)數(shù)為0-9  的在桶里的起始位置

start[0] = 0;

for (size_t i = 1; i < size; ++i)

{

start[i] = start[i - 1] + count[i - 1];

}


for (size_t i = 0; i < size; ++i)

{

int num = a[i] % 10;

bucket[start[num]++] = a[i];

}


//將桶中的數(shù)據(jù)拷貝回去

memcpy(a, bucket, sizeof(int)* 10);

digit *= 10;

++bit;

}

}

void Test()

{

int a[10] = { 2, 3, 1, 4, 5, 6, 9, 7, 8, 0 };

Insertsort(a, 10);

for (int i = 0; i < 10; i++)

{

cout << a[i] << " ";

}

cout << endl;

}

void Test1()

{

int a[10] = { 2, 5, 4, 9, 3, 6, 8, 7, 1, 0 };

ShellSort(a, 10);


for (int i = 0; i < 10; i++)

{

cout << a[i] << " ";

}

cout << endl;

}

void Test2()

{

int a[10] = { 4, 5, 1, 2, 3, 6, 9, 0, 8, 7 };

HeapSort(a, 10);

for (int i = 0; i < 10; i++)

{

cout << a[i] << " ";

}

cout << endl;

}

void Test3()

{

int a[10] = { 2, 5, 4, 9, 3, 6, 8, 7, 1, 0 };

SelectSort(a, 10);


for (int i = 0; i < 10; i++)

{

cout << a[i] << " ";

}

cout << endl;

}

void Test4()

{

int a[10] = { 2, 5, 4, 9, 3, 6, 8, 7, 1, 0 };

SelectionSort(a, 10);


for (int i = 0; i < 10; i++)

{

cout << a[i] << " ";

}

cout << endl;

}

void Test5()

{

int a[10] = { 2, 5, 4, 9, 3, 6, 8, 7, 1, 0 };

    BubbleSort(a,10);

for (int i = 0; i < 10; i++)

{

cout << a[i] << " ";

}

cout << endl;

}

void Test6()

{

int a[10] = { 2, 0, 4, 9, 3, 6, 8, 7, 1, 5 };

QuickSort(a, 0, 9);

for (int i = 0; i < 10; i++)

{

cout << a[i] << " ";

}

cout << endl;

}

void Test7()

{

int a[10] = { 2, 0, 4, 9, 3, 6, 8, 7, 1, 5 };

MergeSort(a,10);

for (int i = 0; i < 10; i++)

{

cout << a[i] << " ";

}

cout << endl;

}

void Test8()

{

int a[10] = { 2, 0, 4, 9, 3, 6, 3, 3, 1, 5 };

CountSort(a, 10);


for (int i = 0; i < 10; i++)

{

cout << a[i] << " ";

}

cout << endl;

}

void Test9()

{

int a[10] = { 2, 0, 4, 9, 3, 6, 8, 7, 1, 5 };

DigitSortLSD(a, 10);


for (int i = 0; i < 10; i++)

{

cout << a[i] << " ";

}

cout << endl;

}

int main()

{

Test();

Test1();

Test2();

Test4();

Test5();

Test6();

Test7();

Test8();

Test9();

system("pause");

return 0;

}


向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI