1 #include<cstdio> 2 #include<cstdlib> 3 #include<string> 4 5 struct Node{ 6 int num; 7 Node* next; 8 }; 9 10 void Add(Node **head, int num){ 11 Node *node = new Node; 12 node->num = num; 13 node->next = *head; 14 (*head) = node; 15 } 16 17 void QuickSort(Node *head, Node *tail){ 18 if(head == tail) 19 return; 20 Node *temp = head; 21 for(Node *idx=head->next;idx!=tail;idx=idx->next){ 22 if(head->num > idx->num){ 23 temp=temp->next; 24 std::swap(temp->num, idx->num); 25 } 26 } 27 std::swap(head->num, temp->num); 28 QuickSort(head, temp); 29 QuickSort(temp->next, tail); 30 } 31 32 void Print(Node *head){ 33 Node *cur = head; 34 while (cur!=NULL){ 35 printf("%d ", cur->num); 36 cur=cur->next; 37 } 38 printf("\n"); 39 } 40 41 int main(){ 42 Node *head = NULL; 43 Add(&head, 1); 44 Add(&head, 2); 45 Add(&head, 3); 46 Add(&head, 4); 47 Add(&head, 5); 48 Add(&head, 6); 49 Add(&head, 7); 50 Add(&head, 8); 51 Print(head); 52 QuickSort(head, NULL); 53 Print(head); 54 return 0; 55 }
时间: 2024-10-19 19:43:22