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

Nine kinds of sorting of simple algorithms (1)

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

Share

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

No matter what, I just like to put together a 9. This time, there are still 9 sorting algorithms, so let's make a summary. Sorting algorithm, although there are many examples of methods, but this is their own summary, very interesting algorithm hope you like it. Go directly to the code building, the following algorithms have been personally tested by the author, and modified to make it effective (paste available).

Package com.css.java.learning.massbag;import java.util.Arrays;/** algorithm: * sort related summary * @ author Red_ant * 20181119 * / public class OrderMethod {/ * 1, Bubble sorting * by comparing two adjacent elements, select the appropriate elements in turn and put them in the I bit of the * array. * / public static int [] bubbleSort (int [] nums) {int num = 0; for (int I = 0; I)

< nums.length -1; i++) { for (int j = 0; j < nums.length - 1 - i; j++) {//两两比较,符合条件的元素居于前面 if(nums[j] >

Nums [j + 1]) {num = nums [j]; nums [j] = nums [j + 1]; nums [j + 1] = num;} return nums } / * 2, Select sort * each trip from the sequence to be sorted, select the smallest element and put it at the end of the sorted sequence, and the rest is the sequence to be sorted. * repeat the above steps until you are finished. * / public static int [] selectSort (int [] nums) {int num = 0; for (int I = 0; I)

< nums.length -1; i++) { int index = i; for (int j = i + 1; j < nums.length; j++) {//选择合适的元素,直接放到数组的第i位 if(nums[index] >

Nums [j]) {index = j;} if (index! = I) {num = nums [I]; nums [I] = nums [index]; nums [index] = num;} return nums } / * 3. Select sort: heap sort is a tree structure selection sort * heap sort requires two processes to establish the heap, and the heap top exchanges the position with the last element of the heap. So heap sorting consists of two functions: * heap building * * function, repeatedly calling * function to realize sorting function * basic idea: * build the sequence into a heap, select large top heap or small top heap according to ascending and descending order * swap the heap top element with the end element, sink the largest element to the end of the array * re-adjust the structure to meet the heap definition Then continue to swap the top element of the heap with the current end element, repeatedly performing the adjustment + swap step until the entire sequence is ordered. * / public static int [] HeapSorts (int [] nums) {/ / build a large-top for (int I = nums.length/2-1; I > = 0; iMuk -) {/ / adjust the structure suitHeap (nums, I, nums.length) from the first non-leaf node from bottom to top, from right to left } / / adjust the heap structure, swap the top element and the end element for (int j = nums.length-1; j > 0; j color -) {exchangeNum (nums, 0, j); / / swap the top element and the end element suitHeap (nums, 0, j);} return nums } private static void exchangeNum (int [] nums, int I, int i2) {int index = nums [I]; nums [I] = nums [i2]; nums [i2] = index;} private static void suitHeap (int [] nums, int I, int length) {int index = nums [I]; / / take out the current element for (int j = iCo2 + 1; j

< length; j = j*2+1) { //从i节点的左节点开始,即2i+1处 if(j+1 < length && nums[j] < nums[j+1]){ //如果,左子节点小于右子节点,j指向右子节点 j++; } if(nums[j] >

Index) {/ / if the child node is larger than the parent node, assign the child node to the parent node nums [I] = nums [j]; I = j;} else {break;}} nums [I] = index / / put the index value to the final position} / * 4, java Arrays sorting * this method is a static method of the Arrays class, and it is very easy to use * / public static int [] ArraySort (int [] nums) {Arrays.sort (nums); return nums when you need to sort the array. } / * 5. Insert sort: insert sort directly * divide the array into two parts, compare the latter part of the element one by one with the first part of the element * and then move the element * / public static int [] insertSort (int [] sort) {/ / from the first part of the header as already sorted Insert the latter one into the sorted list for (int I = 1) I

< nums.length; i++) { int j; int index = nums[i];//index为待插入的元素 for (j = i; j >

0 & & index

< nums[j - 1]; j--) { nums[j] = nums[j -1]; } nums[j] = index; } return nums; } /*6、插入排序:希尔排序 * 建堆,对顶与堆的最后一个元素进行排序 * 希尔排序是插入排序的一种,先取一个小于n的整数作为第一个增量,把文件的全部记录分组。 * 所有距离为d1的倍数的记录放在同一个组中,先在各组内进行直接插入排序。实际上是一种分组插入排序的方法。 */ public static int[] shellSrot(int[] nums){ int index = nums.length/2; while (index >

= 1) {for (int I = index; I

< nums.length; i++) { if(nums[i] < nums[i - index]){ int j; int x = nums[i];//当前待插入的元素 nums[i] = nums[i - index]; for (j = i - index; j >

= 0 & & x

< nums[j]; j = j-index) { nums[j + index] = nums[j]; } nums[j + index] = x;//插入元素 } } index = index/2; } return nums; } /*7、交换排序:快速排序 * 基本思想: * A选择一个基准元素,通常选择第一个元素或最后一个元素 * B通过一趟排序将待排序的记录分割成独立的两部分:记录均值比基准元素值小,元素值比基准元素值大 * C此时基准元素在排序好的正确位置 * D分别对这两部分记录,用同样的方法继续进行排序,直到整个序列有序。 */ public static int[] quickSort(int[] nums, int...is){ int low,hight; if(is.length == 0){ low = 0; hight = nums.length - 1; }else{ low = is[0]; hight = is[1]; } if(low < hight){//判断,让递归函数退出,否则无限循环 int middle = getMiddle(nums, low, hight); quickSort(nums, 0, middle - 1);//基准元素小 quickSort(nums, middle + 1, hight);//比基准元素大 } return nums; } //获取,均值 private static int getMiddle(int[] nums, int low, int hight) { int index = nums[low];//选择基准元素 while (low < hight) { //从hight开始,找出比基准小的,与low换位置 while (low < hight && nums[hight] >

= index) {hight--;} nums [low] = nums [hight]; / / starting with low, find it larger than the benchmark and put it in the position previously vacated by hight while (low < hight & & nums [low] = j) {return;} / / find the intermediate index int middle = (I + j) / 2 / a pair of left array recursive sortmerge (nums, I, middle); / / a pair of right array recursive sortmerge (nums, middle + 1, j); / / merge array merge (nums, I, middle, j) } private static void merge (int [] nums, int I, int middle, int j) {/ / create a temporary intermediate quantity array int [] tmpArr = new int [nums.length]; / / the first element index of the right array int mid = middle + 1; / / record the temporary array index int second = I / / cache the index int tmp = I; while (I) of the first element of the left array

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