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

C language Fast sorting algorithm and Code

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Network Security >

Share

Shulou(Shulou.com)06/01 Report--

Rapid sort is an improvement on bubble sort.

The fundamental idea of the rapid sorting algorithm is to divide the number of sorting into two parts, one of which is smaller than the other part of the data, and then divide the two parts of the data into different divisions, repeatedly perform the above division operations, until all the sorting data becomes ordered.

Can only be based on the fundamental idea of rapid sort of view is not deep, next to n disordered sequence A[0], A[1]…, A[n-1] to take rapid sort method to stop ascending array as an example to stop explanation.

(1)Define the two variables low and high, setting the low and high discriminations to the index of the beginning element and the first element of the sequence to stop sorting. The first time, low and high are distinguished as 0 and n-1, and each subsequent value is determined by dividing the missing sequence start element and the index of the first element.

(2)Define a variable key, and then divide the array A into your two parts based on the value of key. Usually, the value of key is the value of the first element in the sequence to be sorted. The first value is A[0], and the next value is determined by the beginning element of the sequence to be divided.

(3)Scan left from the beginning of the array element pointed to by high, scan while the array elements with index high are sequentially compared with the partition reference value key, until high is not greater than low or the first array element less than the reference value key is found, and then assign this value to the array element pointed to by low, while moving low to the right one position.

(4)If low is still less than high, then the array element pointed to by low begins to scan to the right, scanning while the array element value with index low is sequentially compared with the partitioned reference value key, until low is not less than high or the first array element greater than the reference value key is found, and then the value is assigned to the array element pointed to by high, while moving high to the left.

(5)Repeat steps (3) (4) until the plant of low is not less than high, then the two local discriminations lost after the successful division are A[low... pos-1] and A[pos+1... high], in which the value of the array element corresponding to the pos subscript is the reference value key for stopping the division, so when the division is completed, the array element with the subscript pos is assigned as key.

(6)Divide the lost two parts A[low... pos-1] and A[pos+1... high] and continue to take the above steps to stop dividing until the ordered sequence is lost.

In order to deepen the reader's understanding, the next paragraph of code to understand the rapid sort of detailed completion method.

#include #include #define N 6 int partition(int arr[], int low, int high){ int key; key = arr[low]; while(low

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

Network Security

Wechat

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

12
Report