Select and sort Java program sorting

Source: Internet
Author: User

1. Basic Idea of selecting sorting

Each trip is in n-I + 1 (I = 1, 2 ,... N-1) records with the smallest keyword are selected as the I-th record in the sequence. We mainly introduce simple selection sorting, Tree Selection sorting, and heap sorting.

Simple selection and sortingThe basic idea: simple sorting of the I-train refers to selecting the record with the smallest keyword from the n-I + 1 record through the comparison of the n-I keyword, and exchange with the I-th record. A I-1 comparison is required until all records are sorted. For example, when selecting the I-th record, select the k-th record with the smallest keyword from the current candidate record and exchange it with the I-th record. Figure 9.5 provides a simple sorting example to illustrate the results after the first three trips. In the figure, the candidate record is enclosed in braces, and the sorting records are placed out of braces.

{48 62 35 77 55 14 35 98}↑ I k

14{62 35 77 55 48 35 98}Zookeeper

I k 14 35{62 77 55 48 35 98}↑ I K 14 35 35{
77 55 48 62 98}Sort I k select sort example simple select sort algorithm specific descriptions are as follows:

Void selectsort (recordtype R [], int length )/*

Sort record array R by simple selection. The length is the length of the array.

*/{N = length; for (I = 1; I <= n-1; ++ I)

{K = I; for (j = I + 1; j <= N; ++ J) if (R [J]. key <R [K]. key) k = J; If (K! = I) {x = R [I]; R [I] = R [k]; R [k] = x ;}}/* selectsort */

2. Simple selection and Sorting Algorithm Analysis

In the simple selection and sorting process, the number of records to be moved is relatively small. In the best case, the initial status of the records to be sorted is already in the forward order, so you do not need to move the records. In the worst case, that is, if the initial status of the records to be sorted is in reverse order, the maximum number of records to be moved is 3 (n-1 ). The number of comparisons to be compared in the simple sorting process is irrelevant to the sequence of records to be sorted in the initial state. When I = 1, n-1 comparisons are required; when I = 2, N-2 comparisons are required; and so on, the total number of comparisons required is Σ = (n-1) + (n-2) +... + 2 + 1 = N (n-1)/2, that is, the time complexity of the comparison operation is O (n2 ).

The selection of sorting method is an improvement of the comparison and clearing method (that is, the Bubble sorting method. Before selecting a sorting method, let's take a look at the comparison and comparison methods. For ease of understanding, there are 10 numbers of array elements a [0] ~ A [9. The location comparison exchange method is used to locate a [0] ~ in ascending order. The proper value in a [9] (similar to the competition in the Wulin Conference) is naturally the smallest number in a [9. For example, if a [0] is located, first assume that the current value in a [0] is the maximum number, and a [0] is compared with the following elements one by one. If a [4] is larger, exchange a [0] And a [4], and a [0] is updated with a [5] ~ If a [9] is larger than a [8], a [0] And a [8] are exchanged, and a [0] is a new number, compare with a [9. After a round of comparison, a [0] is the largest number. The martial arts of this competition was born. Next, starting from a [1], because the champion is about to rest, another round of a [1] is the next big number, that is, the eye of the list. Then, starting from a [2], the competition for flowers became a contest, after a [8], the sorting is complete.

3. Select the sorting code for Java

Public static void main (string [] ARGs ){

Random random = new random ();

Int [] pdata = new int [10];

For (INT I = 0; I <pdata. length; I ++) {// randomly generates 10 orders.

Integer A = random. nextint (100 );

Pdata [I] =;

System. Out. Print (pdata [I] + "");

}

System. Out. println ();

Pdata = choose (pdata );

For (INT I = 0; I <pdata. length; I ++ ){

System. Out. Print (pdata [I] + "");

}

System. Out. println ();

}

Public static int [] choose (INT [] pdata ){

System. Out. println ();

For (INT I = 0; I <pdata. length; I ++ ){

For (Int J = I; j <pdata. length; j ++ ){

If (pdata [J] <pdata [I]) {

Int A = pdata [J];

Pdata [J] = pdata [I];

Pdata [I] =;

}

}

}

Return pdata;

}

Related Keywords:

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.