Data structure: common sorting algorithm (6): quick sort (C++ implementation)

Data structure: common sorting algorithm (6): quick sort

1. Basic idea:

Select a reference element, usually the first element or the last element, through a round of scanning, the sequence to be sorted is divided into two parts, one is smaller than the reference element, and the other is greater than or equal to the reference element, and then the reference element is sorted in it Then use the same method to sort the two parts recursively until there is only one number in each interval.

2. Examples

Example: the array a[15]={3,44,38,5,47,15,36,26,2,46,4,19,50,48} is sorted by quick sort

img

img

Image source: https://www.cnblogs.com/zwtgyh/p/10631760.html

Example code:

//辅助函数:交换值
void exchange(int * a, int* b) {
  int temp = *a;
  *a = *b;
  *b = temp;
}

//打印数组函数
void print_arr(int *a, int size) 
{
  cout << "打印数组:";
  for (int i = 0; i<size; i++)  //打印数组 
  {
    cout << a[i] << " ";
  }
  cout << endl << endl;
}
/*序列划分函数*/
int partition(int a[], int p, int r) {
  int key = a[r];//取最后一个
  int i = p - 1;
  for (int j = p; j < r; j++)
  { 
    if (a[j] <= key)
    {     
      i++;
      //i一直代表小于key元素的最后一个索引,当发现有比key小的a[j]时候,i+1 后交换     
      exchange(&a[i], &a[j]);
    }   
  } 
  exchange(&a[i + 1], &a[r]);//将key切换到中间来,左边是小于key的,右边是大于key的值。
  return i + 1;
}
  
void quickSort(int a[], int p, int r) {
  int position = 0;
  if (p<r)
  {
    position = partition(a,p,r);//返回划分元素的最终位置
    quickSort(a,p,position-1);//划分左边递归
    quickSort(a, position + 1,r);//划分右边递归
  } 
}
  //主函数
void main() {
  int a[15]={3,44,38,5,47,15,36,26,2,46,4,19,50,48};
  cout << "输入数组 a[15]={3,44,38,5,47,15,36,26,2,46,4,19,50,48} " << endl; 
  quickSort(a, 0, 14);
  print_arr(a, 15);
  
}

3. Summary:

  • The best case: each round of division divides the sequence to be sorted into two parts, so the time required for each part is 1/2 of the previous round. If a sequence of n elements is sorted, the depth of the recursion tree is [logn]+1, which means that only logn recursions are required, and the total time required is T(n), the first time you need to scan the entire sequence, do n comparisons, and then The sequence is divided into two, and the two parts need T(n/2) time respectively, which are divided in order: T(n) = 2 T(n/2)+n T(n) = 2 (2*(T (n/4)+n/2)+n = 4 T(n/4)+2n and so on, and T(1) = 0, so T(n) = n T(1) + n*logn = O (nlogn)
  • Worst case: When the sequence to be sorted is an ordered sequence (positive or reverse), the situation obtained after each division is that there is 1 element on one side and the remaining elements on the other side, and finally n-1 rounds of looping , And the i-th cycle needs ni comparisons, the total number of comparisons is n-1 + n-2 +… + 1 = n(n-1)/2, that is, the time complexity is O(n^2)
  • Quick sort is in-situ sorting and does not require additional memory space.
  • Quick sort is not a stable sort, it will destroy the stability during the exchange process.

Guess you like

Origin blog.csdn.net/qq_43801020/article/details/108137304
Recommended