Java Algorithm insert sorting optimization code, java Algorithm sorting code

Source: Internet
Author: User

Java Algorithm insert sorting optimization code, java Algorithm sorting code
Original article: java Algorithm insert sorting optimization code: http://www.zuidaima.com/#/1550463280630784.htma step to improve the efficiency of inserting sorting

To run this method, you must pass parameters for the main method.

package com.zuidaima.sort;/***@author www.zuidaima.com**/public class TestSort {public static void main(String args[]){int l = args.length;int[] a = new int[l];for(int i = 0;i < l;i++){a[i] = Integer.parseInt(args[i]);}for(int i = 0;i < l;i++){int k = i;int j;for(j = k + 1;j < l;j++){if(a[j] < a[k]){k = j;}}System.out.print("j = " + j);if(k != i){int temp = a[i];a[i] = a[k];a[k] = temp;}}System.out.println();for(int i = 0;i < l;i++){System.out.print(" a[" + i +"]:" + a[i]);}}}



Analysis of java insertion sorting code

Public class InjectionSort // defines an InjectionSort class
Public static void injectionSort (int [] number) // upload an array
For (int j = 1; j <number. length; j ++) // Loop
Int tmp = number [j]; // loop the second value of the array into tmp
Int I = J-1 // assign an I value
While (tmp <number [I]) // who is smaller than the first value of the array?
Number [I + 1] = number [I]; // assign the first value to the second if the value is smaller
I --;
If (I =-1) // if I value =-1
Break; // exit the loop
Number [I + 1] = tmp // because the previous one in the comparison array is replaced by the last one, the small one is placed in front of
This is the first time, because the loop is an array, and the next loop goes down. Finally, the order in the array is sorted from small to large.
Public static void main (String [] args ){
Int [] num = {,}; // defines the array num
InjectionSort (num); // CALL THE METHOD
For (int I = 0; I <num. length; I ++ ){
System. out. println (num [I]); // The sorted array is displayed, and a value is displayed in one row.

Simply put, the second and first in the array are smaller than others. Put the smaller one in the first one, put the bigger one in the second, and then compare the second one with the third one, the small one is placed in front of the array, and it is always compared to the end of this array, so that it is realized from small to large. I hope I can elaborate on it.

Java insertion Sorting Algorithm

Private static int [] insertSort (int [] array ){
For (int I = 1; I <array. length; I ++ ){
Int temp = array [I];
For (int j = I-1; j> = 0 & temp <array [j]; j --){
Array [j + 1] = array [j];
Array [j] = temp;
}
}

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.