In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains the "Java half insert sort how to achieve", the article explains the content is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in depth, together to study and learn "Java half insert sort how to achieve" it!
The idea of insertion sorting introduces that the principle of half insertion sorting is the same as that of direct insertion sorting algorithm. However, when inserting data into sorted data, a half search (binary search) is used. First take the middle element of the sorted sequence and compare it with the data to be inserted. If the value of the intermediate element is greater than the data to be inserted, then the data to be inserted belongs to the first half of the array, otherwise it belongs to the second half. And so on, constantly narrowing the scope to determine the location to insert. Algorithm description: data to be sorted: 2, 1, 6, 7, 4, take the first element as the ordered table, and the remaining elements as the unordered table, in which the ordered table: 2 Out-of-order table: 1 the first number 1 is taken from the disordered table and compared with the intermediate value 2. 11 is to be placed after 1, and then compared with the middle value 2 in the second half (ordered table: 2). 6 > 2 is inserted into the back of 2, and the ordered table is obtained. Out-of-order table: the first number 7 is taken from the disordered table for the third time, and compared with the intermediate value 2, 7 > 2 is placed behind 2, and then compared with the middle value 6 in the second half (ordered table: 6). 7 > 6 is placed after 6, and the ordered table is obtained: 1, 2, 6, 6, 7, 7 Disordered table: 4 for the fourth comparison, the first number 4 is taken from the disordered table, compared with the intermediate value 2, 4 > 2 and placed after 2, and then compared with the intermediate value 6 of the latter half (ordered table: 6 high+1;). Arr [j] = ar [j-1]; 20.} 21. Arr [high+1] = temp;22. } 23. The sort of summary half insertion is relatively stable, which reduces the number of comparisons compared to the sort of direct insertion, but the number of moves is the same as that of the sort of direct insertion. Introduction to the idea of insertion sorting
The principle of half-insert sort is the same as that of direct insertion sort algorithm. However, when inserting data into sorted data, a half search (binary search) is used. First take the middle element of the sorted sequence and compare it with the data to be inserted. If the value of the intermediate element is greater than the data to be inserted, then the data to be inserted belongs to the first half of the array, otherwise it belongs to the second half. And so on, constantly narrowing the scope to determine the location to insert.
Algorithm description:
Data to be sorted: 2, 1, 6, 7, 4
Take the first element as the ordered table and the remaining elements as the unordered table
Ordered table: 2; disordered table: 1, 6, 7, 4
For the first comparison, take the first number 1 from the unordered table, compare it with the intermediate value 2, 11, put it after 1, and then compare it with the intermediate value 2 of the second half (ordered table: 2). 6 > 2, insert 6 after 2, get
Ordered table: 1, 2, 6; disordered table: 7, 4
In the third comparison, the first number 7 is taken from the unordered table, and compared with the intermediate value 2, 7 > 2 is placed after 2, and then compared with the intermediate value 6 of the second half (ordered table: 6).
Ordered table: 1, 2, 6, 7; disordered table: 4
For the fourth comparison, the first number 4 is taken from the unordered table, compared with the intermediate value 2, 4 > 2 and placed after 2, and then compared with the intermediate value 6 of the second half (ordered table: 6 high+1; 7) {
19. Arr [j] = arr [j-1]
20.}
21. Arr [high+1] = temp
twenty-two。 }
23. }
Thank you for your reading, the above is the "Java half insert sort how to achieve" content, after the study of this article, I believe you on the Java half insert sort how to achieve this problem has a deeper understanding, the specific use of the need for you to practice and verify. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.