mobile wallpaper 1mobile wallpaper 2mobile wallpaper 3mobile wallpaper 4
479 words
2 minutes
Sorting and Binary Search Study Notes
2022-07-08

AcWing Basic Course Study

Sorting#

1. Quick Sort#

1. Principle#

For a segment of an unordered sequence, if you want to sort it, you can proceed as follows:

  • For a segment of numbers, you can first pick an arbitrary point mid as the pivot. (Where mid is generally the middle of the sequence)
  • Perform a single pass on this segment, placing numbers greater than mid to the right, and numbers less than mid to the left.
  • Then for the partitioned subsequences, select its [l,p] and [p+1,r] two subsegments to continue the above operation, until the length is 1. (Where p is the boundary between the left and right ends)

2. Implementation (Code)#

This code idea is adapted from Mr. Y

void quick_sort(int a[],int l,int r){
if(l>=r) return;//设置退出条件
int i=l-1,j+1,mid = a[l+r>>1];//设置判断点
while(i<j){
do i++;while(a[i]<mid);
do j--;while(a[j]>mid);
if(i<j) swap(a[i],a[j]);
}
quick_sort(a,l,j);//继续对子段排序
quick_sort(a,j+1,r);
}
  • Time complexity: O(N*logN)

2. Merge Sort#

1. Principle#

For an unordered sequence, similar to Quick Sort, if you want to sort it, you can proceed as follows:

  • For a segment of numbers, you can first select a point mid as the split point. (Where mid is generally the middle of the sequence)
  • First take its [l,mid] and [mid+1,r] two subsegments for sorting, and assume they are already sorted.
  • Then for the two already sorted subsegments, place them into array b in sorted order, and then overwrite the original sequence with the sorted numbers.

2. Implementation (Code)#

This code idea is adapted from Mr. Y

void merge_sort(int a[],int b[],int l,int r){
if(l>=r) return;//设置退出条件
int mid = l+r>>1,i=l,j=mid+1,k=0;//设置分割点
merge_sort(a,b,l,mid);merge_sort(a,b,mid+1,r);//先对子段排序
while(i<=mid&&j<=r)
if(a[i]<=a[j]) b[k++] = a[i++];
else b[k++] = a[j++];
while(i<=mid) b[k++]=a[i++];
while(j<=r) b[k++]=a[j++];
for(i=l,j=0;i<=r;i++,j++) a[i]=b[j];
}
  • Time complexity: O(N*logN)
  • Inversions

When sorting an array with merge sort, you can observe that when a[i] > a[j] occurs, an inversion is formed, and for the current a[j], there will be mid-i+1 inversions (i.e., a[j] paired with the numbers in [i,mid] form mid-i+1 inversions). Therefore, during sorting, we can compute the number of inversions present in the sequence.

long long merge_sort(int a[],int b[],int l,int r){
if(l>=r) return 0;
int mid = l+r>>1,i=l,j=mid+1,k=0;
long long ans = merge_sort(a,b,l,mid)+merge_sort(a,b,mid+1,r);
while(i<=mid&&j<=r)
if(a[i]<=a[j]) b[k++] = a[i++];
else ans += mid-i+1,b[k++]=a[j++];
while(i<=mid) b[k++] = a[i++];
while(j<=r) b[k++] = a[j++];
for(i=l,j=0;i<=r;i++,j++) a[i]=b[j];
return ans;
}

ea85b4d19f624e219ca3da0cac7fa525.png

Binary Search#

1.1 Algorithm Principle#

For a monotonic sequence, by exploiting its monotonic property, to find the desired number k, you can proceed as follows:

First determine the search range l and r. When l < r, take the midpoint mid, split the interval [l,r] into [l,mid] and [mid+1,r] (here mid = l+r>>1) or [l,mid-1] and [mid,r] (here mid = l+r+1>>1). Then, under the condition l<r, based on the check(mid) function, update l and r until l >= r

1.2 Code Implementation#

This code idea is adapted from Mr. Y

//将区间[l,r]分为[l,mid]和[mid+1,r]
int bsearch_1(int l,int r){
while(l<r){
int mid = l+r>>1;
if(check(mid)) r=mid;
else l=mid+1;
}return l;
}
//将区间[l,r]分为[l,mid-1]和[mid,r]
int bsearch_2(int l,int r){
while(l<r){
int mid = l+r+1>>1;
if(check(mid)) l=mid;
else r=mid-1
}
}

2.1 Algorithm Principle#

For a monotonic sequence, using its monotonic property, similar to integer binary search, to find the desired number k:

First determine the search range l and r. When r-l > eps, take the midpoint mid, mid = (l+r)/2, then, under the condition r-l > eps, based on the check(mid) function, update l and r until r-l <= eps

2.2 Code Implementation#

This code idea is adapted from Mr. Y

double bsearch(double l,double r){
while(r-l>eps){
double mid = (l+r)/2;
if(check(mid)) r=mid;
else l=mid;
}return l;
}

626e3c9dbe834576b1f325b94c772550.png

Share

If this article helped you, please share it with others!

Sorting and Binary Search Study Notes
https://dreaife.tokyo/en/posts/sorting-binary-search/
Author
dreaife
Published at
2022-07-08
License
CC BY-NC-SA 4.0

Some information may be outdated

Related Posts Smart
1
High-Precision Arithmetic Study Notes
algorithm High-precision arithmetic includes addition, subtraction, multiplication, and division for large numbers that exceed the range of ordinary data types. Addition and subtraction are implemented by simulating manual calculation, while multiplication and division process digits step by step and pay attention to removing leading zeros. Related examples and reference code are provided to help understand and implement these operations.
2
Algorithm Study: Bitwise Operations, Discretization, and Interval Merging
algorithm This article introduces algorithms for bitwise operations, discretization, and interval merging. Bitwise operations are used to process binary numbers, discretization is used to optimize storage and queries for sparse data, and interval merging uses sorting and overlap checks to efficiently merge multiple intervals. Related examples and reference code are provided to help understand their applications.
3
Algorithm Study: Prefix Sum, Difference Array, and Two Pointers
algorithm This article introduces the principles and applications of prefix sums, difference arrays, and the two-pointer technique. Prefix sums are used to quickly compute subarray sums, difference arrays are used to efficiently handle range updates, and the two-pointer method optimizes query efficiency by maintaining pointers. Related examples and reference code are provided to help understand and apply these algorithms.
4
Interview Algorithm Study 1
algorithm This post contains multiple algorithm interview problems and solutions, including snake matrix filling, quicksort on a singly linked list, finding peaks and local minima, the egg hardness problem, a stack supporting minimum retrieval, and finding the entry node of a cycle in a linked list. Each problem includes a detailed description, input/output format, and sample code.
5
The First Round of Selection in the New Era
life With the development of AI technology, the cost of using advanced models may lead to social stratification, where only those with strong financial means can use these models. Although current prices are still acceptable, future price increases may make them unaffordable for most people, thus forming the first round of selection. The author feels anxious about this phenomenon, while also realizing that AI applications have moved beyond programming and into broader industries. Facing the challenges and opportunities of a new world, individuals continue to explore under the momentum of the times.

Table of Contents