Common sorting algorithm share of quick sort algorithm hill sort

  • 2020-04-02 02:15:50
  • OfStack

Sorting out several sorting algorithms, through the test, the fastest or fast sorting algorithm, is not an order of magnitude of speed.


#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdbool.h>
#include <time.h>
#include <unistd.h>
//Some sort of sorting algorithm
//Insertion sort algorithm
//Direct insertion sort
void
direct_insert_sort(int *a,int len)
{
 //Idea: compare the last one in turn with the previous one
 //I'm going to move back, of course, from the beginning
 //Start with the smallest comparison array and grow from there
 //To the entire array
 int i,j,temp;
 for(i = 1;i < len;++i) {
  //Gets the last index data
  temp = a[i];
  for(j = i - 1;j >= 0;--j) {
   //Let's start with the penultimate one
   if(a[j] > temp)//Ascending order
    a[j + 1] = a[j];
   else
    break;//Exit immediately
  }
  //Insert the last position into the appropriate position
  a[j + 1] = temp;
 }
}
//Hill sorting
void
shell_insert_sort(int *a,int len)
{
 // Train of thought: namely ratio Direct insertion sort Many layers 
 //Loop, this layer of loop is used to control the step press
 //And the way the algorithm works is that you can reduce
 //Switching frequency
 int i,j,h,temp;
 for(h = len / 2;h > 0;h /= 2) {
  //The inner layer is actually directly inserted
  //Algorithm ideas
  //Notice I += h and I ++
  //The influence of the law
  for(i = h;i < len;i += h) {
   //Gets the value of the last index
   temp = a[i];
   for(j = i - h;j >= 0;j -= h) {
    if(a[j] > temp)//Ascending order
     a[j + h] = a[j];
    else
     break;
   }
   //Inserts the location found into the last index
   a[j + h] = temp;
  }
 }
}
//Selection sort
//Bubble sort
void
bubble_swap_sort(int *a,int len)
{
 //Idea: start pairwise at the end of the array
 //Gradually exchange the underlying data that meets the requirements
 // To the top, which can lead to Switching frequency Too much 
 int i,j,temp;
 //If no exchange occurs in a bubble
 //Consider the permutation over
 bool exchange = false;
 for(i = 0;i < len - 1;++i) {
  for(j = len - 1;j > i;--j) {
   //And if that's the case, we're going to swap
   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 an array
 //Then arrange the array so that both sides of it are
 //Is greater than or less than this value,
 //And then recursively sort it
 //The advantage is finding the middle value every time
 //In exchange for many times.
 int _low,_high,qivot;
 if(low < high) {
  _low = low;
  _high = high;
  //Let's start with the last one
  qivot = a[low];
  //The way to find the middle value is to approach it gradually
  //From the beginning to the end, by the way
  //Sort the
  while(_low < _high) {
   //And since we're starting at low, first of all
   //So let's go from high to something smaller than qivot in liters
   //Sequence alignment)
   while(_low < _high && a[_high] > qivot)
    --_high;//Gradually inching towards the middle
   if(_low < _high)//Must have found a[_high]> The condition of the qivot
    a[_low++] = a[_high];
   //So now a[_high] has cleared the space, so I'm going to go from low
   //More data than qivot
   while(_low < _high && a[_low] < qivot)
    --_low;//Close to the middle
   if(_low < _high)
    a[_high--] = a[_low];
  }
  //And then finally _low == _high so this is the position of qivot
  a[_low] = qivot;
  //Recursive down
  quick_swap_sort(a,low,_high - 1);
  quick_swap_sort(a,_low + 1,high);
 }
}
//Selection sort
// directly Selection sort
void
direct_select_sort(int *a,int len)
{
 //Train of thought: it is to find the extreme value through the number group
 //Put it on the head or tail, and then gradually shrink
 //Range to the smallest comparison array
 int i,j,pos,temp;
 for(i = 0;i < len - 1;++i) {
  //Getting a value from scratch is assumed to be extreme
  pos = i;
  for(j = i + 1;j < len;++j) {
   //Meet the conditions
   if(a[pos] > a[j])//ascending
    pos = j;
  }
  if(pos != i) {
   //swapping
   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;
}


Related articles: