C language sorting algorithm implementation

1 Overview

  The so-called sorting is the operation of arranging a string of records in increasing or decreasing order according to the size of one or some keywords in it. Sorting algorithm is a method of how to arrange records as required. Sorting algorithms have received considerable attention in many fields, especially in the processing of large amounts of data. An excellent algorithm can save a lot of resources. Taking into account various limitations and specifications of data in various fields, in order to obtain an excellent algorithm that is consistent with reality, a lot of reasoning and analysis must be done.
  Stability of sorting algorithm: Assume that there are multiple identical elements in the record sequence to be sorted. If sorted, the relative order of these records remains unchanged, that is, in the original sequence, r[i]=r[j], and If r[i] is before r[j], and in the sorted sequence, r[i] is still before r[j], then this sorting algorithm is called stable; otherwise, it is called unstable.
  Time complexity: that is, the time measurement taken from the initial state of the sequence to the position exchange processing of the sorting algorithm to obtain the final sorted result state. In computer science, time complexity, also known as time complexity, the time complexity of an algorithm is a function that qualitatively describes the running time of the algorithm. This is a function of the length of the string representing the input value to the algorithm. Time complexity is often expressed in O notation.

2. Bubble sort

  The implementation principle of bubble sort is to move smaller elements (larger elements) backward. By comparing adjacent elements pairwise, the positions of the two elements are reversed according to the comparison results, and the order is finally achieved from small to large or from large to small.
  The basic idea of ​​bubble sorting is: If you sort from large to small, compare the first element with the second element. If the first element is less than the second element, swap the positions of the two elements. Otherwise, do not move. ; Then compare the 2nd element and the 3rd element. If the 2nd element is less than the 3rd element, the positions of the two elements will be exchanged. Otherwise, they will not move; and so on, after one round, a minimum value will be found. to the very end. Next, in the second round of comparison, you can find the second smallest value and put it in the penultimate position. Until n-1 rounds of comparison are carried out, the data can be ordered.
  Bubble sort time complexity: O(n²) .
  Bubble sort is a stable sorting algorithm.
Insert image description here

#include <stdio.h>
int main()
{
    
    	
	int data[]={
    
    4,1,3,6,2,5};
	int i=0,j;
	int temp;
	int count=sizeof(data)/sizeof(int);
	for(i=0;i<count-1;i++)//比较轮数
	{
    
    
		for(j=0;j<count-1-i;j++)//每轮比较的次数
		{
    
    
			if(data[j]>data[j+1])
			{
    
    
				temp=data[j];
				data[j]=data[j+1];
				data[j+1]=temp;
			}
		}
	}
	printf("从小到大:");
	for(i=0;i<count;i++)
	{
    
    
		printf("%d ",data[i]);
	}
	printf("\n");
}

2.Quick sort

  The quick sort algorithm implements sorting through multiple comparisons and exchanges. The sorting process is as follows:
  (1) First set a dividing value, and divide the array into left and right parts through this dividing value.
  (2) Concentrate the data greater than or equal to the cut-off value to the right side of the array, and the data smaller than the cut-off value to the left side of the array. At this time, each element in the left part is less than the boundary value, and each element in the right part is greater than or equal to the boundary value.
  (3) Then, the data on the left and right can be sorted independently. For the array data on the left, you can take a dividing value and divide this part of the data into left and right parts. Similarly, place the smaller value on the left and the larger value on the right. The array data on the right can also be processed similarly.
  (4) Repeat the above process. It can be seen that this is a recursive definition. After sorting the left part through recursion, then recursively sort the order of the right part. When the data sorting of the left and right parts is completed, the sorting of the entire array is also completed.
  Quick sort (Quick_sort) is an improvement on bubble sort, and it is also a method of the bubble class.
  Quick sort time complexity: O(log2n) .
  Quicksort is an unstable sorting algorithm.
Insert image description here

#include <stdio.h>
void quicksort(int *arr,int first,int end);
int main()
{
    
    
	int arr[]={
    
    3,5,7,1,2,4,6};
	quicksort(arr,0,7-1);
	int i=0;
	for(i=0;i<7;i++)
	{
    
    
		printf("%d ",arr[i]);
	}
	printf("\n");
	return 0;
}
void quicksort(int *arr,int first,int end)
{
    
    
	if(first>=end)return ;
	int i=first;
	int j=end;
	int pivot=arr[i];
	int cnt=0;
	while(1)
	{
    
    
		while(arr[i]<=pivot && i<end)i++;//从前往后找大于pivot的数
		while(arr[j]>=pivot && j>first)j--;//从后往前找小于pivot的数据
		if(i>=j)break;
		//交换位置
		int temp=arr[i];
		arr[i]=arr[j];
		arr[j]=temp;
	}
	arr[first]=arr[j];
	arr[j]=pivot;
	quicksort(arr,0,j);//对前半部分排序
	quicksort(arr,j+1,end);//对后半部分排序
}

