Sorting algorithm (bubble sort, quick sort)

Sorting algorithm exchange sort (bubble sort and quick sort)

Swap sort: swap the positions of two records in the sequence according to the comparison result of the keywords of the two elements of the sequence.

Bubble Sort

The principle is as follows

  1. Compare adjacent elements. If the first one is larger than the second, swap them two.

  2. Do the same for each pair of adjacent elements, from the first pair at the beginning to the last pair at the end. At this point, the last element should be the largest number.

  3. Repeat the above steps for all elements except the last one.

  4. Continue to repeat the above steps for fewer and fewer elements each time until there is no pair of numbers to compare.

    Time complexity is best o(n) worst o(n2)

    Space complexity o(1)

    Code implementation (c)

    #include <stdio.h>
    #include <stdbool.h>
    
    void BubbleSort (int a[],int n){
          
            //冒泡排序 
    	int i,j,temp;         
    	for( i=0;i<n-1;i++){
          
          
    		bool flag=false;      //表示本趟冒泡是否发生交换的标志 
    		for( j=n-1;j>1;j--)   //一趟冒泡的过程 
    		if(a[j-1]>a[j]){
          
                //后面的数小于前面的 进行交换 
    			temp=a[j];
    			a[j]=a[j-1];
    			a[j-1]=temp;
    			flag=true;    
    		}
    		if(flag==false)    //本趟遍历没有发生交换说明结束 
    		return;
    	}
    }
    void Printarr(int a[],int n)    //输出数组 
    {
          
          
    	int i;
    	for(i=0;i<n;i++){
          
          
    		printf("%d",a[i]);
    	}
    	return;
    } 
    
    int main(){
          
                          //主函数 
    	int b,c;
    	int a[]={
          
          1,8,7,5,6};
    	int n=5;
    	printf("未排序的数\n"); 
    	Printarr(a,n);
    	printf("\n");
    	BubbleSort(a,n);
    	printf("冒泡排好序的数\n");
    	Printarr(a,n);
    	return 0;
    }
    

Quick sort

Separate the records to be sorted into two independent parts by sorting, and the keywords of some of the records are smaller than the keywords of the other part, and then the two parts of records can be sorted separately to achieve the order of the entire sequence.
Code implementation c

#include <stdio.h>
#include <stdlib.h>

int getStandard(int array[], int i, int j) {
    
    
	//基准数据 
	int key = array[i];
	while (i < j) {
    
     	//因为默认基准是从左边开始,所以从右边开始比较 //当队尾的元素大于等于基准数据 时,就一直向前挪动 j 指针 
			while (i < j && array[j] >= key) {
    
    
				j--;
			}
			if (i < j) {
    
    
				array[i] = array[j];//当找到比 array[i] 小的时,就把后面的值 array[j] 赋给它
			}	
			while (i < j && array[i] <= key) {
    
    //当队首元素小于等于基准数据 时,就一直向后挪动 i 指针 
				i++;
			}
			//当找到比 array[j] 大的时,就把前面的值 array[i] 赋给它
			if (i < j) {
    
    
				array[j] = array[i];
			}
		}
	array[i] = key;//跳出循环时 i 和 j 相等,此时的 i 或 j 就是 key 的正确索引位置
	return i;//把基准数据赋给正确位置 
}
void QuickSort(int array[], int low, int high) {
    
    
	//开始默认基准为 low
	if (low < high) {
    
    
		//分段位置下标 
		int standard = getStandard(array, low, high);
		//递归调用排序
		//左边排序 
		QuickSort(array, low, standard - 1);
		//右边排序 
		QuickSort(array, standard + 1, high);
	}
}

void display(int array[], int size) {
    
    
	int i;
	for (i = 0; i < size; i++) {
    
    
		printf("%d ", array[i]);
	}
	printf("\n");
}

int main() {
    
    
	int array[] = {
    
     49,38,65,97,76,13,27,49,10 };
	int size = sizeof(array) / sizeof(int);
	QuickSort(array, 0, size - 1);
	printf("排好的顺序为") ; 
	display(array, size);
    return 0;
}

Insert picture description here

Time complexity O (n * number of recursive layers) Space complexity O (number of recursive layers)

Minimum number of layers (log2n) maximum n

Optimized ideas When selecting the base axis element, choose the three comparisons between the head, the middle and the tail, or choose a random number as the base axis

Time complexity O (n * number of recursive layers) Space complexity O (number of recursive layers)

Minimum number of layers (log2n) maximum n

Optimized ideas When selecting the base axis element, choose the three comparisons between the head, the middle and the tail, or choose a random number as the base axis

Guess you like

Origin blog.csdn.net/weixin_44518702/article/details/110142091