Seven kinds of commonly used sorting algorithm

1, bubble sort

The simplest type of sorting algorithm. Suppose length n array arr, be arranged in ascending order. DETAILED the bubble sort procedure can be described as follows: First, the first element of the array from the start to the last element in the array, the array of two adjacent elements are compared, if an element located at the left end of the array elements of the array is greater than the right end , then the switch position of the two elements in the array, the maximum value of all elements at this time is the rightmost array of elements in the array. Bubble sort the array followed by the n-1 remaining elements, until the whole ordered array. Time complexity of the algorithm is O (n ^ 2).

// 冒泡排序
void BubbleSort(int arr[], int length)
{
	for (int i = 0; i < length; i++)
	{
		for (int j = 0; j < length -  i - 1; j++)
		{
			if (arr[j] > arr[j + 1])
			{
				int temp;
				temp = arr[j + 1];
				arr[j + 1] = arr[j];
				arr[j] = temp;
			}
		}
	}
}

2. Select Sort

Yan Wei Min Edition "Data Structures" The basic idea is to select the sort described: in each pass n-i + 1 (i = 1,2, ..., n-1) th recording key selected as the smallest recording ordered sequence i-th recording. Specifically, assuming that the length n of the array ARR, to be arranged in ascending order, then the start of the n digital min1 minimum is found, if the position is not the minimum min1 leftmost array (i.e. not equal min1 arr [ 0]), then the minimum min1 and ARR [0] exchange, and then find the minimum value min2 in the remaining n-1 numbers, if not equal to the minimum value min2 arr [1], the exchange of these two numbers, And so on, until the array arr ordered. Time complexity of the algorithm is O (n ^ 2).

void  fun(int a[], int n)
{ int p, temp;
  for (int i = 0; i < n-1; i++) 
  {
    p = i;
    for (int j = i; j < n; j++)
      if(a[p] > a[j])
        p = j;
    temp = a[p];
	a[p] = a[i]; 
	a[i] = temp;
  }
}

void main()
{
  int a[N];
  printf("输入数据:\n") ;
  for(int i = 0; i < N; i++) 
	  scanf("%d",&a[i]);
  fun(a,N);
  printf("排序后的数据:\n") ;
  for(int i = 0; i < N; i++) 
	  printf("%5d ",a[i]); 
  printf("\n");
}

3, insertion sort

 Insertion sort of the basic idea is to disorderly sequence into an ordered sequence. To e.g. array arr = [4,2,8,0,5,1] sorting, 4 can be seen as an ordered sequence (FIG indicated in blue), the [2,8,0,5 , 1] treated as a random sequence. Random sequence is smaller than 24, then the insert 2 to the left of 4, this time ordered sequence becomes [2,4], into a random sequence [8,0,5,1]. Random sequence is greater than 84, then the insert 8 to the right 4, the ordered sequence becomes [2,4,8], a random sequence becomes [0,5,1]. And so, ultimately array in ascending order. The time complexity of the algorithm is O (n ^ 2).

// 插入排序
void InsertSort(int arr[], int length)
{
	for (int i = 1; i < length; i++)
	{
		int j;
		if (arr[i] < arr[i - 1])
		{
			int temp = arr[i];
			for (j = i - 1; j >= 0 && temp < arr[j]; j--)
			{
				arr[j + 1] = arr[j];
			}
			arr[j + 1] = temp;
		}
	}
}

4, Hill sorting

