insertion sort.cpp

来自「Program to implement insertion sort」· C++ 代码 · 共 45 行

CPP
45
字号
#include <iostream>

#define ELEMENTS 6

using namespace std;

void insertion_sort(int x[],int length)
{
	int key,i;
	for(int j=1;j<length;j++)
	{
		key=x[j];
		i=j-1;
		while(x[i]>key && i>=0)
		{
			x[i+1]=x[i];
			i--;
		}
		x[i+1]=key;
	}
}

int main()
{
	int A[ELEMENTS];
	int x;
	cout<<"Enter six numbers."<<endl;
	for (int i=0; i<6; i++)
	{
		cin>>A[i];
	}
	cout<<"NON SORTED LIST:"<<endl;
	for(x=0;x<ELEMENTS;x++)
	{
		cout<<A[x]<<endl;
	}
	insertion_sort(A,ELEMENTS);
	cout<<endl<<"SORTED LIST"<<endl;
	for(x=0;x<ELEMENTS;x++)
	{
		cout<<A[x]<<endl;
	}
	return 0;
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?