Quick sort
- dichotomy
- Time complexity (nlogn)
//正序排列
void fast_sort(int *array_input, int start_pos, int stop_pos)
{
int temp_start_pos = start_pos;
int temp_stop_pos = stop_pos;
int compare_obj = array_input[temp_start_pos];
if(temp_start_pos < temp_stop_pos)
{
while(temp_start_pos < temp_stop_pos)
{
while(temp_start_pos < temp_stop_pos && array_input[temp_stop_pos] >= compare_obj)
{
temp_stop_pos--;
}
array_input[temp_start_pos++] = array_input[ temp_stop_pos];
while(temp_start_pos < temp_stop_pos && array_input[temp_start_pos] < compare_obj)
{
temp_start_pos++;
}
array_input[temp_stop_pos--] = array_input[ temp_start_pos];
}
array_input[temp_stop_pos] = compare_obj;
fast_sort(array_input, start_pos, temp_stop_pos-1);
fast_sort(array_input, temp_stop_pos+1, stop_pos);
}
}
Bubble Sort
- Look for the smallest item/the largest item in turn, and replace it directly at the time, paying attention to the difference with the selection sort
- Time complexity (n2)
//正序排列
void bubble_sort(int *array_input, int array_len)
{
int temp = 0;
for(int i = 0 ; i < array_len; i ++)
{
for(int j = array_len - 1; j > i; j--)
{
if(array_input[j] < array_input[j-1])
{
temp = array_input[j];
array_input[j] = array_input[j-1];
array_input[j-1] = temp;
}
}
}
}
Select sort
*Find the maximum/minimum items in turn, and replace only the number of positions in this round and the number of maximum and minimum items found each time
*Time complexity (n2)
void select_sort(int* _array, int _array_len)
{
int temp_mark = 0;
int temp_data = 0;
for (int i = 0; i < _array_len - 1; i++)
{
temp_mark = i;
temp_data = _array[i];
for (int j = i + 1; j < _array_len; j++)
{
if (_array[j] < temp_data)
{
temp_mark = j;
temp_data = _array[j];
}
}
_array[temp_mark] = _array[i];
_array[i] = temp_data;
}
}
Insertion sort
*Used for small data volume and more ordered data, where to insert the current value
*Time complexity (n(1~2))
void insert_sort(int* _array, int array_len)
{
int key_data = 0;
int j = 0;
for (int i = 1; i < array_len; i++)
{
key_data = _array[i]; //该位置前已经有序了
j = i - 1;
while (j >= 0 && key_data < _array[j]) //如果前面数小于比较数key_data,则往后移
{
_array[j + 1] = _array[j];
j--;
}
_array[j + 1] = key_data; //填入到查找到的位置
}
}
Hill sort
*Improved version of insertion sort, interval insertion sort
* Time complexity (n(1.3-2))
void interval_sort(int[] _array, int inter_v, int array_len)
{
int j = 0;
int temp = 0;
int k = 0;
int compare_date = 0;
for (int i = 0; i < inter_v; i++) //比较的大循环次数
{
j = i;
while ((j + inter_v) < array_len) //当前比较的节点
{
j += inter_v;// 从后往前
k = j;
compare_date = _array[k];
while (k - inter_v >= 0) //插入排序 从后往前
{
if (compare_date < _array[k - inter_v]) //前面的序列已排号
{
_array[k] = _array[k - inter_v];
k -= inter_v;
}
else
break;
}
_array[k] = compare_date;
}
}
}
void shell_sort(int[] _array, int array_len)
{
int interv = array_len / 2; //
while (interv > 0)
{
interval_sort(_array, interv, array_len);
interv = interv / 2;
}
}
String lookup
- Find the target string from the source string and return the first pointer to find
char *str_find(char *src, int src_len, const char *dst, int dst_len)
{
int i, j;
for (i=0; i<=src_len-dst_len; i++)
{
if (src[i] == dst[0])
{
for (j=1; j<dst_len; j++)
{
if (src[i+j] != dst[j])
break;
}
if (j == dst_len)
return &src[i];
}
}
return NULL;
}
C/C++ essence
Follow the official account to send C essence to get relevant documents