Java entry-level sorting algorithm and java entry-level Algorithm

Source: Internet
Author: User

Java entry-level sorting algorithm and java entry-level Algorithm

To understand insertion sorting, you must first understand and implement an algorithm --> for an array a [] that has sorted orders from large to small, insert a value so that the order of the entire array is still from large to small;

The implementation of this algorithm is very simple and will not be discussed.

For the first round of the out-of-order array a []; In the outer I loop, I assume that the array has only one element a [0], and a [1] is used as the value for insertion, at the end of the first loop, an ordered array is obtained,

The second loop uses the array with two elements a [0], a [1], a [2] As the value to insert, and so on to sort the entire array.

 

Import java. lang. reflect. Array;
Import java. util. Arrays;

Public class TestArray {
Public static void main (String [] args ){
Int a [] = };
TestArray ();
System. out. println (Arrays. toString ());
}

Static void testArray (int [] ){
For (int I = 1; I <a. length; I ++ ){
Int value = a [I], j;

For (j = I-1; j> = 0 & value <a [j]; j --){
// Leave location a [j] blank and wait for value to be inserted
A [j + 1] = a [j];
}
// Unoptimized j-Loop
// For (j = I-1; j> = 0; j --){
// If (value <a [j]) {
// A [j + 1] = a [j];
//} Else {
// Break;
//}
// After the j loop, run j --
// Therefore, a [j + 1] = value;
A [j + 1] = value;
}
}
}

 

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.