Community detection has a very important role in data processing and analysis, which is very hot in recent years. However, traditional algorithms have shortcomings in both time complexity and precision. In this paper, we introduce a Modified Genetic Algorithm (MGA) that with alleles encoding and half uniform crossover to detect community structure. In the algorithm, each allele of the chromosome stands for the community index of the corresponding node. At the same time, half uniform crossover can better prevent the elite individuals from destroying. And we choose modularity function as its fitness function. It does not need to know how many communities the network has. In order to identify our algorithm is effective. We use both artificial random network and real networks to test our algorithm. The experimental results show that the MGA algorithm can be applied to community detection, and its accuracy and time complexity can reach the effect of classical algorithms.