oral_quiz->#QuickSort#

我回忆的QuickSort:
#include <stdio.h>

int randomNum(int start, int end);
int partition(int* array, int start, int end, int value);

void swap(int* array, int i, int j) {
	int temp = array[i];
	array[i] = array[j];
	array[j] = temp;
}

void quickSort(int* array, int start, int end) {
	if(start >= end) return;
	int random = randomNum(start, end);
	int value = array[random];
	//swap value to end
	swap(array, random, end);

	int rightIndex = partition(array, start, end, value);
	//swap value back
	swap(array, rightIndex, end);

	//left side
	quickSort(array, start, rightIndex-1);
	//right side
	quickSort(array, rightIndex+1, end);
}

int randomNum(int start, int end) {
	return (start+end)/2;
}

int partition(int* array, int start, int end, int value) {
	while(start != end) {
		while(array[start]<value && start<end) {
			++start;
		}
		while(array[end]>=value && end>start) {
			--end;
		}
		swap(array, start, end);
	}
	return start;
}

//=========================Test===========================
void Test(const char* testName, int* array, int start, int end) {
	if(testName != NULL) printf("%s begins:\n", testName);
	//original array
	for(int i=start; i<=end; ++i) {
		printf("%d ", array[i]);
	}
	printf("\n");

	quickSort(array, start, end);

	//sorted array
	for(int i=start; i<=end; ++i) {
		printf("%d ", array[i]);
	}
	printf("\n");
}

//normal array
void Test1() {
	const int length = 8;
	int array[length] = {5,3,8,4,2,0,6,7};
	Test("Test1", array, 0, 7);
}

//include same number
void Test2() {
	const int length = 4;
	int array[length] = {9,7,9,7};
	Test("Test2", array, 0, 3);
}

//include number < 0
void Test3() {
	const int length = 7;
	int array[length] = {-8,-5,4,-79,5,2,-1};
	Test("Test3", array, 0, 6);
}

//positive sequence
void Test4() {
	const int length = 5;
	int array[length] = {5,7,9,17,21};
	Test("Test4", array, 0, 4);
}

//reversed sequence
void Test5() {
	const int length = 5;
	int array[length] = {25,17,9,7,2};
	Test("Test5", array, 0, 4);
}

int main(int argc, char* argv[]) {
	Test1();
	Test2();
	Test3();
	Test4();
	Test5();
	return 0;
}



hht's QuickSort:
#include <stdio.h>
#include <exception>
using namespace std;


void Swap(int* p1, int* p2) {
 int temp;
 temp = *p1;
 *p1 = *p2;
 *p2 = temp;
}


int RandomInRange(int start, int end) {
 return (start+end)/2;
}


int Partition(int* data, int length, int start, int end) {
 if(data == NULL || length <= 0 || start < 0 || end >=length)
 throw std::exception();


 int index = RandomInRange(start, end);
 Swap(&data[index], &data[end]);


 int small = start - 1;
 for(index=start; index<end; ++index) {
 if(data[index] < data[end]) {
 ++small;
 if(small != index) {
 Swap(&data[small], &data[index]);
 }
 }
 }
 ++small;
 Swap(&data[small], &data[end]);


 return small;
}


void QuickSort(int* data, int length, int start, int end) {
 if(start == end) return;


 int index = Partition(data, length, start, end);
 if(index < end) {
 QuickSort(data, length, index+1, end);
 }
 if(start < index) {
 QuickSort(data, length, start, index-1);
 }
}


//=========================Test===========================
void Test(const char* testName, int* array, int length, int start, int end) {
 if(testName != NULL) printf("%s begins:\n", testName);
 //original array
 for(int i=start; i<=end; ++i) {
 printf("%d ", array[i]);
 }
 printf("\n");


 QuickSort(array, length, start, end);


 //sorted array
 for(int i=start; i<=end; ++i) {
 printf("%d ", array[i]);
 }
 printf("\n");
}


//normal array
void Test1() {
 const int length = 8;
 int array[length] = {5,3,8,4,2,0,6,7};
 Test("Test1", array, 8, 0, 7);
}


//include same number
void Test2() {
 const int length = 4;
 int array[length] = {9,7,9,7};
 Test("Test2", array, 4, 0, 3);
}


//include number < 0
void Test3() {
 const int length = 7;
 int array[length] = {-8,-5,4,-79,5,2,-1};
 Test("Test3", array, 7, 0, 6);
}


//positive sequence
void Test4() {
 const int length = 5;
 int array[length] = {5,7,9,17,21};
 Test("Test4", array, 5, 0, 4);
}


//reversed sequence
void Test5() {
 const int length = 5;
 int array[length] = {25,17,9,7,2};
 Test("Test5", array, 5, 0, 4);
}


int main(int argc, char* argv[]) {
 Test1();
 Test2();
 Test3();
 Test4();
 Test5();
 return 0;
}



shaffer's QuickSort:
有时间补上

你可能感兴趣的:(快排)