个人网站的建设参考文献,电商平台下载,如何建立,做网站百度收录注意#xff1a;1.在使用前需要先将Swap函数放在最前面。基本上都用了了Swap函数。 2.完成的是升序版本#xff0c;如想逆序#xff0c;自行更改。
目录 Swap函数#xff1a;
冒泡排序#xff1a;
选择排序
插入排序
希尔排序
堆排序
快速排序 Swap函数#xff1a…注意1.在使用前需要先将Swap函数放在最前面。基本上都用了了Swap函数。 2.完成的是升序版本如想逆序自行更改。
目录 Swap函数
冒泡排序
选择排序
插入排序
希尔排序
堆排序
快速排序 Swap函数
void Swap(int* a, int* b)
{int temp *a;*a *b;*b temp;
} 冒泡排序
void BubbleSort(int arr[], int n)
{for (int i 0; i n-1; i){for (int j 0; j n-i-1; j){if (arr[j] arr[j 1]){Swap(arr[j], arr[j 1]);}}}
}
选择排序
void SlectSrot(int* a, int n)
{int left 0;int right n - 1;while (leftright){int mini left;int maxi left;for (int i left1; i right; i){if (a[i]a[mini]){mini i;}if (a[maxi] a[i]){maxi i;}}Swap(a[mini], a[left]);if (maxi left)//因为如果maxileftleft已经和mini交换了之前的left的位置此时在mini处。{maxi mini;}Swap(a[maxi], a[right]);left;right--;}
}
插入排序
void InsertSort(int* a, int n)
{for (int i 0; i n-1; i){int end i;int temp a[end 1];while (end 0){if (a[end] temp){Swap(a[end], a[end1]);end--;}else{break;}}a[end 1] temp;}
}
希尔排序
void ShellSort(int* a, int n)
{int garp n/2;while (garp0){for (int i 0; i n - garp; i){if (garp 1){break;}int end i;int temp a[end garp];while (end 0){if (a[end] temp){Swap(a[end], a[end garp]);end - garp;}else{break;}}a[end garp] temp;}garp / 2;}
}
堆排序
void HeapSort(int arr[], int n)
{for (int i (n - 1 - 1) / 2; i 0; i--){AdjustDown(arr, i, n);}int end n - 1;while (end 0){Swap(arr[0], arr[end]);AdjustDown(arr, 0, end);end--;}
}//向下调整
void AdjustDown(int arr[], int father,int n)
{int childfather*21;while(childn){if (child 1 n arr[child] arr[child 1]){child;}if (arr[child]arr[father]){Swap(arr[child], arr[father]);father child;child father * 21;}else{break;}}
}
快速排序
void QuickSort(int* a, int left,int right)
{ if (left right){return;}int beginleft;int end right;//对快速排序的优化//1.随机取值//2.三数取中//随机取值//int randi rand() % (right - left);//randi left;//Swap(a[left], a[randi]);//三数取中//int mid FindMid(a, left, right);//Swap(a[left], a[mid]);int keyileft;while (left right){while (leftright a[right]a[keyi]){right--;}while (left right a[left]a[keyi]){left;}Swap(a[left], a[right]);}Swap(a[left], a[keyi]);QuickSort(a, begin, left - 1);QuickSort(a, left 1, end);
}