Java Data Structure-simple sorting

Source: Internet
Author: User

The data structure refers to the arrangement of data in the computer storage space (or disk), and the algorithm refers to the process in which the software program is used to operate the data in these structures.

I. Bubble Sorting

Bubble Sorting is the simplest but the most efficient sorting of all data items. For example, the sorting times of 10 data items are 10 + 9 + 8 + 7... + 1.

N data items, the first sort has a N-1 comparison, the second N-2 comparison, and so on, the number of sort is :( N-1) + (N-2) + (N-3) +... + 1 = N * (N-1)/2

Public static void bubblearray (INT [] array) {// a maximum of N-1 sequential orders for (INT I = 0; I <array. length-1; I ++) {// to the current array [0 ...... length-i-1] The scope of sorting // J is gradually narrowing down, because after the internal loop has passed through, the largest or smallest has certainly been put to the end. For (Int J = 0; j <array. length-I-1; j ++) {If (array [J]> array [J + 1]) {int temp = array [J]; array [J] = array [J + 1]; array [J + 1] = temp ;}}for (INT x = 0; x <array. length; X ++) system. out. println (array [x]); system. out. println ("------------------------------------------");} public static void main (string [] ARGs) {int [] array = new int [] {5, 4, 3, 2, 1}; bubblearray (array );}

Output:

4
3
2
1
5
--------------------------------------------
3
2
1
4
5
--------------------------------------------
2
1
3
4
5
--------------------------------------------
1
2
3
4
5
--------------------------------------------

 

Ii. Select sorting

Select "sort" to start from the first element on the left as the base number and compare it with that on the right. Compare the elements with values greater than or less than the base number, when the largest element greater than the base number or the smallest element smaller than the base number is found, the two elements are exchanged and then started from the second element on the left.

Therefore, the Select sort and bubble sort performed the same number of comparisons: N * (N-1)/2

For 10 data items, 45 comparisons are required, but 10 data items only need less than 10 exchanges. When the data size is large, the sorting method is better than the Bubble sorting method.

Public static void selectionarray (INT [] array) {// index indicates the subscript int I, j, temp, index, min; for (I = 0; I <array. length-1; I ++) {min = array [I]; Index = I; for (j = I + 1; j <array. length; j ++) {If (min> array [J]) {min = array [J]; Index = J ;}} temp = array [I]; array [I] = array [Index]; array [Index] = temp; For (INT x = 0; x <array. length; X ++) system. out. println (array [x]); system. out. println ("------------------------------------------");} public static void main (string [] ARGs) {int [] array = new int [] {5, 1, 4, 3, 2, 6}; selectionarray (array );}

Min = array [J]; makes sure that the smallest element is updated for each comparison. After the J loop ends, the following table of the smallest element can be found, then, exchange elements in the I loop.

Output:

1
5
4
3
2
6
--------------------------------------------
1
2
4
3
5
6
--------------------------------------------
1
2
3
4
5
6
--------------------------------------------
1
2
3
4
5
6
--------------------------------------------
1
2
3
4
5
6
--------------------------------------------

 

Iii. Insert sorting

Insert sorting is the best sort among the three basic sorting algorithms. Generally, it is twice faster than Bubble sorting, and faster than insert sorting.

Insert sorting means that each step inserts a data to be sorted to an appropriate position in the sorted data according to its size until all data is inserted.

The insert sorting method is divided into two types: Direct insertion sorting and semi-insertion sorting. Here we only introduce direct insertion sorting,

In the first algorithm, a maximum of 1 times, the second trip is a maximum of 2 times, and so on, the last trip is the most, the N-1 times.

So there are 1 + 2 + 3 +... + (N-1) = N * (N-1)/2 times because each sort finds that just half of all data is compared on average before the insertion point,

So the number of comparisons is: N * (N-1)/4 times,

Demonstrate the process of directly inserting and sorting the four elements. Three inserts are required: (a), (B), and (c.

 

Public static void insertsort (INT [] array) {for (INT I = 1; I <array. length; I ++) {int T = array [I]; Int J = I; // The while part is used for element swapping on the partial elements in the sorted order. While (j> 0) & (array [J-1]> T )) {array [J] = array [J-1]; // shifts the sorted elements to the right -- J;} array [J] = T; // Insert the element to the appropriate position of the ordered Partial Element for (INT x = 0; x <array. length; X ++) system. out. println (array [x]); system. out. println ("========================= ");}} public static void main (string [] ARGs) {int [] array = {5, 1, 4, 2, 3}; insertsort (array );}

Output:

1
2
4
5
9
23
27
45

The insert sort section references: http://www.cnblogs.com/kkun/archive/2011/11/23/2260265.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.