spriteYang
12/17/2018 - 8:42 AM

ShellSort

class ShellSort {
public:
void sort(int *A, int n, int m)
{
	if (m > 1)
	{
		for (int i = 0; m + i < n; i++)
		{
			if (A[i] > A[i + m])
			{
				int temp = A[i];
				A[i] = A[i + m];
				A[i + m] = temp;
			}
		}
		for (int i = 0; i < n; i++)
		{
			cout << A[i] << " ";
		}
		cout << endl;
	}
	else
	{
		for (int i = 1; i < n; i++)
		{
			for (int j = 0; j < i; j++)
			{
				if (A[i] < A[j])
				{
					int temp = A[j];
					A[j] = A[i];
					A[i] = temp;
				}
			}
		}
	}
}
void shell(int *A, int n, int m)
{
	if (m>1 && m % 2 != 0)
		m = m / 2 + 1;
	else if(m>1 && m % 2==0)
		m = m / 2;
	if (m > 1)
	{
		sort(A, n, m);
		shell(A, n, m);
	}
	else if (m == 1)
	{
		sort(A, n, m);
	}
}
int* shellSort(int* A, int n) {
	// write code here
	int m = n;
	shell(A, n, m);
	return A;
}
}