Aprenda los cuatro métodos de clasificación bajo la plantilla de c ++ (clasificación por selección, clasificación por burbujas, clasificación rápida, clasificación por inserción)

Hoy, mientras aprendía sobre plantillas en C ++, descubrí que necesito aprender sobre cuatro métodos de clasificación comunes de C / C ++:

Las siguientes reglas antiguas, adjuntan algunos enlaces en el estudio:

csdn

Programa completo de cuatro algoritmos de clasificación en lenguaje C

Explicación detallada e implementación de la clasificación de selección de lenguaje C

Video explicativo en la estación B

Este video tiene una columna gráfica || [Video + Gráfico + Animación] Orden de selección detallado

Seleccione el código para ordenar

#include <iostream>
#include<string>
using namespace std;
template<typename T>
void myswap(T &a,T&b)
{
    
    
	T temp = a;
	a = b;
	b = temp;
}

template <class T>
void mysort(T arr[],int len)
{
    
    
	for (int i = 0; i < len; i++)
	{
    
    
		int max = i;
		for (int j = i + 1; j < len; j++)
		{
    
    
			if (arr[j] > arr[max])
			{
    
    
				max = j;
			}
		}
		if (max != i)
		{
    
    
			myswap(arr[i], arr[max]);
		}

	}	

}
template<class T>
void print_arr(T arr[],int len)
{
    
    
	for (int i = 0; i < len; i++)
	{
    
    
		cout << arr[i] << endl;
	}
}
void test()
{
    
    
	int len = 0;

	char tbuf[20] = "fdbcae";
	len = sizeof(tbuf) / sizeof(char);
	mysort(tbuf,len );
	print_arr(tbuf, len);
}
void test2()
{
    
    
	int len = 0;

	int tbuf[] = {
    
    5,6,59,54,12};
	len = sizeof(tbuf) / sizeof(int);
	mysort(tbuf, len);
	print_arr(tbuf, len);
}
int main()
{
    
    
	test();
	//test2();
	return 0;
}

Código de clasificación de burbujas

//冒泡排序
void BubleSort(int a[],int n)
{
    
    
    int i,j,x;
    for(i=0;i<n;i++)   
    {
    
    
        for(j=1;j<n-i;j++) 
        {
    
    
            if(a[j-1]>a[j]) 
            {
    
    
                x=a[j];
                a[j]=a[j-1];
                a[j-1]=x;
            }
        }
    }
}

Ordenación rápida

Más complicado, usando la idea de ramificación y recursividad:

Publique un video de preparación detallado:
algoritmo de clasificación rápida

//快速排序
void QuickSort(int a[], int left, int right)
{
    
    
    if (left < right)
    {
    
    
        int i = left, j = right, x = a[left];
        while (i < j)
        {
    
    
            while(i < j && a[j] >= x) // 从右向左找第一个小于x的数
                j--;  
            if(i < j) 
                a[i++] = a[j];

            while(i < j && a[i] < x) // 从左向右找第一个大于等于x的数
                i++;  
            if(i < j) 
                a[j--] = a[i];
        }
        a[i] = x;
        QuickSort(a, left, i - 1); // 递归调用 
        QuickSort(a, i + 1, right);
    }
}

Clasificación por inserción directa

Dar enlace:
tipo de inserción directa de algoritmo de clasificación

template<class T>
void InsertSort(T* array, int n) {
    
                   //array待排序数组,n:数组元素数量
	int i, j;                                    //循环变量
	T temp;                                      //存储待排序元素
	for (i = 1; i < n; i++) {
    
    
		j = i;
		temp = array[i];                         //待排序元素赋值给临时变量
		while (j > 0 && temp < array[j - 1]) {
    
       //当未达到数组的第一个元素或者待插入元素小于当前元素
			array[j] = array[j - 1];             //就将该元素后移
			j--;                                 //下标减一,继续比较
		}
		array[j] = temp;                         //插入位置已经找到,立即插入
	} 
}

Supongo que te gusta

Origin blog.csdn.net/ice_masters/article/details/108760013
Recomendado
Clasificación