Test the time complexity of algorithms such as binary search and direct selection sorting

        1.
        Binary search Binary search, also known as binary search, the premise of binary search is that the linear table or tree has been sorted. Here, the linear table is taken as an example. It is assumed that the linear table has been sorted by keywords and is increasing Orderly arranged.
        The idea of ​​binary search: Let the linear table R[left, right] be the current search interval, first determine the midpoint position of the interval mid = (left+right)/2, and then compare the K value to be checked with R[mid] The comparison of .key is as follows:
        1) If K = R[mid].key, then directly return the index of the element;
        2) If K <R[mid].key, then
            left = mid-1;
            mid = (left+right)/2;
      search in the left half interval [left, mid-1];
        3) If K> R[mid].key, then
            right = mid+1;
            mid = (left+right)/2;
      Search in the right half of the interval [left, mid-1];
    4) Repeat steps 2) and 3) until left> right, that is, the search space becomes 0. If found, return the index of the element. Otherwise, it returns -1.
    Binary search, each search, the search interval is reduced by half, so the algorithm is very fast, the time complexity is O (log n) O(log \,n)O(logn ) .
    //Binary search binarySearch()

	int binarySearch(int arr[], int n, int target) {
    
    
		int left = 0, right = n-1;
		while (left <= right) {
    
    
			int mid = (left+right)/2;
			if(arr[mid] == target)
				return mid;

			if(arr[mid] > target)
				right = mid - 1;
			else
				left = mid + 1;
		}
		return -1;
	}

    2.
    Selection sort The basic method of selection sort is: each step selects the record with the smallest key word from the records to be sorted, and the order is placed at the end of the sorted records until all sorts are completed. Selection sorting includes direct selection sorting and heap sorting. Here is a direct selection sort.
    The process of direct selection and sorting is: suppose the records are placed in R[0,n-1], R[0,i-1] is the ordered area, R[i,n-1] is the disordered area, and there are All keywords in the sequence area are smaller than all keywords in the disorder area. Need to add R[i] to R[0,i-1] to make R[0,i] ordered. Here, each pass selects a record R[k] with the smallest key from the disordered area R[i,n-1] and exchanges it with R[i]. Obviously R[0,i] becomes A new orderly area.
    2.1) Use structure to indicate direct selection sort


#define MaxSize 100
typedef int KeyType;
typedef char ElemType;
typedef struct{
    
    
	KeyType  key;       //关键字域
	ElemType data[10];  //其他数据域
}SqList;

void SelectSort(SqList R[], int n) {
    
    
	int i, j, k;
	SqList tmp;
	for (i = 0; i < n - 1; j++) {
    
    
		k = i;
		for (j = i + 1; j < n; j++) {
    
    
			if (R[j].key < R[k].key)
				k = j;
		}

		tmp = R[i];
		R[i] = R[k];
		R[k] = tmp;
	}
}

    2.2 Use an array to indicate direct selection sort

//交换a和b 方法一
void swapValue(int &a, int &b) {
    
    
	int tmp = a;
	a = b;
	b = tmp;
}

//交换a和b 方法二
void swapValue2(int &a, int &b) {
    
    
	a = a + b;
	b = a - b;
	a = a - b;	
}


//直接选择排序
void selectionSort(int arr[], int n) {
    
    
	for (int i = 0; i < n - 1; i++) {
    
    
		int minIndex = i;
		for (int j = i + 1; j < n; j++) {
    
    
			if (arr[j] < arr[minIndex]) {
    
    
				minIndex = j;
			}
		}
		swapValue(arr[i], arr[minIndex]);
	}
}

    3. Writing test cases for algorithms
    3.1 Put all algorithms in a command space, such as namespace MyAlgorithmTester, which is a header file: MyAlgorithm.h
    //MyAlgorithm.h

#ifndef MY_ALGORITHM_H
#define MY_ALGORITHM_H

#include <iostream>
#include <cassert>
#include <cmath>

using namespace std;

#define min(a,b)  (((a)<(b))?(a):(b))

namespace MyAlgorithmTester {
    
    

