今是昨非

今是昨非

日出江花红胜火,春来江水绿如蓝

Algorithm_Sort

Algorithm_Sort#

Given an integer array nums sorted in non-decreasing order, return an array of the squares of each number sorted in non-decreasing order.

QuickSort#

Implementation logic:
Take the value at the specified position index, compare it with the numbers before the index, and if the number before is greater than the number after, swap the positions.

For example:

The code is as follows:

Selection Sort#

Implementation logic:
Traverse the array to find the smallest value and place it at the index 0 of the result array.
Traverse the array to find the second smallest value and place it at the index 1 of the result array.
...

The code is as follows:

Bubble Sort#

Logic:
Iterate through adjacent elements, if the previous element is larger, swap positions; then continue to compare the next elements;
Repeat the above steps
Repeat the above steps
...
Until there are no elements that can be swapped.

For example:

The code is as follows:

Merge Sort#

Implementation logic:
Split the array into two halves, then continue to split the two halves of the array until the number of elements in the array is 1, then compare and return the sorted last split smallest array, then sort and concatenate concatenate concatenate.

The diagram is as follows:

merge sort demo

The code is as follows:

Reference#

Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.