(仅供参考)王道2020算法题代码2.2(P18-P19)

(仅供参考)王道2020算法题代码2.2(P18-P19))

部分题目写入了次优解或者多种写法。
2.3节传送门

 //静态顺序表 
 typedef struct
 {
 	Elem data[Maxsize];
 	int length;
 }seqlist;
 
 //顺序表插入操作 
bool ListInsert(seqlist &L, Elem e, int i)
{
	if(i < 1 || i > L.length + 1)
		return false;
		
	if(L.length >= Maxsize)
		return false;
		
	else
	{
		for(int k = L.length; k >= i; k--)
			L.data[k] = L.data[k-1];

		L.data[i-1] = e;
		L.length ++;
		
		return true;
	}
 } ;
 
 //顺序表删除操作 
bool ListDelete(seqlist &L, int i, Elem &e)
 {
	if(i < 1 || i > L.length)
		return false;
	
	e = L.data[i-1];
	
	for(int k = i; k <L.length; k++)
		L.data[k-1] = L.data[k];
	L.length--;
	
	return true;
}; 

//按值查找元素

int LocateElem(seqlist &L, Elem e)
{
	for(int k = 0; k <L.length; k++)
		if (L.data[k] == e)
		return k+1;
	
	return 0;
 }; 

//T1

bool Deletemin(seqlist &L, Elem &e)
{
	if(L.length == 0)
		return false;
		
	e = L.data[0];
	int pos;
	for(int k = 1; k < L.length; k++)
	{
		if(L.data[k] < e)
		{
			e = L.data[k];
			pos = k;
		}
	}
	
	L.data[pos] = L.data[L.length];
	L.length--;
	
	return true;
};

//T2
void Reverse(seqlist &L)
{
	Elem e;
	int i,j;
	for(i = 0, j = L.length-1; i < j; i++, j--)
	{
		e = L.data[i];
		L.data[i] = L.data[j];
		L.data[j] = e;
	}
 } ;
 
//T3 
void delx1(seqlist &L, Elem x)
{
	int k = 0;
	for(int i = 0; i < L.length; i++)
	{
		if(L.data[i] = x)
			k++;
		else
			L.data[i-k] = L.data[i];
			
		L.length -= k;
	}
 } ;
void delx2(seqlist &L, Elem x)
{
	int k = 0;			//k为 ≠x的数量;
	
	for(int i = 0; i < L.length; i++)
{
	if(L.data[i] != x)
	{
		L.data[k] = L.data[i];
		k++;
	}
 } 
};

//T4
bool deleterange(seqlist &L, Elem s, Elem t)
{
	if(s >= t || L.length == 0)
		return false;
	
	int i = 0;	int j;
	for(j = 0; L.data[j] <= t; j++)
	{
		if(L.data[j] <= s)
			i++;
	}
	
	for(i = i+1; j < L.length; i++,j++)
		L.data[i] = L.data[j];
		
	L.length = i;
	return true;
};

