Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the beautiful sorting method in C language?

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)05/31 Report--

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.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report