In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces Java how to achieve simple sorting, the article is very detailed, has a certain reference value, interested friends must read it!
Sorting is a very common and core operation in data processing, although there is a small chance that we need to implement it manually in the actual project development. after all, there are many kinds of implementation of sorting algorithms in the class library of each language. But understanding these exquisite ideas is of great benefit to us. This article briefly reviews the three most basic algorithms: selection, bubbling, and insertion.
First define a function to exchange array elements for calling when sorting
/ * * Exchange array elements * @ param arr * @ param a * @ param b * / public static void swap (int [] arr,int a reint b) {arr [a] = arr [a] + arr [b]; arr [b] = ARR [a]-arr [b]; arr [a] = ARR [a]-arr [b];} simple selection sort
Simple selection sorting is the most simple and intuitive algorithm, the basic idea is to select the smallest (or largest) element from the data elements to be sorted as the first element for each trip, until all the elements have been sorted, simple selection sorting is unstable sorting.
In the implementation of the algorithm, each time to determine the minimum elements, the head position will be kept to the current minimum by constantly comparing and swapping, which is a more time-consuming operation. In fact, it is easy to find that these exchanges are meaningless until the current minimum elements are fully determined. We can set a variable min, each comparison only stores the array subscript of the smaller elements, when the loop ends, this variable stores the current smallest element subscript, and then perform the swap operation. The code implementation is very simple, let's take a look.
Code implementation / * * simple selection sorting * * @ param arr * / public static void selectSort (int [] arr) {for (int I = 0; I)
< arr.length - 1; i++) { int min = i;//每一趟循环比较时,min用于存放较小元素的数组下标,这样当前批次比较完毕最终存放的就是此趟内最小的元素的下标,避免每次遇到较小元素都要进行交换。 for (int j = i + 1; j < arr.length; j++) { if (arr[j] < arr[min]) { min = j; } } //进行交换,如果min发生变化,则进行交换 if (min != i) { swap(arr,min,i); } } } 简单选择排序通过上面优化之后,无论数组原始排列如何,比较次数是不变的;对于交换操作,在最好情况下也就是数组完全有序的时候,无需任何交换移动,在最差情况下,也就是数组倒序的时候,交换次数为n-1次。综合下来,时间复杂度为O(n2) 冒泡排序 冒泡排序的基本思想是,对相邻的元素进行两两比较,顺序相反则进行交换,这样,每一趟会将最小或最大的元素"浮"到顶端,最终达到完全有序 代码实现 在冒泡排序的过程中,如果某一趟执行完毕,没有做任何一次交换操作,比如数组[5,4,1,2,3],执行了两次冒泡,也就是两次外循环之后,分别将5和4调整到最终位置[1,2,3,4,5]。此时,再执行第三次循环后,一次交换都没有做,这就说明剩下的序列已经是有序的,排序操作也就可以完成了,来看下代码 /** * 冒泡排序 * * @param arr */ public static void bubbleSort(int[] arr) { for (int i = 0; i < arr.length - 1; i++) { boolean flag = true;//设定一个标记,若为true,则表示此次循环没有进行交换,也就是待排序列已经有序,排序已然完成。 for (int j = 0; j < arr.length - 1 - i; j++) { if (arr[j] >Arr [juni1]) {swap (arr,j,j+1); flag = false;}} if (flag) {break;}
According to the bubbling implementation above, if the original array itself is ordered (which is the best case), it only needs to be compared once; if it is in reverse order, the number of comparisons is n-1+n-2+...+1=n (nMel 1) / 2, and the number of exchanges and comparisons are equivalent. Therefore, the time complexity is still O (N2). On the whole, the performance of bubble sorting is still slightly worse than that of the above kind of selective sorting.
Directly insert sort
The basic idea of direct insertion sorting is to insert a record to be sorted into the ordered sequence that has been sorted before until all the elements have been inserted.
Code implementation / * * insert sort * * @ param arr * / public static void insertionSort (int [] arr) {for (int I = 1; I)
< arr.length; i++) { int j = i; while (j >0 & & arr [j] < arr [jmur1]) {swap (arr,j,j-1); jmurmuri;}
In the best case, the simple insertion sort needs to be compared once without exchanging elements, and the time complexity is O (n). In the worst case, the time complexity is still O (N2). However, in the case of random arrangement of array elements, insertion sorting is better than the above two sorts.
The above is all the content of the article "how to achieve simple sorting in Java". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow 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.
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.