当前位置:网站首页>408 day attendance Chapter 8 sorting in class code collection
408 day attendance Chapter 8 sorting in class code collection
2022-07-18 14:24:00 【Obsession cuts the river】
The so-called in class code refers to , Non after class exercise code , Is the given code to recite , Altogether : Selection sort 、 Insertion sort 、 Bubble sort 、 Merge sort 、 Heap sort 、 Quick sort . And give a test example .
Bubble sort
void BubbleSort(int a[],int n){
int i,j,flag;
int tmp;
for(i=n-1;i>=0;i--){
flag = 0;
for(j = 1;j<=i;j++){
if(a[j-1]>a[j]){
tmp = a[j];
a[j] = a[j-1];
a[j-1] = tmp;
flag = 1;
}
}
if(flag == 0)
return ;
}
}
It's called
BubbleSort(a,6);//6 Refer to 0-5
Selection sort
void InsertSort(int a[],int n){
int i,j;
int tmp;
for(i=0;i<n;i++){
tmp = a[i];
j = i-1;
while(j>=0 && tmp<a[j]){
a[j+1] = a[j];
--j;
}
a[j+1] = tmp;
}
}
It's called
InsertSort(a,5);
Quick sort
void QuickSort(int a[],int low,int high){
int tmp;
int i = low,j = high;
if(low<high){
tmp = a[low];
while(i<j){
while(j>i && a[j] >= tmp) j--;
if(i<j){
a[i] = a[j];
++i;
}
while(i<j && a[i] < tmp) ++i;
if(i<j){
a[j] = a[i];
--j;
}
}
a[i] = tmp;
QuickSort(a,low,i-1);
QuickSort(a,i+1,high);
}
}
It's called
QuickSort(a,0,6);
Selection sort
void SelectSort(int a[],int n){
int i,j,k;
int tmp;
for(i=0;i<n;i++){
k = i;
for(j=i+1;j<n;j++)
if(a[k] > a[j])
k = j;
tmp = a[i];
a[i] = a[k];
a[k] = tmp;
}
}
It's called
SelectSort(a,5);
Heap sort
void Sift(int a[],int low,int high){
int i = low,j = 2*i;
int tmp = a[i];
while(j<=high){
if(j<high && a[j] < a[j+1])
++j;
if(tmp<a[j]){
a[i] =a[j];
i = j;
j = 2*i;
}else
break;
}
a[i] = tmp;
}
void HeapSort(int a[],int n){
int i;
int tmp;
for(i=n/2;i>=1;i--)
Sift(a,i,n);
for(i=n;i>=2;--i){
tmp = a[1];
a[1] = a[i];
a[i] = tmp;
Sift(a,1,i-1);
}
}
It's called
HeapSort(a,6);
Merge sort
void Merge( int A[], int TmpA[], int L, int R, int RightEnd )
{
/* Will be orderly A[L]~A[R-1] and A[R]~A[RightEnd] To merge into an ordered sequence */
int LeftEnd, NumElements, Tmp;
int i;
LeftEnd = R - 1; /* Left end position */
Tmp = L; /* Starting position of ordered sequence */
NumElements = RightEnd - L + 1;
while( L <= LeftEnd && R <= RightEnd ) {
if ( A[L] <= A[R] )
TmpA[Tmp++] = A[L++]; /* Copy left element to TmpA */
else
TmpA[Tmp++] = A[R++]; /* Copy right element to TmpA */
}
while( L <= LeftEnd )
TmpA[Tmp++] = A[L++]; /* Copy the rest on the left */
while( R <= RightEnd )
TmpA[Tmp++] = A[R++]; /* Copy directly the rest on the right */
for( i = 0; i < NumElements; i++, RightEnd -- )
A[RightEnd] = TmpA[RightEnd]; /* Will be orderly TmpA[] Copy back A[] */
}
void MergeSort(int a[],int low,int high){
if(low<high){
if(low<high){
int mid = (low+high)/2;
MergeSort(a,low,mid);
MergeSort(a,mid+1,high);
int b[6];
Merge(a,b,low,mid,high);
}
}
}
It's called
MergeSort(a,0,6);
Complete test code
#include<iostream>
using namespace std;
void Print(int a[],int n){
for(int i =0;i<n;i++)
cout << a[i] << " ";
cout << endl;
}
void BubbleSort(int a[],int n){
int i,j,flag;
int tmp;
for(i=n-1;i>=0;i--){
flag = 0;
for(j = 1;j<=i;j++){
if(a[j-1]>a[j]){
tmp = a[j];
a[j] = a[j-1];
a[j-1] = tmp;
flag = 1;
}
}
if(flag == 0)
return ;
}
}
void InsertSort(int a[],int n){
int i,j;
int tmp;
for(i=0;i<n;i++){
tmp = a[i];
j = i-1;
while(j>=0 && tmp<a[j]){
a[j+1] = a[j];
--j;
}
a[j+1] = tmp;
}
}
void QuickSort(int a[],int low,int high){
int tmp;
int i = low,j = high;
if(low<high){
tmp = a[low];
while(i<j){
while(j>i && a[j] >= tmp) j--;
if(i<j){
a[i] = a[j];
++i;
}
while(i<j && a[i] < tmp) ++i;
if(i<j){
a[j] = a[i];
--j;
}
}
a[i] = tmp;
QuickSort(a,low,i-1);
QuickSort(a,i+1,high);
}
}
void SelectSort(int a[],int n){
int i,j,k;
int tmp;
for(i=0;i<n;i++){
k = i;
for(j=i+1;j<n;j++)
if(a[k] > a[j])
k = j;
tmp = a[i];
a[i] = a[k];
a[k] = tmp;
}
}
void Sift(int a[],int low,int high){
int i = low,j = 2*i;
int tmp = a[i];
while(j<=high){
if(j<high && a[j] < a[j+1])
++j;
if(tmp<a[j]){
a[i] =a[j];
i = j;
j = 2*i;
}else
break;
}
a[i] = tmp;
}
void HeapSort(int a[],int n){
int i;
int tmp;
for(i=n/2;i>=1;i--)
Sift(a,i,n);
for(i=n;i>=2;--i){
tmp = a[1];
a[1] = a[i];
a[i] = tmp;
Sift(a,1,i-1);
}
}
void Merge( int A[], int TmpA[], int L, int R, int RightEnd )
{
/* Will be orderly A[L]~A[R-1] and A[R]~A[RightEnd] To merge into an ordered sequence */
int LeftEnd, NumElements, Tmp;
int i;
LeftEnd = R - 1; /* Left end position */
Tmp = L; /* Starting position of ordered sequence */
NumElements = RightEnd - L + 1;
while( L <= LeftEnd && R <= RightEnd ) {
if ( A[L] <= A[R] )
TmpA[Tmp++] = A[L++]; /* Copy left element to TmpA */
else
TmpA[Tmp++] = A[R++]; /* Copy right element to TmpA */
}
while( L <= LeftEnd )
TmpA[Tmp++] = A[L++]; /* Copy the rest on the left */
while( R <= RightEnd )
TmpA[Tmp++] = A[R++]; /* Copy directly the rest on the right */
for( i = 0; i < NumElements; i++, RightEnd -- )
A[RightEnd] = TmpA[RightEnd]; /* Will be orderly TmpA[] Copy back A[] */
}
void MergeSort(int a[],int low,int high){
if(low<high){
if(low<high){
int mid = (low+high)/2;
MergeSort(a,low,mid);
MergeSort(a,mid+1,high);
int b[6];
Merge(a,b,low,mid,high);
}
}
}
int main(){
int a[6] = {
0,6,9,1,4,3};
Print(a,6);
MergeSort(a,0,6);
Print(a,6);
return 0;
}
边栏推荐
- Tomato learning notes -vscade configuring makefile (using task.jason and launch.jason)
- math_排序不等式的推导
- ts中的联合类型和类型保护
- Signal函数大全:
- 基于单片机的氢气监测系统设计(#0491)
- 斑马888-TT不认新纸
- Have you used useeffect and uselayouteffect
- 汇编基础-CTF
- 8、某网络拓扑如图所示,路由器R1通过接口E1、E2分别连接局域网1、局域网2,通过接口L0连接路由器R2,并通过路由器R2连接域名服务器与互联网。R1的L0接口的IP地址是202.118.2.1/2
- Complete set of signal functions:
猜你喜欢

PHP QRcode generates QR code

Anaconda的基本使用

math_ Derivation of ordering inequality

HMS core graphics and image technology shows the latest functions and application scenarios, and accelerates the construction of digital intelligence life
![[the most complete and detailed] seven distributed global ID generation strategies](/img/ac/e16b424e00a78341344e6f7a590614.png)
[the most complete and detailed] seven distributed global ID generation strategies

Codeforces Global Round 21 A. NIT orz!

高项 成本分析
![[leetcode brush questions]](/img/f7/0e0714ddad1d6b2420ea38882891db.png)
[leetcode brush questions]

Random vibration calculation and simulation verification of single degree of freedom system
![[pytorch quantitative practice (1)]](/img/d4/26dec935fdbdb719a78254288ba585.png)
[pytorch quantitative practice (1)]
随机推荐
[summary of quantitative methods of pytorch model]
QT使用多线程
nn. Gru() use
Basic interview questions
基于单片机的氢气监测系统设计(#0491)
Huawei cloud stack opens its framework to the south to help ecological partners enter the cloud efficiently
指针常量与常量指针
Docker installs redis cluster
Solve the problem of Vue multi-level route cache invalidation solve the problem of multi-level route cache based on keep alive Vue keep alive cache invalidation Vue element admin multi-level route cac
You may not know the function of the third parameter of setTimeout
Chinese garbled code in kettle version 8.2
If you don't want to step on those holes in SaaS, you must first understand the "SaaS architecture"
Is it safe for Guosen Securities to open a mobile account? How to open an account
汇编基础-CTF
Talk about your understanding of reflection mechanism
[paper reading] IMPALA: V-trace
HMS Core图形图像技术展现最新功能和应用场景,加速构建数智生活
[gbase] modify the varchar length of the field
Liquibase first use
math_ Derivation of ordering inequality