排序_Dir_Insert(直接插入排序)

直接插入排序就是很简单的,逻辑排序,更通俗一点的就是日常生活中我们都是利用这种方法排序的。

贴出代码:

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <iostream>
#include <algorithm>
#include <string>

using namespace std;

int a[10000];

int N;

void Dir_Insert () //直接插入排序 
{
	for (int i = 1; i < N; i++)
	{
		int j = i - 1;
		int t = a[i]; // 这样赋值是为了让你更能清楚的知道i层的含义,仅仅是作为被插入的对象。 
		while (t < a[j] && j != -1) // 一定注意要加上j != -1 不然的话一比较负数就会死循环的! 
		{
			a[j + 1] = a[j];
			j--; 
		}
		a[j + 1] = t;
	}
}

int main()
{
	cout << "Input the sorting number that you want:  " << endl;
	cin >> N;
	cout << "Please input the numbers: " << endl;
	for (int i = 0; i < N; i++)
	{
		cin >> a[i];
	}
	Dir_Insert();
	for (int i = 0; i < N; i++)
	{
		cout << a[i] << " ";
	}
	cout << endl;
	system ("pause");
	return 0;
}
	


`__dir__` 是一个特殊方法(也称为魔术方法或魔法方法),用于定义一个对象的自定义 `dir()` 行为。当使用内置函数 `dir()` 来获取一个对象的属性和方法列表时,如果该对象定义了 `__dir__` 方法,Python 会调用该方法来获取列表。 例如,假设有一个自定义的类 `MyClass`,我们可以在该类中定义 `__dir__` 方法来返回自定义的属性和方法列表。下面是一个示例: ```python class MyClass: def __dir__(self): return ['attr1', 'attr2', 'method1', 'method2'] obj = MyClass() print(dir(obj)) ``` 输出结果为: ``` ['attr1', 'attr2', 'method1', 'method2'] ``` 可以看到,我们在 `MyClass` 类中定义了 `__dir__` 方法,该方法返回了一个自定义的属性和方法列表。当调用 `dir(obj)` 时,Python 会调用 `obj.__dir__()` 方法来获取列表。 另一方面,`dir()` 是一个内置函数,用于获取一个对象的属性和方法列表。当我们调用 `dir(obj)` 时,Python会返回该对象所有可用的属性和方法的名称列表。 ```python obj = [1, 2, 3] print(dir(obj)) ``` 输出结果为: ``` ['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', 'append', 'clear', 'copy', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort'] ``` 可以看到,`dir(obj)` 返回了一个包含列表对象 `obj` 的所有属性和方法名称的列表。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值