In the 5G era, base stations can offer multiple services in various scenarios, enhancing spectrum efficiency by utilizing idle frequency bands for low-demand services while ensuring high-priority services are prioritized. This creates a distributed architecture for spectrum allocation. The proposed dynamic spectrum allocation scheme for base stations, optimizes spectrum rearrangement based on service priority, energy consumption, and renting cost. We employ the Particle Swarm Optimization (PSO) algorithm, inspired by bird flocking or fish schooling behavior, to solve the problem. Moreover, provide detailed information on our approach, demonstrating the effectiveness of PSO in optimizing spectrum allocation. By using PSO, we find the optimal solution considering service priority, energy consumption, and renting cost, supported by mathematical proofs of PSO algorithm performance. MATLAB is used as the simulation tool to carry out these techniques. Simulation experiments validate the approach, showing the stability and efficacy of PSO-based algorithm, which aligns with the theoretical analysis. . Key Words: Spectrum efficiency; 5G; dynamic spectrum allocation; service priority; PSO.