Ejemplo n.º 1
0
int main()
{
	//get input for asceding or descending
	int direction = 0;
	while (direction != 1 && direction != 2){
		cout << "Press 1 for ascending -- Press 2 for descending. " << endl;
		cin >> direction;
	}

	//init SelectionSort class + begin tests
	SelectionSort select;


	//RECURSIVE selection sort
	populate();
	cout << "Selection Sort [recursive]: " << endl;
	//small array + vec
	printData(arrayS, small);
	select.recursiveSort(direction, arrayS, small, 0);
	printData(arrayS, small);
	printData(vecS, small);
	select.recursiveSort(direction, vecS, small, 0);
	printData(vecS, small);

	//large array + vec
	select.recursiveSort(direction, arrayL, large, 0);
	select.recursiveSort(direction, vecL, large, 0);

	//ITERATIVE selection sort
	populate();
	cout << "Selection Sort [iterative]: " << endl;
	//small array + vec
	printData(arrayS, small);
	select.iterativeSort(direction, arrayS, small);
	printData(arrayS, small);
	printData(vecS, small);
	select.iterativeSort(direction, vecS, small);
	printData(vecS, small);

	//large array + vec
	select.iterativeSort(direction, arrayL, large);
	select.iterativeSort(direction, vecL, large);


  return 0;
}
Ejemplo n.º 2
0
int main()
{
	
	const int dataSizeSmall = 20; //the size of the small data set
	const int loSmall = 100; //the lower  bound for random number generation of the small data set
	const int hiSmall = 300; //the higher bound for random number generation of the small data set

	int arrSmall_1[dataSizeSmall]; //a C-style array with dataSize elements, arrSmall_1
	dataGenerator(arrSmall_1, dataSizeSmall, loSmall, hiSmall); //arr_1 was generated with random number between lo and hi

	int arrSmall_2[dataSizeSmall]; //another C-style array with dataSize elements, arrSmall_2
	copy(arrSmall_1, arrSmall_1 + dataSizeSmall, arrSmall_2); //helper function: std::copy, copy arrSmall_2 from arrSmall_1

	vector<int> vecSmall_1(arrSmall_1, arrSmall_1 + dataSizeSmall); //define a STL vector, copy vecSmall_1 from arrSmall_1
	vector<int> vecSmall_2 = vecSmall_1; //copy vecSmall_2 from vecSmall_1
	
	cout << "---------------- Now test selection sorting with " << dataSizeSmall << " elements ----------------" << endl;
	cout << endl;
	//display the arr_1, arr_2, and vec_1, vec_2
	cout << "Before the selection sort, arrSmall_1: " << endl;
	printData(arrSmall_1, dataSizeSmall);
	cout << "Before the selection sort, arrSmall_2: " << endl;
	printData(arrSmall_2, dataSizeSmall);
	cout << "Before the selection sort, vecSmall_1: " << endl;
	printData(vecSmall_1, dataSizeSmall);
	cout << "Before the selection sort, vecSmall_2: " << endl;
	printData(vecSmall_2, dataSizeSmall);


	SelectionSort ssSmall; //an instance of class SelectionSort

	ssSmall.recursiveSort(arrSmall_1, dataSizeSmall); //sort a C-style arrays with recursive method
	cout << "After the recursive selection sort, arrSmall_1: " << endl;
	printData(arrSmall_1, dataSizeSmall);

	ssSmall.iterativeSort(arrSmall_2, dataSizeSmall);//sort a C-style arrays with iterative method
	cout << "After the iterative selection sort, arrSmall_2: " << endl;
	printData(arrSmall_2, dataSizeSmall);

	ssSmall.recursiveSort(vecSmall_1); //sort a STL vector with recursive method
	cout << "After the recursive selection sort, vecSmall_1: " << endl;
	printData(vecSmall_1, dataSizeSmall);

	ssSmall.iterativeSort(vecSmall_2); //sort a STL vector with iterative method
	cout << "After the iterative selection sort, vecSmall_2: " << endl;
	printData(vecSmall_2, dataSizeSmall);
	cout << endl;

	/////////////////////////////////////////////////////////////////////////////////////////////////////
	// Analyze performance of selection algorithms using large arrays and large vectors.

	const int dataSizeLarge = 8000; //the size of the large data set
	const int loLarge = 0; //the lower  bound for random number generation
	const int hiLarge = 10000; //the higher bound for random number generation

	int arrLarge_1[dataSizeLarge]; //a C-style array with dataSizeLarge elements, arr_1
	dataGenerator(arrLarge_1, dataSizeLarge, loLarge, hiLarge); //arr_1 was generated with random number between lo and hi

	int arrLarge_2[dataSizeLarge]; //another C-style array with dataSizeLarge elements, arr_2
	copy(arrLarge_1, arrLarge_1 + dataSizeLarge, arrLarge_2); //helper function: std::copy, copy large arr_2 from large arr_1

	vector<int> vecLarge_1(arrLarge_1, arrLarge_1 + dataSizeLarge); //define a STL vector, copy vec_1 from arr_1
	vector<int> vecLarge_2(vecLarge_1); //copy large vec_2 from large vec_1

	cout << "---------------- Now test selection sorting with " << dataSizeLarge << " elements ----------------" << endl;
	cout << endl;
	SelectionSort ssLarge; //an instance of class SelectionSort

	TimeStamp ts; //an instance for class TimeStamp
	cout << "For recursively sorting large C-style array with " << dataSizeLarge << " elements: " << endl;
	cout << "Start time:  " << ts.GetTime() << endl; //print timestamp
	ssLarge.recursiveSort(arrLarge_1, dataSizeLarge); //sort a C-style arrays with recursive method
	cout << "Ending time: " << ts.GetTime() << endl; //print timestamp
	cout << endl;

	cout << "For iteratively sorting large C-style array with " << dataSizeLarge << " elements: " << endl;
	cout << "Start time:  " << ts.GetTime() << endl; //print timestamp
	ssLarge.iterativeSort(arrLarge_2, dataSizeLarge);//sort a C-style arrays with iterative method
	cout << "Ending time: " << ts.GetTime() << endl; //print timestamp
	cout << endl;

	cout << "For recursively sorting large STL vector with " << dataSizeLarge << " elements: " << endl;
	cout << "Start time:  " << ts.GetTime() << endl; //print timestamp
	ssLarge.recursiveSort(vecLarge_1); //sort a STL vector with recursive method
	cout << "Ending time: " << ts.GetTime() << endl; //print timestamp
	cout << endl;


	cout << "For iteratively sorting large STL vector with " << dataSizeLarge << " elements: " << endl;
	cout << "Start time:  " << ts.GetTime() << endl; //print timestamp
	ssLarge.iterativeSort(vecLarge_2); //sort a STL vector with iterative method
	cout << "Ending time: " << ts.GetTime() << endl; //print timestamp
	cout << endl;

}