	//O(logN)
	int binarySearch(int arr[], int n, int target) {
    
    
		int left = 0, right = n-1;
		while (left <= right) {
    
    
			int mid = (left+right)/2;
			if(arr[mid] == target)
				return mid;

			if(arr[mid] > target)
				right = mid - 1;
			else
				left = mid + 1;
		}
		return -1;
	}

	//O(N)
	int findMax(int arr[],int n) {
    
    
		assert(n > 0);
		int res = arr[0];
		for (int i=1;i<n;i++) {
    
    
			if(arr[i] > res)
				res = arr[i];
		}

		return res;
	}

	//O(NlogN)
	void __merge(int arr[],int left,int mid,int right, int aux[]) {
    
    
		int i = left,j = mid+1;
		for(int k=left; k<= right; k++) {
    
    
			if (i > mid) {
    
    
				arr[k] = aux[j];
				j++;
			}
			else if (j > right) {
    
    
				arr[k] = aux[i];
				i++;
			}
			else if (aux[i] < aux[j]) {
    
    
				arr[k] = aux[i];
				i++;
			}
			else {
    
    
				arr[k] =aux[j];
				j++;
			}		
		}
	}

	//自顶向上排序
	void mergeSort(int arr[], int n) {
    
    
		int *aux = new int[n];
		for (int i=0;i<n;i++) {
    
    
			aux[i] = arr[i];
		}

		for (int sz=1;sz<n;sz += sz) {
    
    
			for (int i=0; i < n; i += sz+sz) {
    
    
				__merge(arr,i,i+sz-1,min(i+sz+sz-1, n-1), aux);
			}
		}

		delete[] aux;
		return;
	}

	//O(N^2)
	void selectionSort(int arr[], int n) {
    
    
		for (int i=0;i<n-1; i++) {
    
    
			int minIndex = i;
			for (int j=i+1; j<n; j++) {
    
    
				if(arr[j] < arr[minIndex])
					minIndex = j;
			}
			swap(arr[i],arr[minIndex]);
		}
	}


}

#endif

    3.2) Put the function that generates the data set in the MyUtil namespace, which is also a header file: MyUtil.h
    //MyUtil.h

#ifndef MY_UTIL_H
#define MY_UTIL_H

#include <iostream>
#include <cassert>
#include <ctime>
using namespace std;

namespace MyUtil {
    
    
	int *generateRandomArray(int n, int rangeL, int rangeR) {
    
    
		assert(n > 0 && rangeL <= rangeR );

		int *arr = new int[n];
		srand(time(NULL));
		for (int i = 0; i < n; i++) {
    
    
			arr[i] = rand() % (rangeR - rangeL + 1) + rangeL;
		}
		return arr;
	}

	int *generateOrderedArray(int n) {
    
    
		assert(n > 0);

		int *arr = new int[n];
		for (int i = 0; i < n; i++) {
    
    
			arr[i] = i;
		}
		return arr;
	}

}


#endif

    3.3) Write the main function
    //main.cpp

#include "MyAlgorithm.h"
#include "MyUtil.h"

#include <cmath>
#include <ctime>
#include <windows.h>
using namespace std;