//T5
bool deleterange5(seqlist &L, int s, int t)
{
	if(s >= t || L.length == 0)
		return false;
		
	int k = 0;
	for(int i = 0; i < L.length; i++)
	{
		if(L.data[i] >= s && L.data[i] <= t)
			k++;
		else
			L.data[i-k] = L.data[i];
	}
	
	L.length -= k;
	
	return true;
 } ;
 
 //T6
 bool deletesame(seqlist &L)
 {
 	if(L.length == 0)
 		return false;
 	
	int k = 0;	
 	for(int i = 1; i < L.length; i++)
 	{
 		if(L.data[i-1] == L.data[i])
 			k++;
 		else
 			L.data[i-k] = L.data[i];
	 }
	 
	 L.length -= k;
	 return true;
  } 
  bool deletesame1(seqlist &L)
  {
  	if(L.length == 0)
  		return false;
  		
  	int i,j;	
  	for(i = 0,j = 1; j < L.length; j++)
  	{
  		if(L.data[i] != L.data[j])
			L.data[++i] = L.data[j];
		
	}
	
	L.length = i+1;
	return true;
  }
  
  //T7
  bool Merge(seqlist L1, seqlist L2, seqlist &L3)
  {
  	if(L1.length + L2.length > Maxsize)
  		return false;
  		
  	int i,j,k;
  	for(i = 0,j = 0,k = 0; i < L1.length || j < L2.length; k++)
  		if(L1.data[i] <= L2.data[j])
  			L3.data[k] = L1.data[i++];
  		else
  			L3.data[k] = L2.data[j++];
  			
  	while(j < L2.length)
  		L3.data[k++] = L2.data[j++];
	while(i < L1.length)
		L3.data[k++] = L1.data[j++];
		
	L3.length = k;
	return true;
  }
  
  //T8
  void Reverse1(Elem a[], int l, int r, int arraysize)
  {
  	if(l >= r || r >= arraysize)
  		return;
  	
  	Elem temp;
  	for(; l < r; l++, r--)
  	{
  		temp = a[l];
  		a[l] = a[r];
  		a[r] = temp;
	}
  }
  
  void Exchange(Elem a[], int m, int n, int arraysize)
  {
  	Reverse1(a, 0, m+n-1, arraysize);
  	Reverse1(a, 0, n-1, arraysize);
  	Reverse1(a, n, m+n-1, arraysize);
  }
  
  //T9
   void LocateExchangeorInsert(Elem a[], int n, Elem e)
   {
   		int low = 0;
		int high = n-1;
		int mid;
   		while(low <= high)
   		{
   			mid = (low + high) / 2;
   			if (a[mid] == e)
   				break;
   			else if (a[mid] < e)
   				low = mid + 1;
   			else
   				high = mid - 1;
		}
		
		if(low > high)						//折半插入算法 
		{
			for(int i = n-1; i > high; i--) //等价于for(int i = n-1; i >= mid; i--)
				a[i+1] = a[i];
			a[high + 1] = e; 
		}
		
		if(a[mid] == e && mid != n-1)
		{
			a[mid] = a[++mid];
			a[mid] = e;
		}
   }
   
   //T10 同T8思路一致 时间复杂度O(n)空间复杂度O(1) 
   void Reserve_T10(Elem a[], int l, int r)
   {
   		Elem temp;
   		for(; l < r; l++, r--)
		{
			temp = a[l];
			a[l] = a[r];
			a[r] = temp;
		}
   }
   
   void Leftmove(Elem a[], int n, int p)
   {
   		Reserve_T10(a, 0, n-1);
   		Reserve_T10(a, 0, n-p-1);
   		Reserve_T10(a, n-p-1, n-1);
   }

//T11 最优解 T(n) = O(log2n) S(n) = O(1); 
int Midsearch(Elem a[], Elem b[], int n)
{
	int s1, d1, s2, d2, m1, m2;
	s1 = 0; s2 = 0; d1 = n-1; d2 = n-1;
	
	while(s1 < d1 || s2 < d2)
	{
		m1 = (s1 + d1) / 2;
		m2 = (s2 + d2) / 2;
		if(a[m1] == b[m2])
			return a[m1];
		else if(a[m1] < b[m2])
		{
			if((s1 + d1) % 2 == 0) //奇数个? 
			{
				s1 = m1;
				d2 = m2;
			}
			else
			{
				s1 = m1 + 1;
				d2 = m2; 
			}
		}
		else
		{
			if((s1 + d1) % 2 == 0) //奇数个? 
			{
				d1 = m1;
				s2 = m2;
			}
			else
			{
				d1 = m1;
				s2 = m2 + 1; 
			}
		}
		 
	}
	return a[s1] < b[s2] ? a[s1] : b[s2];
 } 
 //T11次优解 T(n) = O(n) S(n) = O(1);
int Midsearch2(Elem a[], Elem b[], int n)
{
	int m1 = 0;
	int m2 = 0;
	int m;
	
	while(m1 + m2 +2 <= n)
	{
		if(a[m1] < b[m2])
		{
			m1++;
			m = b[m2];
		}
		else
		{
			m2++;
			m = a[m1];
		}
	}
	
	return m;
}

//T12次优解T(n) = O(n) S(n) = O(n);
int MainElem(int a[], int n)
{
	int b[n] = {0};
	int i = 0;
	for(i = 0; i < n; i++)
	{
		b[a[i]]++;
	}
	
	int max = 0;
	for(i = 1; i < n ; i++)
		if(b[max] < b[i])
			max = i;
	
	if(b[max] > n/2)
		return b[max];
	else
		return -1;
}
//T12最优解 
int MainElem1(int a[], int n)
{
	int count = 1;
	int temp = a[0];
	int i;
	for(i = 1; i < n; i++)
	{
		if(a[i] == temp)
			count++;
		else if(count > 0)
			count--;
		else
			temp = a[i];
			count = 1;
			
	}
	for(i = count = 0; i < n; i++)
		if(a[i] == temp)
			count++;
			
	if(count > n/2)
		return temp;
	else
		return -1;
} 

//T13最优解T(n) = O(n) S(n) = O(n);
int Findmissmin(int a[], int n)
{
	int b[n] = {0};
	int i;
	
	for(i = 0; i < n; i++)
	{
		if(a[i] > 0 && a[i] <= n)
			b[a[i]-1] = 1;
	}
	for(i = 0; i < n; i++)
	{
		if(b[i] == 0)
			return i+1;
	}
	
	return i+1;
}

你可能感兴趣的:(王道DS)