# Several sort implementations for data structure - Quick sort, Bubble sort, Insert sort directly, Select sort directly, Heap sort

1. Insert Sort Directly

Select one value to be sorted at a time to insert into the existing sorting sequence;

(4,2,6,5,1,8) For an example of incremental ordering:

1. key=2; less than 4, 4 move right, 2 insert 4, 246518
2. key=6; position fixed, 246518
3. key=5; 6 moved backward, 5 inserted; 245618
4. .... Repeat the above steps until the last number is inserted

Implementation code:

```//Direct insert sort***************************************************************//
void insert_sort(int a[],int n)
{
int tem;
for (int i=0;i<n-1;i++)
{
tem=a[i+1];
for (int j=i;j>=0;j--)
{
//Ascending order example//
if (a[j]>tem)
{
a[j+1]=a[j];
a[j]=tem;
}
else
{
a[j+1]=tem;
break;
}
}
}
for (int i=0;i<n;i++)
{
cout<<"Insert Sort Directly:"<<a[i]<<endl;
}
}```

2. Direct Selection Sorting

Select the smallest number in an array at a time

```void xuanze_sort(int a[],int n)
{
//Ascending Sort//
int min,min_index=0;
for(int i=0;i<n-1;i++)
{
min=a[i];
for (int j=i+1;j<n;j++)
{
if (min>a[j])
{
min=a[j];
min_index=j;
}
}
if (min<a[i])
{
a[min_index]=a[i];
a[i]=min;
}
}
for (int i=0;i<n;i++)
{
cout<<"Direct Sorting:  "<<a[i]<<endl;
}
}```

3. Bubble method

Each round compares adjacent elements, swapping positions based on size

```void maopao(int a[],int n)
{
bool flag=true;
int tem=0;
for (int i=0;i<n-1;i++)
{
for(int j=0;j<n-i-1;j++)
{
if (a[j]<a[j+1])
{
tem=a[j];
a[j]=a[j+1];
a[j+1]=tem;
}
}
}
for(int i=0;i<n;i++)cout<<a[i]<<endl;
}```

4. Quick sorting (recursive implementation)

Each time the first number is the datum (ascending order, for example), less than the left of the datum, greater than the right of the datum.

Then do the same for the left sequence and the right sequence, calling recursion until all numbers are arranged.

```void quicksort(int a[],int left,int right)
{
if (left>=right)return;
//Ascending Sort//
int i=left,j=right,num_cmp=a[left];
while (i<j)
{
while (i<j&&a[j]>=num_cmp)
{
j--;
}
a[i]=a[j];
while (i<j&&a[i]<=num_cmp)
{
i++;
}
a[j]=a[i];
}
a[j]=num_cmp;

quicksort(a,left,i-1);
quicksort(a,i+1,right);

}```

5. Heap sorting (recursive implementation)

Ascending order as an example:

Each time you build a big root heap,

Number Exchange of Root and Last Leaf Nodes

Rebuild the large root heap for the first n-1 numbers;

Loop front operation.

```void stack_sort(int a[],int n)
{
if (n==1)
{
cout<<"stack_sort :"<<a[0]<<endl;
return;
}
//Build a large root heap//
int tem;
bool is_even;
if(n%2==0)is_even=true;
else is_even=false;
for (int i=n/2;i>0;i--)
{
if (is_even)
{
//Since the array ordinal starts at 0, all binary tree ordinals correspond to the array position ordinal minus one
if(a[2*i-1]>a[i-1])
{
tem=a[2*i-1];
a[2*i-1]=a[i-1];
a[i-1]=tem;
}
is_even=false;
}
else
{
if (a[2*i-1]>a[2*i])
{
if (a[2*i-1]>a[i-1])
{
tem=a[2*i-1];
a[2*i-1]=a[i-1];
a[i-1]=tem;
}
}
else
{
if (a[2*i]>a[i-1])
{
tem=a[2*i];
a[2*i]=a[i-1];
a[i-1]=tem;
}
}
}

}
//Build Initial Large Root Heap End//
//Root and Last Cotyledon Exchange Location//
tem=a[0];
a[0]=a[n-1];
a[n-1]=tem;

//Recursively calls itself to rebuild the big root heap for the first n-1 remaining data//
stack_sort(a,n-1);
cout<<"stack_sort: "<<a[n-1]<<endl;
}```

Tags: less

Posted on Sat, 30 Nov 2019 16:20:09 -0500 by motofzr1000