Learning to program is a challenging task for novices. Students vary substantially in their ability to understand complex and abstract topics in computer programming logic, such as loop logic, function recursion, arrays, passing parameters, and program structure design. Cooperative learning is an effective method of learning and teaching programming. In traditional cooperative learning, students group themselves, or teachers group students intuitively. This paper proposes a clustering method based on item response theory (IRT) and the revised Girvan-Newman clustering for clustering students by learning ability. Item response theory calculated the learner's ability and interpersonal relationship questionnaire generated by the social network analysis. The proposed method was validated by conducting a quasi-experimental test in a freshmen programming course, and the method significantly improved learning outcomes in this course.