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 Seven sorts of Java data structure

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

Most people do not understand the knowledge points of this article "how to use the seven sorts of Java data structures", so the editor summarizes the following contents, detailed contents, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "how to use the seven sorts of Java data structures" article.

Insert sort 1. Insert sort directly

When inserting the I (I > = 1) element, the preceding array [0], array [1],... , Array [I-1] has been sorted, then use array [I] and Array [I-1], Array [I-2],... Make a comparison and find that the insertion position is array [I] insertion, and the order of the elements in the original position is moved backward.

The closer the data is to order, the less time it takes to insert the sort directly.

Time complexity: O (N ^ 2)

The space complexity O (1) is a stable algorithm.

Insert the sort directly:

Public static void insertSort (int [] array) {for (int I = 1; I)

< array.length; i++) { int tmp=array[i]; int j=i-1; for(;j>

) {if (Array [j] > tmp) {array [junk 1] = Array [j];} else {break;}} array [junk 1] = tmp;}} 2, Hill sort

The basic idea of Hill sorting method is: first select an integer gap, divide all the records in the file to be sorted into gap groups, all the numbers with distance of gap in the same group, and insert and sort the numbers in each group directly. Then take the gap=gap/2 and repeat the above grouping and sorting work. When gap=1, all numbers are directly inserted and sorted in a group.

Hill sorting is an optimization of direct insertion sorting.

When gap > 1, it is pre-sorted in order to make the array more ordered. When gap = = 1, the array is close to ordering, and direct insertion sorting will be very fast.

The time complexity of Hill sorting is not easy to calculate, because there are many ways to value gap, which makes it difficult to calculate.

Hill sort:

Public static void shellSort (int [] array) {int size=array.length; / / the initial value of gap defined here is half of the length of the array int gap=size/2; while (gap > 0) {/ / the interval is the direct insertion sort for of gap (int I = gap; I)

< size; i++) { int tmp=array[i]; int j=i-gap; for(;j>

) {if (Array [j] > tmp) {array [j+gap] = Array [j];} else {break;}} array [j+gap] = tmp;} gap/=2 Second, select sort 1, select sort

Select the smallest data element in the element collection Array [I]-Array [n-1]

If it is not the first element in the group, swap it with the first element in the group

In the remaining collection, repeat the above steps until there is 1 element left in the collection

Time complexity: O (N ^ 2)

The space complexity is O (1), unstable.

Select sort:

/ / Exchange private static void swap (int [] array,int iMaginint j) {int tmp=array [I]; array [I] = Array [j]; array [j] = tmp;} / / Select sort public static void chooseSort (int [] array) {for (int I = 0; I)

< array.length; i++) { int minIndex=i;//记录最小值的下标 for (int j = i+1; j < array.length; j++) { if (array[j]=0; parent--) { shiftDown(array,parent,array.length); } } //堆排序 public static void heapSort(int[] array){ //创建大根堆 createHeap(array); //排序 for (int i = array.length-1; i >

0; iMel -) {swap (array,0,i); shiftDown (array,0,i);}} III. Exchange sort 1, bubble sort

Two-layer loop, the first layer loop represents the number of trips to be sorted, and the second layer loop represents the number of trips to be compared; the bubbling sorting here is optimized. In each comparison, we can define a counter to record the number of data exchanges. If there is no exchange, it means that the data is in order, and there is no need for sorting.

Time complexity: O (N ^ 2)

The space complexity is O (1), which is a stable sort.

Bubble sort:

Public static void bubbleSort (int [] array) {for (int iarray [jacks 1]) {swap (array,j,j+1); count++;}} if (count==0) {break;}} 2, Quick sort

Take an element in the sequence of elements to be sorted as the reference value, and divide the sorted set into two subsequences according to the sorting code, all elements in the left subsequence are less than the reference value, and all elements in the right subsequence are greater than the reference value. then the process is repeated in the most left and right subsequence until all the elements are arranged in the corresponding position.

Time complexity: best O (n*logn): the sequence to be sorted can be divided evenly each time.

Worst-case O (N ^ 2): the sequence to be sorted is ordered.

Space complexity: best O (logn), worst O (N). Unstable sorting

(1) digging method

When the data is ordered, quick sorting is equivalent to a binary tree without left or right subtrees, and the space complexity will reach O (N). If a large amount of data is sorted, it may lead to stack overflow.

Public static void quickSort (int [] array,int left,int right) {if (left > = right) {return;} int lager; int ritual; int tmp=array [l]; while (l=tmp&&l)

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

Development

Wechat

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

12
Report