Java Sort algorithm Summary of insertion sort _java

Source: Internet
Author: User

The example in this article describes the Java Insert Sort method. Share to everyone for your reference. The specific analysis is as follows:

There is an ordered sequence of data that requires inserting a number into the sorted sequence of data, but the data sequence is still ordered after insertion, and the insertion sort method is used. This article is mainly about the Java implementation of the insert sort.

The basic operation of inserting a sort is to insert a data into the ordered data, so as to get a new, number plus one ordered data. The time complexity of comparison and Exchange is O (n^2), the algorithm is adaptive, the time complexity is O (n), the algorithm is stable and the cost is very low for the data has been basically ordered. The algorithm is suitable for the situation that the data is basically ordered or the data quantity is small.

The insertion algorithm divides the array to be sorted into two parts: the first part contains all the elements of the array, except for the last element, and the second part contains only this element. After the first part is sorted, the last element is inserted into the position in the first part of the order now.

Algorithm description

In general, the insertion sort is implemented on an array using In-place. The specific algorithm is described as follows:

1. Starting with the first element, the element can be considered to have been sorted
2. Take out the next element and scan backwards in the sorted sequence of elements
3. If the element (sorted) is greater than the new element, move the element to the next position
4. Repeat step 3 until you find the sorted element is less than or equal to the new element's position
5. Insert the new element into the next location
6. Repeat step 2

If the cost of comparison operations is greater than the exchange operation, the binary lookup method can be used to reduce the number of comparison operations. The algorithm can be considered a variant of the insertion sort, called a binary lookup sort.

Code implementation

public void Insertionsort () { 
  //insert sort 
  int out, in; 
  int count1 = 0, Count2 = 0;//Number of copies, comparison for 
  (out = 1; out < Nelems; out++) { 
   Long temp = a[out]; 
   in = out; 
   Boolean flag=in>0&&a[in-1]>=temp; 
   while (flag) { 
   if (a[in-1]>=temp) { 
    if (in>0) { 
    a[in]=a[in-1]; 
    count1++; 
    --in  
    } 
   } 
    count2++; 
    flag=in>0&&a[in-1]>=temp; 
   }  
   A[in] = temp; 
  } 
  System.out.println ("The number of copies is:" + count1 + "comparison is:" + Count2); 
}

The insertion sort method has better efficiency when the data is in a certain order. But if the data has no rules, it needs to move a lot of data, and its efficiency is as bad as the bubble sort method and the selection method.

I hope this article will help you with your Java programming.

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.