面試題算法

快速排序

int key(vector<int> &a, int low, int high)
{
	int key = a[low];
	while (low < high)
	{
		while (low < high&&key <= a[high])
			high--;
		a[low] = a[high];
		while (low<high&&key>=a[low])
			low++;
		a[high] = a[low];
	}
	a[low] = key;
	return low;

}

void QSort(vector<int> &a, int low, int high)
{
	int key_record;
	if (low < high)
	{
		key_record = key(a,low,high);
		QSort(a, low, key_record - 1);
		QSort(a, key_record + 1, high);
	}


}

int main()
{
	vector<int> a{ 1,5,9,7,5,6,85,98,45,65,45,87,5,4,65,0,64,48 };
	int len = a.size();
	
	QSort(a, 0, len - 1);

	cout << "快排:" << endl;
	for (int i = 0; i < len; i++)
	{
		cout << a[i] << "  ";
	}
	cout << endl;
	system("pause");
}

實現memcpy

void* my_memcpy(void *dest, const void*src, size_t length)
{
	assert(dest != NULL && src != NULL);

	char *pdest = (char*)dest;
	const char *psrc = (char*)src;
	while (length--)
	{
		*pdest++ = *psrc++;
	}
	return dest;
}
int main()
{
	char a[] = "sdsf";
	char b[] = "hdfhgh";

	my_memcpy(a, b, 4);

	printf("%s", a);


	system("pause");
}

實現strcpy

char* my_strcpy(char* dest,const char* src)
{
	assert((dest != NULL) && (src != NULL));
	char *pDest = (char*)dest;
	while ((*pDest++ = *src++) != '\0');
		
	return dest;
}



int main()
{
	char a[] = "hdfhgh";
	const char b[] = "sfaa";

	my_strcpy(a,b);

	printf_s("%s", a);


	system("pause");
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章