What is the beautiful sorting method in C language?
Today Xiaobian to share with you what the c language beautiful sorting method is related knowledge points, detailed content, clear logic, I believe most people still know too much about this knowledge, so share this article for everyone to refer to, I hope you have some gains after reading this article, let's learn about it together.
Pretty sort algorithm its code to achieve look, on, to very neat very nice!
//@ Programmer Xiao Wu on page 95 of the 2nd edition of Introduction to Algorithms, where i and j are used, in order to
//better understand, I used low and high instead here
private static void stoogeSort(int[] A, int low, int high){
if(A[low] > A[high]) swap(A, low, high);
if(low + 1 >= high ) return;
int split = (high - low + 1) / 3;
stoogeSort(A, low, high - split);
stoogeSort(A, low + split, high);
stoogeSort(A, low, high - split);
}
You may be able to see it more intuitively through the pictures.
The whole idea of the code is based on recursion, the specific operation is: for the incoming array first sort the head and tail, then recursively call the first two-thirds of the sort, then recursively call the last two-thirds of the sort, and finally recursively call the first two-thirds of the sort.
Animation Description 1. Step 1: Compare the head and tail elements of the incoming array 2. Step 2: Determine whether the array can be trisected. If it can be trisected 3. Step 3: The first 2 / 3 area of the same logically recursive sorted array 4. Step 4: The last 2 / 3 area of the same logically recursive sorted array
5. Step 5: The same logic recursively sorts the first 2 / 3 of the array
The above is "c language beautiful sorting method is what" all the contents of this article, thank you for reading! I believe everyone has a great harvest after reading this article. Xiaobian will update different knowledge for everyone every day. If you want to learn more knowledge, please pay attention to the industry information channel.