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

How to use the fast sorting method of java

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

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

This article introduces the relevant knowledge of "how to use java fast sorting method". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

It shows that the fast sorting method (quick sort) is recognized as one of the fastest sorting methods (depending on the object of solving the problem). Although the fast sorting method can reach O (N2) in the worst case, in most cases, the efficiency performance of the fast sorting method is quite good.

The basic spirit of the fast sorting method is to find out the appropriate axis in the series, and then divide the series into two, sorting the left and right sequences respectively, and it is the selection of the axis that affects the efficiency of the fast sorting method. The first version of the fast sorting method introduced here is the version mentioned in most textbooks, because it is the easiest to understand and most consistent with the concept of axis division and sorting left and right, so it is suitable for beginners to explain. The fast calculation introduced here is as follows: set the leftmost number as the axis and record its value as s

Circle processing:

Make index I look from the left to the right of the sequence until you find a number greater than s

Make index j look from the left and right of the sequence to the left until you find a number less than s

If I > = j, leave the loop

If I

< j,则交换索引i与j两处的值 将左侧的轴与j 进行交换 对轴左边进行递回 对轴右边进行递回 透过以下演算法,则轴左边的值都会小于s,轴右边的值都会大于s,如此再对轴左右两边进行 递回,就可以对完成排序的目的,例如下面的实例,*表示要交换的数,[]表示轴: [41] 24 76* 11 45 64 21 69 19 36* [41] 24 36 11 45* 64 21 69 19* 76 [41] 24 36 11 19 64* 21* 69 45 76 [41] 24 36 11 19 21 64 69 45 76 21 24 36 11 19 [41] 64 69 45 76 在上面的例子中,41左边的值都比它小,而右边的值都比它大,如此左右再进行递回至排序完 成。 #include #include #include #define MAX 10 #define SWAP(x,y) {int t; t = x; x = y; y = t;} void quicksort(int[], int, int); int main(void) { int number[MAX] = {0}; int i, num; srand(time(NULL)); printf("排序前:"); for(i = 0; i < MAX; i++) { number = rand() % 100; printf("%d ", number); } quicksort(number, 0, MAX-1); printf("\n排序后:"); for(i = 0; i < MAX; i++) printf("%d ", number); printf("\n"); return 0; } void quicksort(int number[], int left, int right) { int i, j, s; if(left < right) { s = number[left]; i = left; j = right + 1; while(1) { // 向右找 while(i + 1 < number.length && number[++i] < s) ; // 向左找 while(j -1 >

-1 & & number [--j] > s)

If (I > = j)

Break

SWAP (number, number [j])

}

Number [left] = number [j]

Number [j] = s

Quicksort (number, left, jmur1); / / A pair of left returns

Quicksort (number, jacks 1, right); / / A pair of right sides for recursion

}

}

This is the end of the content of "how to use java Quick sorting". Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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

Servers

Wechat

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

12
Report