7,8 bucket sort of Fortune left counting sequencing method and its application subject based class2-

1. counting sequencing

Suppose there are 15 number, in the range of (0-9), do not compare to the number of all lined up.

Introduction

1. Sort the count data is based on the actual situation of a non-sorting algorithm to compare samples are sorted closely related, the
order is not often used in practice
2. The time complexity of O (N), the additional space complexity O (N )
3. stable sort

analysis

1. Prepare the same length range of the tub and the tub - A 10 value (value 0-9)
2. iterate, a value is, the position of a barrel of a number plus
3. Reconstruction array

Core code

Traversing: a [] is an auxiliary array, as used to count the number of barrels; arr [i] is the original array. Values ​​of the original array as the ordinal position of the barrel, the barrel plus a value of this position.

	for(int i = 0;i < arr_Length;i++)
	{
		a[arr[i]]++;
	}

The complete code

#include <iostream>
using namespace std;
#include<time.h>
#define arr_Length 15


int main()
{
	//生成随机数
	srand((unsigned)time(NULL));
	int arr[arr_Length];
	cout << "原数组arr: ";
	for(int i = 0;i < arr_Length;i++)
	{
		arr[i] = rand()%10;
		cout << arr[i]<< ' ';
	}
	cout << endl <<endl;

	//准备桶 O(n)
	int a[arr_Length] = {0};

	//遍历O(n)
	for(int i = 0;i < arr_Length;i++)
	{
		a[arr[i]]++;
	}

	//重构O(n)
	for(int i = 0;i < arr_Length;i++)
	{
		if(a[i] != 0)
			cout<<a[i]<<"个"<<i<<endl;
	}

	system("pause");
	return 0;
}

2. Application: Given an array, after sorting if required, a maximum difference of two adjacent number, required time complexity O (N), and the request can not be compared with a non-sort based. [9 generates a random number, in the range 0-99]

analysis

1. Assuming a total number of N, N + 1 prepared buckets. Traversing the array to find the minimum and maximum, respectively, in the 0 position and a final position. The value between the maximum and minimum values in the array is divided into N + 1 aliquot (N + 1 buckets).
2. Prepare three arrays of length N + 1, namely num_max [], num_min [], num_empty []. It indicates the current position of the barrel of maximum, minimum and bucket is empty. An array of N + 1 then filled buckets, updating maximum and minimum values. There must be an empty barrel (N + 1 buckets, N number) after filling.
3. No. 1 starting with the tub, the tub is empty if continued to the next, a non-empty bucket seeking the maximum value and the minimum difference of current bucket when the front barrel to find a non-empty. After traversing the maximum value is also desired.
Tips1: not only find the number of empty drums on both sides, although the difference is smaller than the same difference between the tub empty bucket, but not necessarily on both sides of the maximum value of the empty barrel. eg in the range of 10 is assumed bucket 19 (10-19) | empty (20-29) | 30 (30-39) | 49 (40-49), the maximum is 19 at this time.
Tips1: If max == min returned as 0 values of all array.

Core code

1. Find the entire array of min and max

int min = arr[0];
	int max = arr[0];
	for(int i = 0;i<length;i++)
	{
		min = arr[i] < min ? arr[i] : min;
		max = arr[i] > max ? arr[i] : max;
	}

2. The maximum value is the minimum value min, max, the division between the number of min, max buckets of N + 1, the presence of a number of the current bucket values.

a = (arr[i]-min)*(length+1) / (max-min+1)

Appreciated:
(. 1) (min-max +. 1) refers to the number of a minimum value between a maximum value, (length + 1) refers to the length of the barrel, (max-min + 1) / (length + 1) between the means the number of equally. The current number of arr [i] is then divided by bisecting the denominator is turned up formula.
(2) Code Division left God a = (arr [i] -min ) * (length) / (max-min); not when debugging.
eg length of the array 9, the length of the tub 10, maximum 99, the minimum value 0, should be divided into 10 groups, 0-9,10-19 ... 90-99, the code will be problems left God. In addition, max-min + 1 if not, then a more realistic feel, but debugging error. When the number is 99, (99-0) × 10 / ( 99-0) = 10, i.e. position 11, the length of the array 10 will overflow, where god need explain.

3. Update the maximum and minimum barrel

There are several empty barrels current number directly into the barrel when the barrel is assigned the most value.