int main() {
    
    

	//1) findMax 数据规模倍乘测试 
	//O(n)
	//for (int i = 10; i <= 26; i++) {
    
    
	//	int n = pow(2, i);
	//	int *arr = MyUtil::generateRandomArray(n, 0, 100000000);

	//	clock_t startTime = clock();
	//	MyAlgorithmTester::findMax(arr, n);
	//	clock_t endTime = clock();

	//	cout << "data size 2^" << i << " = " << n << "\t";
	//	cout << "Time cost: " << double(endTime - startTime) / CLOCKS_PER_SEC << endl;

	//	delete[] arr;
	//}

	//2) selectionSort 数据规模倍乘测试 
	//O(n^2)
	//cout << "Test for selectionSort:" << endl;
	//for (int i = 10; i <= 15; i++) {
    
    
	//	int n = pow(2, i);
	//	int *arr = MyUtil::generateRandomArray(n, 0, 100000000);

	//	clock_t startTime = clock();
	//	MyAlgorithmTester::selectionSort(arr, n);
	//	clock_t endTime = clock();

	//	cout << "data size 2^" << i << " = " << n << "\t";
	//	cout << "Time cost: " << double(endTime - startTime) / CLOCKS_PER_SEC << endl;

	//	delete[] arr;
	//}

	//3) binarySearch 数据规模倍乘测试 
	//O(logn)
	//cout << "Test for selectionSort:" << endl;
	//double run_time = 0;
	//LARGE_INTEGER time_start;
	//LARGE_INTEGER time_end;
	//double dqFreq;
	//LARGE_INTEGER freq;
	//QueryPerformanceFrequency(&freq);
	//dqFreq = (double)freq.QuadPart;

	//for (int i = 10; i <= 27; i++) {
    
    
	//	int n = pow(2, i);
	//	int *arr = MyUtil::generateRandomArray(n, 0, 100000000);		

	//	QueryPerformanceCounter(&time_start);
	//	MyAlgorithmTester::binarySearch(arr, n, 0);
	//	QueryPerformanceCounter(&time_end);
	//	run_time = 1000000 * (time_end.QuadPart - time_start.QuadPart) / dqFreq;
	//	cout << "data size 2^" << i << " = " << n << "\t";
	//	cout << "Time cost: " << run_time << endl;

	//	delete[] arr;
	//}

	//4) binarySearch 数据规模倍乘测试 
	//O(logn)
	cout << "Test for selectionSort:" << endl;
	double run_time = 0;
	LARGE_INTEGER time_start;
	LARGE_INTEGER time_end;
	double dqFreq;
	LARGE_INTEGER freq;
	QueryPerformanceFrequency(&freq);
	dqFreq = (double)freq.QuadPart;

	for (int i = 10; i <= 26; i++) {
    
    
		int n = pow(2, i);
		int *arr = MyUtil::generateRandomArray(n, 0, 100000000);

		QueryPerformanceCounter(&time_start);
		MyAlgorithmTester::mergeSort(arr, n);
		QueryPerformanceCounter(&time_end);
		run_time = 1000 * (time_end.QuadPart - time_start.QuadPart) / dqFreq;
		cout << "data size 2^" << i << " = " << n << "\t";
		cout << "Time cost: " << run_time << endl;

		delete[] arr;
	}



	system("pause");

	return 0;
}

    3.3) Algorithm description
    a) findMax() Find the largest element in the array.
    This algorithm, when the amount of data doubles, the algorithm time consumption doubles, indicating that the time complexity of the algorithm is linear, that is, the time complexity of findMax() is O (n) O(n)O ( n )

Figure (1) findMax() Find the largest element algorithm

    b) selectionSort() directly selects and sorts
    the array . When the amount of data doubles, the algorithm takes 4 times longer, indicating that the time complexity of the algorithm is an exponential type, with an exponent of log 2 4 = 2 log_2{4} = 2log24=2 , that is, the time complexity of the selectionSort() algorithm isO (n 2) O(n^2)O ( n2)

Figure (2) selectionSort() direct selection sorting algorithm

    c) binarySearch() performs binary search on the array
    . When the amount of data doubles, the time-consuming increase of the algorithm is very small, almost no increase. It can be inferred that the time complexity of the algorithm is O (log n) O(log \,n)O(logn)

Figure (3) binarySearch() binary search algorithm

    c) mergeSort() merges and sorts
    the array . When the amount of data doubles, the algorithm takes a little more than 2 times, indicating that the complexity of the algorithm is between O (n) O(n)O ( n ) ~O (n 2) O (n ^ 2)O ( n2 ), it can be speculated that the time complexity of the algorithm isO (nlog n) O(nlog \,n)O ( n l o gn)

Figure (4) mergeSort() merge sort algorithm

        References:
        1. Data structure exercises and analysis level B, Li Chunbao, 2006

Guess you like

Origin blog.csdn.net/sanqima/article/details/107578007