Three basic sorting algorithms

As a programmer, I am learning algorithms recently, so I want to review several sorting algorithms. All sorts in this article are for 升序, and the description code isjava

1 select sort

1.1 Process analysis

For an indeterminate integer array, first compare the number where i=0 with all subsequent numbers, find the smallest number i=j, exchange the numbers in the array, and then compare from i=1 until i =num.length-1

1.2 Animation analysis

Insert picture description here

1.2 Algorithm description

public class selectsort {
    
    
	public static void main(String[] args) {
    
    

	int[] num= {
    
    2,3,5,4,1,9,8,7,56,3,66,56,52,51,20,32};//要排序的数组
	selectsort(num);
	for(int n:num)    //增强型for语句用于输出排序后的数组
	{
    
    
		System.out.println(n);
	}
	}
	public static void selectsort(int[] num)     //构造方法
	{
    
    
		for(int i=0;i<num.length;i++)
		{
    
    
			int minindex=i;
			for(int j=i+1;j<num.length;j++)
			{
    
    
				if(num[minindex]>num[j])
				{
    
    
					minindex=j;
				}
							}
			swap(num,i,minindex);
		}
	}
	public static void swap(int[] num,int i,int minindex)//交换下标为i和minindex中的数组元素
	{
    
    
		int temp;
		temp=num[minindex];
		num[minindex]=num[i];
		num[i]=temp;
	}
}

1.4 Complexity

Time complexity: O(n^2)

2 Insertion sort

2.1 Algorithm description

In an integer array, starting from the second number, compare it with the previous number. If it is smaller than the previous number, swap positions with it until it reaches the leftmost side of the array. If it is larger than the previous number, then Start comparing the size of the third number with the previous number

2.2 Animation analysis

Insert picture description here

2.3 Algorithm code

public class Insertsort {
    
    

	public static void main(String[] args) {
    
    
		// TODO Auto-generated method stub
		int[] num= {
    
    2,3,5,4,1,9,8,7,56,66,56,52,51,20,32};//要排序的数组
          Insertsort(num);
          for(int n:num)    //增强型for语句用于输出排序后的数组
      	{
    
    
      		System.out.print(n+",");
      	}
	}
	public static void Insertsort(int[] num)   //排序算法
	{
    
    
		for(int i=1;i<num.length;i++)
		{
    
    
			      
			for(int j=i;j>0;j--)
			{
    
    
				if(num[j]<num[j-1])
				{
    
    
					swap(num,j-1,j);
				}
				else{
    
    
                    break;
                }
			}
		}
	}
  public static void swap(int[] num,int index,int j)//交换两个数的下标
  {
    
    
	  int temp;
	  temp=num[index];
	  num[index]=num[j];
	  num[j]=temp;
  }
}

2.4 Complexity

Time complexity: O(n^2)

3 Bubble sort (also called bubble sort)

3.1 Algorithm description

For an array to be sorted, first start with the subscript I=0 +1, whenever two numbers i and i+1 are encountered, when num[i]>num[i++1], exchange the two numbers After the first traversal, the largest element must be on the far right. After the second traversal, the penultimate tree is also at the second position on the right. After num.length-1 traversal, The elements in the prime group are sorted in ascending order

3.2 Dynamic analysis

Insert picture description here

3.3 Algorithm description

public class Bobblesort {
    
    

	public static void main(String[] args) {
    
    
		// TODO Auto-generated method stub
		int[] num= {
    
    2,3,5,4,1,9,8,7,56,66,56,52,51,20,32};//要排序的数组
        Bobblesort(num);
        for(int n:num)    //增强型for语句用于输出排序后的数组
    	{
    
    
    		System.out.print(n+",");
    	}
	}

	private static void Bobblesort(int[] num) {
    
    
		for(int i=0;i<num.length;i++)
		{
    
    
			for(int j=0;j<num.length-i-1;j++)
			{
    
    
				if(num[j]>num[j+1])
				{
    
    
					swap(num,j,j+1);
				}
			}
		}
		
	}
	public static void swap(int[] num,int index,int j)//交换两个数的下标
	  {
    
    
		  int temp;
		  temp=num[index];
		  num[index]=num[j];
		  num[j]=temp;
	  }
}

3.4 Complexity

Time complexity: O(n^2) to
be continued...
PS: The picture comes from the Internet

Guess you like

Origin blog.csdn.net/qq_43475285/article/details/106095539