bool num_empty[length+1] = {0};
	int num_max[length+1] = {0};
	int num_min[length+1] = {0};

	for(int i = 0;i<length;i++)//对所有的数循环不是对所有桶循环不能length+1
	{
		int a = (arr[i]-min)*(length+1) / (max-min+1);
		
		if(num_empty[a] == 0)
		{
			num_max[a] = arr[i];
			num_min[a] = arr[i];
			num_empty[a] = 1;
		}
		num_max[a] = arr[i]>num_max[a]?arr[i]:num_max[a];
		num_min[a] = arr[i]<num_min[a]?arr[i]:num_min[a];
		
	}

4. seeking maximum difference

The maximum difference is a maximum value before generating the tub into the tub current minimum. First, the maximum recording bucket 0, then traversing the 1-position, traverses only non-empty buckets, then update the maximum value of the maximum difference and a front of the tub, is used as the next comparison.

int res = 0;//最大差值
	int last_max = num_max[0];
	for(int i = 1;i < length + 1;i++)
	{
		if(num_empty[i])
		{
			res = (num_min[i] - last_max) < res ? res: (num_min[i] - last_max);
			last_max = num_max[i];
		}
	}

Auxiliary Code:

void print(int arr[],int max,int min,int num_max[],int num_min[],bool num_empty[])
{
	cout<<"arr: ";
	for(int i = 0;i < length ;i++)
	{
		cout<<arr[i]<<" ";
	}
	cout<<endl<<"max= :"<<max<<"    "<<"min= :"<<min<<endl;
	cout<<"num_max:";
	for(int i = 0;i < length+1 ;i++)
	{
		cout<<num_max[i]<<" ";
	}
	cout<<endl<<"num_min:";
	for(int i = 0;i < length+1 ;i++)
	{
		cout<<num_min[i]<<" ";
	}
	cout<<endl;
}

The complete code

#include<iostream>
#include<time.h>
#define length  9
using namespace std;

//输出可视化
void print(int arr[],int max,int min,int num_max[],int num_min[],bool num_empty[])
{
	cout<<"arr: ";
	for(int i = 0;i < length ;i++)
	{
		cout<<arr[i]<<" ";
	}
	cout<<endl<<"max= :"<<max<<"    "<<"min= :"<<min<<endl;
	cout<<"num_max:";
	for(int i = 0;i < length+1 ;i++)
	{
		cout<<num_max[i]<<" ";
	}
	cout<<endl<<"num_min:";
	for(int i = 0;i < length+1 ;i++)
	{
		cout<<num_min[i]<<" ";
	}
	cout<<endl;
}

//
int maxgap(int arr[])
{
	//找出整个数组的min和max
	int min = arr[0];
	int max = arr[0];
	for(int i = 0;i<length;i++)
	{
		min = arr[i] < min ? arr[i] : min;
		max = arr[i] > max ? arr[i] : max;
	}
	if(max == min)
		return 0;
	bool num_empty[length+1] = {0};
	int num_max[length+1] = {0};
	int num_min[length+1] = {0};

	for(int i = 0;i<length;i++)//对所有的数循环不是对所有桶循环不能length+1
	{
		int a = (arr[i]-min)*(length+1) / (max-min+1);
		
		if(num_empty[a] == 0)
		{
			num_max[a] = arr[i];
			num_min[a] = arr[i];
			num_empty[a] = 1;
		}
		num_max[a] = arr[i]>num_max[a]?arr[i]:num_max[a];
		num_min[a] = arr[i]<num_min[a]?arr[i]:num_min[a];
		
	}
	print(arr,max,min,num_max,num_min,num_empty);
	int res = 0;
	int last_max = num_max[0];
	for(int i = 1;i < length + 1;i++)
	{
		if(num_empty[i])
		{
			res = (num_min[i] - last_max) < res ? res: (num_min[i] - last_max);
			last_max = num_max[i];
		}
	}
	return res;
}

int main()
{
	//生成随机数组
	int arr[length] ;
	srand((unsigned)time(NULL));
	for(int i = 0;i<length;i++)
	{
		arr[i] = rand()%100;
	}

	cout<<"maxgap=:"<<maxgap(arr);

	system("pause");
	return 0;
}

result

Just put a randomly generated number two
Export
Here Insert Picture Description

Published 51 original articles · won praise 1 · views 1393

Guess you like

Origin blog.csdn.net/shi_xiao_xuan/article/details/103564471
Recommended