#include <stdio.h> #include <stdlib.h> typedef int ElemType; typedef struct ListNode { ElemType val; struct ListNode* next; } ListNode, *PListNode, *List; // function declaration void printList(List L); // 简单选择排序算法。复杂的选择排序又称堆排序!!! List simple_selection_sort_algorithm(List L, const int size); int main(const int argc, const char* argv[]) { int n, val; fscanf(stdin, "%d", &n); PListNode head = NULL, tail = NULL, p; while (fscanf(stdin, "%d", &val) != EOF) { p = (PListNode) malloc(sizeof(ListNode)); p->val = val; p->next = NULL; if (!tail) head = tail = p; else tail = tail->next = p; } head = simple_selection_sort_algorithm(head, n); return printList(head), 0; } void printList(List L) { PListNode p = L; while (p) { fprintf(stdout, "%d", p->val); if (p->next) fputc(32, stdout); p = p->next; } } List simple_selection_sort_algorithm(List L, const int size) { // corner case if (size < 2) return L; PListNode nodes[size], p = L; int i, t; for (i = 0; i < size; ++i, p = p->next) *(nodes + i) = p; int min; PListNode tmp; for (t = 0; t < size - 1; ++t) { min = t; for (i = t + 1; i < size; ++i) if ((*(*(nodes + i))).val < (*(*(nodes + min))).val) min = i; tmp = *(nodes + t); *(nodes + t) = *(nodes + min); *(nodes + min) = tmp; } for (i = 0; i < size - 1; ++i) (*(nodes + i))->next = *(nodes + i + 1); (*(nodes + size - 1))->next = NULL; return *nodes; }
import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.IOException; class ListNode { public int val; public ListNode next; public ListNode(int val) { this.val = val; this.next = null; } } public class Main { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int n = Integer.parseInt(br.readLine().trim()); String[] strArr = br.readLine().trim().split(" "); ListNode head = new ListNode(Integer.parseInt(strArr[0])); ListNode cur = head; for(int i = 1; i < n; i++){ cur.next = new ListNode(Integer.parseInt(strArr[i])); cur = cur.next; } head = selectionSort(head); while(head != null){ System.out.print(head.val + " "); head = head.next; } } private static ListNode selectionSort(ListNode head) { ListNode cur = head; while(cur.next != null){ ListNode minNode = cur; ListNode innerCur = minNode.next; while(innerCur != null){ if(innerCur.val < minNode.val) minNode = innerCur; innerCur = innerCur.next; } if(minNode.val != cur.val){ int val = minNode.val; minNode.val = cur.val; cur.val = val; } cur = cur.next; } return head; } }
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class Main { public static class Node { public int value; public Node next; public Node(int value) { this.value = value; } } public static Node listGenerator(int length, String[] numbers) { Node head = new Node(Integer.parseInt(numbers[0])); Node cur = head; for (int i = 1; i < length; i++) { cur.next = new Node(Integer.parseInt(numbers[i])); cur = cur.next; } cur.next = null; return head; } public static void printList(Node head) { while (head != null) { System.out.print(head.value +" "); head = head.next; } System.out.println(); } public static Node selectionSort(Node head) { if (head == null || head.next == null) { return head; } Node cur = head; Node node; Node min; int temp; while (cur != null) { node = cur.next; min = cur; while (node != null) { if (node.value < min.value) { min = node; } node = node.next; } temp = cur.value; cur.value = min.value; min.value = temp; cur = cur.next; } return head; } public static void main(String[] args) throws IOException { BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in)); int n = Integer.parseInt(bufferedReader.readLine()); String[] numbers = bufferedReader.readLine().split(" "); Node head = listGenerator(n, numbers); head = selectionSort(head); printList(head); } }
list_node * selection_sort(list_node * head) { //////在下面完成代码 int count = 0; list_node *newhead = new list_node(); newhead->next = head; head = newhead; while(head->next!=nullptr){ count ++; head=head->next; } list_node *tailnode = head; head = newhead; int min = INT32_MAX; list_node *minnode; while(count){ for(int i = 0;i < count;i++){ if(head->next->val < min){ min = head->next->val; minnode = head; } head = head->next; } head = newhead; tailnode->next = minnode->next; minnode->next = minnode->next->next; tailnode = tailnode->next; min = INT32_MAX; count--; if(count == 0)tailnode->next = nullptr; } return newhead->next; }
list_node * selection_sort(list_node * head) { //////在下面完成代码 if(head==NULL || head->next==NULL) return head; int len=0; list_node* p=head; while(p) { len++; p=p->next; } //冒泡排序 for(int i=0;i<len;i++) for(list_node* p=head;p->next;p=p->next) { if(p->val>p->next->val) swap(p->val,p->next->val); } return head; }
// 只有简单的题,才敢发到评论区 list_node * selection_sort(list_node * head,int n) { //////在下面完成代码 list_node* cur = head; list_node* ans = cur; while(cur != nullptr){ int Min = cur->val; list_node* m = cur; head = cur->next; while(head != nullptr){ if(head->val < Min){ m = head; Min = head->val; } head = head->next; } swap(cur->val,m->val); cur = cur->next; } return ans; }
list_node * selection_sort(list_node * head) { //////在下面完成代码 if(head==nullptr)return head; list_node *cur=new list_node(); cur->val=-1; cur->next=head; list_node *pHead=cur; while(cur->next!=nullptr){ list_node *small=cur->next; list_node *p=small; list_node *preSmall=p; list_node *pre=p; while(p!=nullptr){ if(p->val<small->val){ small=p; preSmall=pre; } pre=p; p=p->next; } if(small!=cur->next){ preSmall->next=small->next; small->next=cur->next; cur->next=small; } cur=cur->next; } return pHead->next; }
list_node * selection_sort(list_node * head) { //////在下面完成代码 for(list_node* p = head;p->next!=NULL;p=p->next) { int min = p->val; list_node *point=p;; list_node* q; for(q = p->next;q!=NULL;q=q->next){ if(min > q->val){ min = q->val; point = q; } } point->val = p->val; p->val = min; } return head; }