C + + implementation sequencing (simple INSERT, hill, select, fast, bubbling, heap)

Source: Internet
Author: User

Simple Insert Sort

Applies to records that have fewer and basically ordered records. algorithm thought: given a sequence of the existence of the dividing line, the dividing line to the left order, the right disorderly, in order to the right of the number of the left is not ordered to compare the sequence, insert the corresponding position, and then make corresponding adjustments to the dividing line, the following diagram to illustrate.

The code is as follows:

Time complexity: Best case O (n), worst O (n^2).

Hill sort

The hill sort is improved after the simple insert sort. algorithm idea: sort the sequence in groups, and finally make a simple insert sort.

As for how to group, I will show you the following diagram

The subscript of these numbers starts with 0, that is, 0,3, 6,9 is a group, 1,4,7 is a group, and 2,5,8 is a group. That is, the gap%3 subscript is equal to a group. Gap=gap/3+1.

The code is as follows:

void Shellsort (int *a, size_t size)//Hill Sort
{
ASSERT (a);
int gap = size;
while (Gap > 1)
{
Gap = GAP/3 + 1;
for (int i = gap; i < size; ++i)
{
int index = i;
int tem = A[index];
int end = INDEX-GAP;
while (end >= 0 && tem < A[end])
{
A[end + gap] = A[end];
End-= Gap;
}
A[end + gap] = tem;
}
}
}

Time Complexity of O (n^1.5)

Select sort

Algorithm idea: Each loop finds the minimum value and swaps it.

The code is as follows:

void Selectsort (int *a, size_t size)
{
ASSERT (a);
for (int i = 0; i < size; ++i)
{
int minindex = i;
int tem;
for (int j = i + 1; j < size; ++j)
{
if (A[j] < A[minindex])
{
Minindex = j;
tem = A[minindex];
A[minindex] = A[i];
A[i] = tem;
}
}
}
}

Time complexity O (n^2)

Quick Sort

Algorithm idea: Select key, adjust key to a reasonable position, so that all the left is less than key, all the right is greater than key;

How to adjust the key to the appropriate location, here to use the method of three-digit.

Note: If the sequence is basically ordered or the number of sequences is small, a simple insert sort can be used, because fast sorting is inefficient for these situations;

The code is as follows:

int getmidindex (int *a, int left, int. right)/////////three count in/////////
{
ASSERT (a);
int mid=left+ (right-left)/2;
if (left < right)
{
if (A[mid] < A[left])
return left;
else if (A[mid] < a[right])
return mid;
Else
return right;
}
Else
{
if (A[mid] < a[right])
return right;
else if (A[mid] < A[left])
return mid;
Else
return left;
}
}

int partionsort (int *a, int left, int. right)
{
int midindex = Getmidindex (A, left, right);
Swap (A[midindex], a[right]);
int cur = left;
int prev = left-1;
while (cur < right)
{
if (A[cur] < A[right] && ++prev! = cur)
{
Swap (A[cur], A[prev]);
}
++cur;
}
++prev;
Swap (A[prev], a[right]);
return prev;
}

void QuickSort (int *a,int left,int right)
{
ASSERT (a);
int size = Right-left + 1;
if (Right-left > 13)//////////Optimization: When the length is greater than 13 with a fast row, less than 13 is reduced to a simple insert sort////////////
{
int boundary = Partionsort (A, left, right);
QuickSort (A, left, boundary-1);
QuickSort (A, boundary + 1, right);
}
Else
Insertsort (A, size); Simple Insert Sort
}

Bubble sort

Algorithm thought: 22 Compare again exchange, a trip sort down can only find one biggest, the rest are disorderly order, repeat this can follow from small to large order down.

The code is as follows:

void Bubblesort (int *a, size_t size)
{
ASSERT (a);
for (int i = 0; i < size; i++)
{
for (int j = 0; j = size-1-I; j + +)
{
if (A[j] > a[j + 1])
{
int tem = A[J + 1];
A[j + 1] = A[j];
A[J] = tem;
}
}
}
}

The above bubble sort has an efficiency problem, when the sequence basically close to the order, then does not need to sort, the above code will carry on the constant comparison, affects the efficiency, therefore makes the following improvement, sets up a Boolean variable flag,

If no elements have been exchanged in a loop, the order is already queued.

Optimization:

void Bubblesort (int *a, size_t size)
{
ASSERT (a);

BOOL Flag=true;
for (int i = 0; i < size; i++)
{

BOOL Flag=false;
for (int j = 0; j = size-1-I; j + +)
{
if (A[j] > a[j + 1])
{
int tem = A[J + 1];
A[j + 1] = A[j];
A[J] = tem;

BOOL Flag=true;
}
}
}
}

Heap Sort

Algorithm idea: the backlog is built into a large heap, and then the heap top data and the last leaf node of the heap data exchange, and then re-adjust to a large heap, the number of data per heap minus 1.

In turn ...

The code is as follows:

void Heapsort (int *a, size_t size)//heap Sort
{
ASSERT (a);
for (int i = (size-2)/2; I >= 0; i.)
{
Adjustdown (A, size, i);
}
for (int i = 0; i < size; ++i)
{
Swap (a[0], a[size-i-1]);
Adjustdown (A, size-i-1, 0);
}
}

Build a large heap of/////////

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
}
}
}

C + + implementation sequencing (simple INSERT, hill, select, fast, bubbling, heap)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.