データ構造:一般的なソートアルゴリズム(2):直接挿入ソート(C ++実装)

データ構造:一般的なソートアルゴリズム(2):直接挿入ソート

挿入ソート:

1.アイデア:各ステップで、すべての挿入がソートされるまで、ソートされるレコードが、シーケンスコードサイズに従って、以前にソートされたワードシーケンスの適切な位置に挿入されます。
2.重要な質問:以前にソートされたシーケンスで適切な挿入位置を見つけます。
方法:への直接挿入ソートバイナリ挿入ソートシェルソート

直接挿入ソート

1.基本的な考え方:

各ステップで、すべての挿入がソートされるまで、ソートされるレコードが、シーケンスコードサイズに従って(後ろから前に適切な位置を見つけた後)、前にソートされたワードシーケンスの適切な位置に挿入されます。(これは最も簡単な並べ替え方法です。基本的な操作は、並べ替えられた順序付きリストにレコードを挿入して、レコード数を1つ増やした新しい順序付きリストを取得することです。)

2.例

***例1:直接挿入方法を使用して配列a [6] = {20,30,40,10,60,50}を並べ替えます***

img

[画像ソース:https://www.cnblogs.com/zwtgyh/p/10631760.html]

コード

実装の最初の方法:2つのforループを使用する

#include<iostream>
#include<cstdio>
using namespace std;
int main() {
	/* 插入排序*/
	int num[6] = { 20, 30, 40, 10, 60, 50 };
	int temp;
	int i, j;
	int length = sizeof(num) / sizeof(num[0]);
	for (i = 1; i < length; i++)
	{
		temp = num[i];    //待排序元素
		for (j = i - 1; j >= 0 && num[j] > temp; j--)
		{
			num[j + 1] = num[j];
		}
		num[j + 1] = temp;
	}
	for (int i = 0; i < length; i++)
	{
		cout << num[i] << " ";
	}
	return 0;
}

実現の2番目の方法:

#include<iostream>
#include<cstdio>
using namespace std;
int main() {
	/* 插入排序*/
	int num[6] = { 20, 30, 40, 10, 60, 50 };
	int pos, temp;
	int length = sizeof(num) / sizeof(num[0]);
	for (int i = 1; i < length; i++)
	{
		pos = i - 1;    //有序序列的最后一个元素位置
		temp = num[i];    //保存待排序元素的值
		while (pos >= 0 && num[pos] > temp)
		{
			num[pos + 1] = num[pos];
			pos--;
		}
		num[pos + 1] = temp;    //将待排序元素插入数组中
	}
	for (int i = 0; i < length; i++)
	{
		cout << num[i] << " ";
	}
	return 0;
}

**例2:直接挿入ソートを使用して配列b [15] = {3,44,38,5,47,15,36,26,27,2,46,4,19,50,48}をソートします

img

[画像ソース:https://www.cnblogs.com/zwtgyh/p/10631760.html]

コード

上記と同じ方法で、両方の方法を実現できます

方法1:

#include<iostream>
#include<cstdio>
using namespace std;
int main() {
	/* 插入排序*/
    int num[15] = { 3, 44, 38, 5, 47, 15, 36, 26, 27, 2, 46, 4, 19, 50, 48 };
	int temp;
	int i, j;
	int length = sizeof(num) / sizeof(num[0]);
	for (int i = 1; i < length; i++)
	{
		temp = num[i];    //待排序元素
		for (int j = i - 1; j >= 0 && num[j] > temp; j--)
		{
		C	num[j + 1] = num[j];
		}
		num[j + 1] = temp;
	}
	for (int i = 0; i < length; i++)
	{
		cout << num[i] << " ";
	}
	return 0;
}

方法2:

#include<iostream>
#include<cstdio>
using namespace std;
int main() {
	/* 插入排序*/
	int num[15] = { 3, 44, 38, 5, 47, 15, 36, 26, 27, 2, 46, 4, 19, 50, 48 };
	int pos, temp;
	int length = sizeof(num) / sizeof(num[0]);
	for (int i = 1; i < length; i++)
	{
		pos = i - 1;    //有序序列的最后一个元素位置
		temp = num[i];    //保存待排序元素的值
		while (pos >= 0 && num[pos] > temp)
		{
			num[pos + 1] = num[pos];
			pos--;
		}
		num[pos + 1] = temp;    //将待排序元素插入数组中
	}
	for (int i = 0; i < length; i++)
	{
		cout << num[i] << " ";
	}
	return 0;
}

3.要約:

1.挿入ソートの最良のケースは、配列がすでに順序付けられていることです。現時点では、n-1回の比較のみが必要であり、時間の複雑さはO(n)です。

2.最悪の場合、配列が逆の順序でソートされます。このとき、n(n-1)/ 2の比較とn-1の割り当て操作(挿入)が必要です。

3.平均して、挿入ソートアルゴリズムの複雑さはO(n2)です。

4.スペースの複雑さに関しては、直接挿入方法はその場でソートされており、スペースの複雑さは(O(1))です。

おすすめ

転載: blog.csdn.net/qq_43801020/article/details/108134951
おすすめ