In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article introduces the relevant knowledge of "how to realize the top ten sorting algorithms of Java". 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!
Stability of sorting algorithm:
It is assumed that there are multiple records with the same keywords in the sequence of records to be sorted. If the relative order of these records remains unchanged after sorting, that is, a [I] = a [j] in the original sequence, and a [I] before a [j], the sorting algorithm is said to be stable after sorting to ensure that a [I] is still before a [j]; otherwise, it is called unstable.
one。 Select sort
Each time, select the smallest element from the elements to be sorted, followed by 1, 2, 3. The elements of the location are exchanged. This forms an ordered region in the front of the array. For each exchange, the length of the ordered area is increased by one.
Public static void selectionSort (int [] arr) {/ / details 1: here can be arr.length or arr.length-1 for (int I = 0; I
< arr.length-1 ; i++) { int mini = i; for (int j = i+1; j < arr.length; j++) { //切换条件,决定升序还是降序 if(arr[mini]>Arr [j]) mini = j;} swap (arr,mini,i);}} di. Bubbling sort
Compare the two adjacent numbers in turn, and swap them if the order is wrong, so that each round of comparison can put the maximum number where it should be. (it's like sending the biggest bubble to the top.)
Here is an explanation of the meaning of the wrong order. We sort it in ascending order, and the latter value should be greater than or equal to the previous value, and if it is not satisfied, swap.
Public static void bubbleSort (int [] arr) {for (int I = 0; I)
< arr.length-1; i++) { //记录本次有没有进行交换的操作 boolean flag = false; //保存在头就动头,保存在尾就动尾 for(int j =0 ; j < arr.length-1-i ; j++){ //升序降序选择地 if(arr[j] >Arr [arr,j,j+1 1]) {swap (arr,j,j+1); flag = true;}} / / if the exchange operation is not carried out this time, the data has been ordered if (! flag) {break;} / / Program end}} 3. Insert sort
Insertion sorting can actually be understood as the process of touching cards when we play poker. The cards in our hands are always orderly when we touch cards. Every time we touch a card, we insert the card into its place. When all the cards are touched, all the cards will be in order.
Think about it: an ordered region is formed in front of the array, so when I find out where the current number should be inserted, can I use binary to optimize the complexity of the insertion sort to O (nlogn)?
Two points can find the location with the complexity of log. The key is that if you use an array to store the data, the data will be moved back to O (n) complexity when you insert it. If you use a linked list, you will find that the insertion position is O (1), but the linked list can not be divided into two parts.
Public static void insertSort (int [] arr) {/ / starting with the second number, insert each number in turn into the specified position for (int I = 1; I)
< arr.length ; i++) { int key = arr[i]; int j = i-1; //大的后移操作 while(j >= 0 & & arr [j] > key) {arr [jacks 1] = arr [j]; Jmuri;} arr [jacks 1] = key;}} 4. Hill ranking
Hill sorting is a sorting algorithm proposed by Donald Shell in 1959, which is an efficient version of the improved direct insertion sorting. Hill sorting needs to prepare a set of data as an incremental sequence.
This set of data needs to meet the following three conditions:
1. Data decreasing permutation
two。 The maximum value in the data is less than the length of the array to be sorted.
3. The minimum value in the data is 1.
As long as the array that meets the above requirements can be used as an incremental sequence, but different incremental sequences will affect the efficiency of sorting. Here we use {5pm 3pm 1} as an incremental sequence to explain.
The reason for achieving optimization: reduce the amount of data, so that the gap between O (n) and O (n ^ 2) is not large.
Public static void shellSort (int [] arr) {/ / Block processing int gap = arr.length/2; / / Increment while (1 = 0 & & arr [j] > key) {arr [j+gap] = arr [j]; JMZ arr [j+gap] = key } gap = gap/2;}} five Heap sort
It is a complete binary tree, which can be divided into two types: big root pile and small root pile.
You can take the maximum / minimum value of O (1), delete the maximum / minimum value of O (logn), and insert the element with O (logn).
MIN-HEAPIFY (I) Operation:
We assume that the left and right subtrees of a node I in a complete binary tree satisfy the property of a small root heap, and that the left child of node I is the right child of left_i,i node is rig ℎ Tubi. Then if a [I] is greater than a [left _ I] or a [rig ℎ I], then the whole subtree with the I node as the root node does not satisfy the nature of the small root heap, and now we are going to do an operation: adjust the subtree with the I node into a small root heap.
/ / the heap sort public static void heapSort (int [] arr) {/ / starts to adjust the position of the last leaf node int start = (arr.length-1) / 2; / / starts traversing from the last leaf node, and adjusts the binary tree for (int I = start; I > = 0; Ilam -) {maxHeap (arr, arr.length, I) } for (int I = arr.length-1; I > 0; I -) {int temp = arr [0]; arr [0] = arr [I]; arr [I] = temp; maxHeap (arr, I, 0) }} / / adjust the binary tree to public static void maxHeap (int [] arr, int size, int index) {/ / establish the left child node int leftNode = 2 * index + 1; / / establish the right child node int rightNode = 2 * index + 2; int maxNode = index; / / adjust if when the value of the left child node is greater than the root node (leftNode
< size && arr[leftNode] >Arr [maxNode]) {maxNode = leftNode;} / / adjust if (rightNode) when the value of the right child node is greater than the root node
< size && arr[rightNode] >Arr [maxNode]) {maxNode = rightNode;} if (maxNode! = index) {int temp = arr [maxNode]; arr [maxNode] = arr [index]; arr [index] = temp / / the original structure may be destroyed after the exchange, so you need to adjust / / Recursive call again to adjust maxHeap (arr, size, maxNode);}}
I use a big root heap, and the sorting process boils down to: first the left root and then the right root (see how you write it)-> each root is up and down. (left and right up and down)
six。 Merge and sort
Merging and sorting is a typical application of the divide-and-conquer method, which first introduces the divide-and-conquer method, which divides a complex problem into two or more identical or similar sub-problems, and then divides the sub-problems into smaller sub-problems. Finally, the scale of the sub-problem is very small and can be solved directly, and then the solutions of the sub-problems are combined to get the solution of the original problem.
The process of merging and sorting subproblems is to divide the array into two parts at a time until the length of the array is 1 (because the array with only one number is ordered). Then the adjacent ordered numbers are combined into an ordered array. Until it all comes together, the whole array is sorted. The problem to be solved now is how to combine two ordered numbers into an ordered array. In fact, it is to compare the current smallest two elements of two arrays each time, and choose which one is small.
Array a
Array b
Description
Answer array
2,5,7
1,3,4
one
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.