Hill sorting (Shell's Sort) improved on the basis of insertion sort algorithm, the time complexity of the algorithm is compared with the previous several algorithms greater improvement. The basic idea of ​​the algorithm is: first row to be recorded is divided into several sub-sequence insertion sort sequences, while "substantially ordered" to be recorded throughout the sequence, and then once for all records direct insertion sort.

// 插入排序
void ShellSort(int arr[], int length)
{
	int increasement = length;
	int i, j, k;
	do
	{
		// 确定分组的增量
		increasement = increasement / 3 + 1;
		for (i = 0; i < increasement; i++)
		{
			for (j = i + increasement; j < length; j += increasement)
			{
				if (arr[j] < arr[j - increasement])
				{
					int temp = arr[j];
					for (k = j - increasement; k >= 0 && temp < arr[k]; k -= increasement)
					{
						arr[k + increasement] = arr[k];
					}
					arr[k + increasement] = temp;
				}
			}
		}
	} while (increasement > 1);
}

  5, Quick Sort

The basic idea is to quickly sort: sort by a trip to discharge record to be divided into two independent parts, where the key part of the record key is smaller than the other part of the record, you can record each of the two parts continue to sort, It has reached the entire sequence order. Before selecting a record (usually selecting the first record) to be sorted from any column as a reference value, then it is smaller than the record key of the records are arranged in its place: the specific trip quicksort process can be described as the record key than its big records are placed behind its position. Thus, the reference is to the boundary, the two sequences to be sorted into the column.

Specifically trip quick sort of: providing two pointers point to be low and high, respectively, the beginning and end of the sorted column, record the reference value baseval (records to be sorted first column), then start the position referred to high a search forward until you find and less than a recording baseval exchange, and then from the location pointed to low back search until it finds a record is larger than baseval exchange and, repeating these two steps until the low = high up.

// 快速排序
void QuickSort(int arr[], int start, int end)
{
	if (start >= end)
		return;
	int i = start;
	int j = end;
	// 基准数
	int baseval = arr[start];
	while (i < j)
	{
		// 从右向左找比基准数小的数
		while (i < j && arr[j] >= baseval)
		{
			j--;
		}
		if (i < j)
		{
			arr[i] = arr[j];
			i++;
		}
		// 从左向右找比基准数大的数
		while (i < j && arr[i] < baseval)
		{
			i++;
		}
		if (i < j)
		{
			arr[j] = arr[i];
			j--;
		}
	}
	// 把基准数放到i的位置
	arr[i] = baseval;
	// 递归
	QuickSort(arr, start, i - 1);
	QuickSort(arr, i + 1, end);
}

6, merge sort

"Merge" means that the combination of two or more ordered sequences into a new ordered list. It assumed that the initial sequence contains n records, it can be seen as the n ordered subsequences, each subsequence of length 1, then merge twenty-two give (represents the smallest integer not less than x) of length 2 (or 1) an ordered sequence, and then merge twenty-two. Repeat, until a length of up to an ordered sequence of n. This sequencing method is called 2-way merge sort.

// 归并排序
void MergeSort(int arr[], int start, int end, int * temp)
{
	if (start >= end)
		return;
	int mid = (start + end) / 2;
	MergeSort(arr, start, mid, temp);
	MergeSort(arr, mid + 1, end, temp);
 
	// 合并两个有序序列
	int length = 0; // 表示辅助空间有多少个元素
	int i_start = start;
	int i_end = mid;
	int j_start = mid + 1;
	int j_end = end;
	while (i_start <= i_end && j_start <= j_end)
	{
		if (arr[i_start] < arr[j_start])
		{
			temp[length] = arr[i_start]; 
			length++;
			i_start++;
		}
		else
		{
			temp[length] = arr[j_start];
			length++;
			j_start++;
		}
	}
	while (i_start <= i_end)
	{
		temp[length] = arr[i_start];
		i_start++;
		length++;
	}
	while (j_start <= j_end)
	{
		temp[length] = arr[j_start];
		length++;
		j_start++;
	}
	// 把辅助空间的数据放到原空间
	for (int i = 0; i < length; i++)
	{
		arr[start + i] = temp[i];
	}
}

7 heap sort

/*
	@param arr 待调整的数组
	@param i 待调整的结点的下标
	@param length 数组的长度
*/
void HeapAdjust(int arr[], int i, int length)
{
	// 调整i位置的结点
	// 先保存当前结点的下标
	int max = i;
	// 当前结点左右孩子结点的下标
	int lchild = i * 2 + 1;
	int rchild = i * 2 + 2;
	if (lchild < length && arr[lchild] > arr[max])
	{
		max = lchild;
	}
	if (rchild < length && arr[rchild] > arr[max])
	{
		max = rchild;
	}
	// 若i处的值比其左右孩子结点的值小,就将其和最大值进行交换
	if (max != i)
	{
		int temp;
		temp = arr[i];
		arr[i] = arr[max];
		arr[max] = temp;
		// 递归
		HeapAdjust(arr, max, length);
	}
}
 
// 堆排序
void HeapSort(int arr[], int length)
{
	// 初始化堆
	// length / 2 - 1是二叉树中最后一个非叶子结点的序号
	for (int i = length / 2 - 1; i >= 0; i--)
	{
		HeapAdjust(arr, i, length);
	}
	// 交换堆顶元素和最后一个元素
	for (int i = length - 1; i >= 0; i--)
	{
		int temp;
		temp = arr[i];
		arr[i] = arr[0];
		arr[0] = temp;
		HeapAdjust(arr, 0, i);
	}
}

 

Published 407 original articles · won praise 150 · views 380 000 +

Guess you like

Origin blog.csdn.net/ds1130071727/article/details/103240589