Common sorting algorithm collation sharing (fast sorting algorithm, Hill sort) _c language

Source: Internet
Author: User

Sorted out a few sorting algorithms, through the test, the fastest or fast sorting algorithm, is simply not an order of magnitude speed.

Copy Code code as follows:

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdbool.h>
#include <time.h>
#include <unistd.h>

//Some sort algorithm collation
//Insert Sort algorithm
//Direct insert sort
void
direct_insert_sort (int *a,int len)
{
 // Train of thought: The last one in turn is compared to the preceding,
 //moves back the conditions that are satisfied, of course from the beginning
 //and gradually expands from the smallest comparison array
 //to the entire array
 int i,j, Temp
 for (i = 1;i < Len;++i) {
  //get the last index data
  temp = A[i];
  for (j = i-1;j >= 0;--j) {
   //from the penultimate start
   if (A[j] > Temp)/ Ascending order
    a[j + 1] = A[j];
   else
    break;//immediately quits
  
  // Insert the last position into the appropriate location
  a[j + 1] = temp;
 }
}

//Hill sort
void
shell_insert_sort (int *a,int len)
{
 //idea: It's more than a layer
 //loop that inserts a direct sort. This loop is used to control the stepping press
 //The original idea of the algorithm is this can reduce
 //switching times
 int i,j,h,temp;
 for (h = len/2;h > 0;h/= 2) {
  //inner layer is actually directly inserted
  //algorithm idea
  //NOTE i + = h and I + + The effect of the
  //method
  for (i = h;i < Len;i + + h) {
   //get the value of the last index
   temp = A[i];
   for (j = i-h;j >= 0;j-= h) {
    if (A[j] > Temp)//ascending order
 &nbs P;   a[j + h] = a[j];
    else
     break;
&NBSP;&NBSP;&NBSP}
   //inserts the found location into the last index
   a[j + h] = temp;
&NBSP;&NBSP}
 }

Select sort
Bubble sort
void
Bubble_swap_sort (int *a,int len)
{
Idea: Starting at the end of the array 22 comparison
Gradually swapping the underlying data to meet the requirements
To the upper level, which may result in too many exchange times
int i,j,temp;
If swapping does not occur in a bubble, you can
To think this arrangement is over
BOOL Exchange = FALSE;
for (i = 0;i < Len-1;++i) {
for (j = len-1;j > I;--j) {
When the conditions are met, they are exchanged.
if (A[j] < a[j-1]) {
temp = A[j];
A[J] = a[j-1];
A[J-1] = temp;
Exchange = TRUE;
}
}
if (Exchange)
Exchange = FALSE;
Else
Break
}
}

Quick Sort
void
Quick_swap_sort (int *a,int low,int High)
{
Idea: Find a value from the array
Then arrange the array so that both sides are
is greater than or less than this value,
And then recursively sort it down
The advantage is that every time you find an intermediate value,
In exchange for many times.
int _low,_high,qivot;
if (Low < high) {
_low = low;
_high = high;
Here, starting with the last one.
Qivot = A[low];
The way to find the middle value is to approach it gradually.
Starting at the end of the head, and by the way
Sort of
while (_low < _high) {
Since it's starting from low, then first
From high to find less than Qivot (l
Order)
while (_low < _high && A[_high] > Qivot)
--_high;//gradually toward the middle.
if (_low < _high)//Must be found A[_high] > Qivot situation
a[_low++] = A[_high];
The next A[_high] vacated the position, so from low to find
Data larger than Qivot.
while (_low < _high && A[_low] < Qivot)
--_low;//approaching the middle.
if (_low < _high)
a[_high--] = A[_low];
}
Finally _low = = _high so this position is qivot position.
A[_low] = Qivot;
Pass it down.
Quick_swap_sort (a,low,_high-1);
Quick_swap_sort (A,_low + 1,high);
}
}

Select sort
Direct Selection Sort
void
Direct_select_sort (int *a,int len)
{
Idea: to traverse an array to find the Extremum
Put your head or tail down so that it shrinks.
Range to smallest comparison array
int i,j,pos,temp;
for (i = 0;i < Len-1;++i) {
Get a value from scratch assume the extremum
pos = i;
for (j = i + 1;j < len;++j) {
Meet the conditions
if (A[pos] > A[j])/Ascending
pos = j;
}
if (pos!= i) {
to Exchange
temp = A[pos];
A[pos] = A[i];
A[i] = temp;
}
}
}

void
DISP (int *a,int len)
{
int i = 0;
for (; i < len;i++) {
if (i!= 0 && i% 16 = 0)
printf ("\ n");
printf ("%d", a[i]);
}
printf ("\ n");
}

#define Test_array_len 100000
#define TEST_COUNT 1

Int
Main (int Argc,char *argv[])
{
int a[] = {1,8,4,0,9,6,3,7,2,18,74,5,64,12,39};
int len = sizeof (a)/sizeof (a[0]);
Direct_insert_sort (A,len);
Shell_insert_sort (A,len);
Bubble_swap_sort (A,len);
Quick_swap_sort (a,0,len-1);
Direct_select_sort (A,len);
Disp (A,len);

return 0;
}

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.