Primary data structure - sorting

1. The concept of sorting

Sorting is the operation of arranging a series of data according to specific rules.

Common Sorting Algorithms

//排序实现的接口
void InsertSort(int* a,int n);   //插入排序

void ShellSort(int* a,int n);    //希尔排序

void SelectSort(int* a, int n);   // 选择排序

void AdjustDwon(int* a, int n, int root);
void HeapSort(int* a, int n);// 堆排序

// 冒泡排序
void BubbleSort(int* a, int n)

// 快速排序递归实现
// 快速排序hoare版本
int PartSort1(int* a, int left, int right);
// 快速排序挖坑法
int PartSort2(int* a, int left, int right);
// 快速排序前后指针法
int PartSort3(int* a, int left, int right);
void QuickSort(int* a, int left, int right);

// 快速排序 非递归实现
void QuickSortNonR(int* a, int left, int right)


// 归并排序递归实现
void MergeSort(int* a, int n)
// 归并排序非递归实现
void MergeSortNonR(int* a, int n)

// 计数排序
void CountSort(int* a, int n)

2. Implementation of common sorting

1. Insertion sort

direct insertion

The direct insertion method is a simple insertion sort method, which inserts the elements to be sorted into a sorted sorted sequence one by one to form a new sorted sequence.

It's like inserting {3, 6, 1, 5} into {2, 4}, you get {1, 2, 3, 4, 5, 6}.

So how is it achieved?

Inline Features

  •  1. The closer the element is to the order before insertion, the higher the insertion efficiency
  • 2. Time complexity: O(N^2)
  • 3. Space complexity: O(1)
  • 4. Stability: stable

Hill sort (shrinking incremental sort)

The Hill sorting method is to first select an integer, divide the elements to be sorted into several groups, divide all the elements with equal distances into the same group, and sort each group of elements.

Features of Hill Sort

  •  1. Hill sort is an optimization of direct insertion sort
  • 2. When gap>1 is pre-sorted, the purpose is to make the sorted elements closer to order.
  • 3. Time complexity: O(N^1.3 - N^2)
  • 4. Stability: Unstable

2. Selection sort

Select the smallest (largest) one from the elements to be sorted each time, and place it at the beginning of the sequence until all elements are exhausted.

direct selection sort

Direct selection is to compare the elements to be sorted, select the largest or smallest one, and then repeat this operation for the remaining elements until the remaining elements are 1, and stop the operation.

Features of Direct Selection Sort

  • 1. Direct selection sorting is easy to understand, but the efficiency is not very good, and it is rarely used in practice
  • 2. Time complexity: O(N^2)
  • 3. Space complexity: O(1)
  • 4. Stability: Unstable

 heap sort

Heap sort is a sorting algorithm designed using the data structure of the heap, and it is a kind of selection sort. Remember to build large heaps in ascending order and small heaps in descending order.

 Features of Heap Sort

  • 1. Heap sort uses the heap to select numbers, which is much more efficient.
  • 2. Time complexity: O(N*logN)
  • 3. Space complexity: O(1)
  • 4. Stability: unstable

3. Swap sort

Swap the order of the two elements according to the result of the comparison of the two elements, moving the larger value to the end and the smaller value to the front.

Bubble Sort

Features of bubble sort

  •  1. Bubble sort is a very easy to understand sort
  • 2. Time complexity: O(N^2)
  • 3. Space complexity: O(1)
  • 4. Stability: stable

quicksort

Quicksort is a binary tree exchange sorting method. Any element is taken as the reference value, and then the set is divided into two sequences, the left is less than the right, and the same is repeated.

Common ways to divide an interval into left and right halves according to the base value are:

  • 1. Hoare version
  • 2. Digging method
  • 3. Front and rear pointer version

4. Merge Sort

Merge sort is to sort the existing subsequences and merge two sorted lists into one sorted list.

Merge Sort Features

  • 1. The disadvantage of merging is that it requires O(N) space complexity. The thinking of merge sorting is more to solve the problem of external sorting in the disk.
  • 2. Time complexity: O(N*logN)
  • 3. Space complexity: O(N)
  • 4. Stability: stable

5. Non-comparative sorting

Features of non-comparative sorting

  •  1. When the count sorting is concentrated in the data range, the efficiency is very high, but the applicable scope and scenarios are limited.
  • 2. Time complexity: O(MAX(N, range))
  • 3. Space complexity: O (range)
  • 4. Stability: stable

Guess you like

Origin blog.csdn.net/qq_59392324/article/details/122009954