Jsp Insertion Sorting Algorithm instance code

Source: Internet
Author: User

In the past, we used sorting methods in other languages. Other algorithms are the same regardless of the language. They are simply processed using a temporary variable or some built-in functions.

In the past, we used sorting methods in other languages. Other algorithms are the same regardless of the language. They are simply processed using a temporary variable or some built-in functions.

Public class CharuSort {
Public static void main (String [] args ){

Int [] sort = {4, 6, 3, 9, 5 };

Sort (sort );

For (int I = 0; I <sort. length; I ++)

System. out. print (sort [I] + "");

}

Public static void Sort (int [] sort ){

Int I; // indicates the number of scans.

Int j; // specifies the comparison element.

For (I = 1; I <sort. length; I ++) {// Number of scans: sort. length-1

Int temp; // temp is used for temporary data storage.

Temp = sort [I];

J = I-1;

While (j> = 0 & temp <sort [j]) {// if the second element is smaller than the first element

Sort [j + 1] = sort [j]; // pushes all elements to a position.

J --;

}

Sort [j + 1] = temp; // place the smallest element in the first position.

}

}

}

Related Article

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.