Insertion sort + Hill sort (insert straight hope)

Insertion Sort Algorithm Detailed Explanation and Implementation

Insertion Sort Summary of Sorting Algorithms

 

Sorting (2) Ten minutes will let you master insertion sorting

 

direct insertion sort 

Binary Insertion Sort 

Hill sort

 

Insertion sort principle:

Divide a set of data into two groups, the ordered group and the to-be-inserted group.

Each time an element is removed from the group to be inserted , compared with the elements of the ordered group , and a suitable position is found, and the element is inserted into the ordered group. In this way, each time an element is inserted, the ordered group increases and the group to be inserted decreases. Until the number of elements in the group to be inserted is 0. Of course, the insertion process involves movement of elements.

 

The insertion sort algorithm has a recursive idea in it, which consists of N-1 sorts. Initially, only the element at index 0 of the array is considered, and there is only one element, which is obviously ordered.

Then the first pass sorts the elements at subscript 1 to ensure that the elements on the array [0,1] are ordered;

The second pass sorts the elements at subscript 2 to ensure that the elements on the array [0,2] are ordered;

.....

.....

The N-1th pass sorts the elements at subscript N-1 to ensure that the elements on the array [0, N-1] are ordered, that is, the entire array is ordered.

Its recursive idea is reflected in: when sorting the elements at position i, the elements at [0,i-1] must be already in order.



 

  void InsertionSort(int *num,int n)
  {
  	int i = 0;
  	int j = 0;
  	int tmp = 0;
  	for(i = 1;i<n;i++)
  	{
           tmp = num[i];//Remove the first element from the group to be inserted.
	   j = i-1; //i-1 is the subscript of the last element of the ordered group (adjacent to the element to be inserted)
	    while(j>=0&&tmp<num[j]) //Note that there are two judgment conditions, and j>=0 limits them. The second is the insertion judgment condition
	   {
	        num[j+1] = num[j];//If it is not a suitable position, the ordered group elements move backward
		 j--; 
	   }
	    num[j+1] = tmp;//找到合适位置,将元素插入。 
	}
  }
  int main() 
  {
  	int i = 0;
  	int num[8]={9,3,4,2,6,7,5,1};
  	InsertionSort(num,8); 
  	/*这个函数必须知道元素的个数,所以将元素个数传入。
	有心者可以在函数内部用sizeof求出元素个数 */
  	for(i=0;i<8;i++)
  	{
     	printf("%d ",num[i]);
	}
  	return 0;
  }

 

package com.test;

public class InsertSort {

	public static void printArray(int[] a){  
        for(int item : a){  
            System.out.print(item + " ");  
        }  
    }  
	
	
	public static void main(String[] args) {
		int[] arr={5,3,2,45,65,33,12};     
		sort(arr);
		printArray(arr);
	}
	
	
	public static void sort(int[] arr){
		
		//以第一个为已排序队列,后面所有数字为待排序队列
		for(int i=1;i<arr.length;i++){
			//从待插入组取出第一个元素
			int tmp = arr[i];
			
			int j = 0;
			for(j = i-1;j>=0;j--){
				
				//若待插入数字小于已排序队列最后一个数字,则将已排序队列最后一个数字往后移一位
				if(tmp < arr[j]){					
					arr[j+1] = arr[j];
				}else{
					//若待插入数字大于已排序队列最后一个数字,则直接跳出
					break;
				}
				
			}
			
			arr[j+1] = tmp;
		}
	}
}

  

二、希尔排序

图解排序算法(二)之希尔排序

。。。

 

 

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326195630&siteId=291194637