3. Insertion sort

  Insertion sort is also generally called direct insertion sort. It is an efficient algorithm for sorting a small number of elements. Insertion sort is the simplest sorting method. Its implementation principle is to insert an element into an ordered list. The implementation process can be achieved through two layers of loops. The outer loop controls the number of ordered lists, and the inner loop judges new elements and inserts new elements into the ordered list.

  • time complexity:

  In insertion sort, when the array to be sorted is in order, this is the optimal situation. You only need to compare the current number with the previous number. In this case, a total of N- 1 comparisons are required, and the time complexity is O(N ) .
  The worst case is that the array to be sorted is in reverse order. At this time, the most number of comparisons are required. The total number of comparisons is recorded as: 1+2+3+...+N-1. Therefore, the worst case time complexity of insertion sort is O (N²) .
  Insertion sort is suitable when some data is already in order, and the larger the ordered sequence, the better. Generally, it is not recommended to use insertion sort when the data size is greater than 1000 .
  Insertion sort is an unstable sorting algorithm.
Insert image description here

#include <stdio.h>
int main()
{
    
    
	char buff[]={
    
    3,5,7,1,2,64,6};
	int count=sizeof(buff)/sizeof(char);
	int i,j;
	int temp;
	for(i=1;i<count;i++)
	{
    
    
		for(j=i;j>0;j--)
		{
    
    
			if(buff[j]>buff[j-1])
			{
    
    
				temp=buff[j];
				buff[j]=buff[j-1];
				buff[j-1]=temp;
			}
		}
	}
	for(i=0;i<count;i++)
	{
    
    
		printf("%d ",buff[i]);
	}
	printf("\n");
}

4. Hill sorting

  Shell's Sort is a type of insertion sort, also known as "Diminishing Increment Sort". It is a more efficient and improved version of the direct insertion sort algorithm. This method is named after DLShell, which was proposed in 1959.
  Hill sorting is to group the elements to be sorted by insertion sort, and use the number of groupings of elements as the number of rounds. The number of groups is generally set to log2N (N is the number of members). Each time a round is completed, the number of groups is halved until the number of groups is 1, that is, the entire elements are divided into one group, that is, the sorting is completed.
  Hill sorting is an insertion sort of elements according to different step sizes. When the elements are very disordered at the beginning, the step size is the largest, so the number of elements in insertion sort is very small and the speed is very fast; when the elements are basically ordered, the step size is the largest. The length is very small, and insertion sort is very efficient for ordered sequences. Therefore, the time complexity of Hill sorting will be better than O(N²).
  Hill sort is a stable sorting algorithm.
Insert image description here

#include <stdio.h>
void shellSort(int *arr,int count)
{
    
    
	int gap=0;
	int temp;
	int i=0,j=0;
	for(gap=count>>1;gap>0;gap>>=1)
	{
    
    
		for(i=gap;i<count;i++)
		{
    
    
			temp=arr[i];
			j=i-gap;
			while(j>=0 && arr[j]<temp)
			{
    
    
				arr[j+gap]=arr[j];
				j-=gap;
			}
			arr[j+gap]=temp;
		}
	}
}
int main()
{
    
    
	int arr[]={
    
    1,2,3,4,5,6,7,8};
	int count=sizeof(arr)/sizeof(int);
	shellSort(arr,count);
	printf("从大到小排序:\n");
	for(int i=0;i<count;i++)
	{
    
    
		printf("%d ",arr[i]);
	}
	printf("\n");
}

5.Select sort

  Selection sort is a simple and intuitive sorting algorithm. Its working principle is: first select the smallest (or largest) element from the data elements to be sorted, store it at the beginning of the sequence, and then find the smallest (largest) element from the remaining unsorted elements. elements and place them at the end of the sorted sequence. And so on, until the number of all data elements to be sorted is zero.
  The time complexity of selection sort is O(n²) .
  Selection sort is an unstable sorting algorithm.
Insert image description here

void SelectSort(int *arr,int count)
{
    
    
	int i,j;
	int temp;
	int min;
	for(i=0;i<count-1;i++)
	{
    
    
		min=i;
		for(j=i+1;j<count;j++)
		{
    
    
			//找到最小的下下标
			if(arr[j]<arr[min])min=j;
		}
		temp=arr[i];
		arr[i]=arr[min];
		arr[min]=temp;
	}
}
void main(){
    
    
	int arr[] = {
    
    6,4,8,9,2,3,1,0};
	int count=sizeof(arr)/sizeof(int);
	SelectSort(arr,count);
	int i=0;
	printf("排序结果:");
	for(i=0;i<count;i++)
	{
    
    
		printf("%d ",arr[i]);
	}
	printf("\n");
}

Guess you like

Origin blog.csdn.net/weixin_44453694/article/details/131455168