单链表的选择排序

描述

给定一个无序单链表,实现单链表的选择排序(按升序排序)。

要求:额外空间复杂度是O(1)。

输入描述:

第一行一个整数 n,表示单链表的节点数量。
第二行 n 个整数 val 表示单链表的各个节点。

输出描述:

在给出的函数内返回给定链表的头指针。

示例

输入:

5
1 3 2 4 5

输出:

1 2 3 4 5

 解题思路:额外空间复杂度是O(1)代表着不能用额外的数据结构。在有限的几个变量里对链表实现选择排序。选择排序就是在未排序部分里找出最小的那个数,然后放到排好序部分的末尾。逐渐将未排序的部分缩小,最后全变成排好序的部分。

import java.util.Scanner;
import java.util.HashSet;

class Node{
    public int value;
    public Node next;
    public Node(int value){
        this.value=value;
    }
}
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        String[] n=in.nextLine().split(" ");
        String[] num=in.nextLine().split(" ");
        
        Node node=transForNode(num);
        
        Node nodeF=selectionSort(node);
        
        show(nodeF);
    }
    //打印链表
    public static void show(Node head){
        while(head!=null){
            System.out.print(head.value+" ");
            head=head.next;
        }
        System.out.println();
    }
    //数组转链表
    public static Node transForNode(String[] nums) {
        Node head = new Node(Integer.parseInt(nums[0]));
        Node cur = head;
        for(int i = 1; i < nums.length; i++) {
            cur.next = new Node(Integer.parseInt(nums[i]));
            cur = cur.next;
        }
        return head;
    }
    
    //
    public static Node selectionSort(Node head){
        Node tail=null;
        Node cur=head;
        Node smallPre=null;
        Node small=null;
        while(cur!=null){
            small=cur;
            smallPre=getSmallestPreNode(cur);
            if(smallPre!=null){
                small=smallPre.next;
                smallPre.next=small.next;
            }
            cur=cur==small?cur.next:cur;
            if(tail==null){
                head=small;
            }else{
                tail.next=small;
            }
            tail=small;
        }
        return head;
    }
    
    //
    public static Node getSmallestPreNode(Node head){
        Node smallPre=null;
        Node small=head;
        Node cur=head.next;
        Node pre=head;
        while(cur!=null){
            if(cur.value<small.value){
                smallPre=pre;
                small=cur;
            }
            pre=cur;
            cur=cur.next;
        }
        return smallPre;
    }
}

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
好的,单链表选择排序的C语言实现可以分为以下几个步骤: 1. 定义链表节点结构体,包括数据域和指向下一个节点的指针域。 2. 定义链表排序函数,函数参数为链表头节点指针。 3. 在排序函数中,使用两个指针变量p和q,分别指向当前未排序部分的头节点和尾节点。 4. 在未排序部分中,找到最小值节点,并将其从未排序部分中删除。 5. 将最小值节点插入到已排序部分的尾部。 6. 重复步骤4和5,直到所有节点都被排序。 下面是单链表选择排序的C语言实现代码: ```c #include <stdio.h> #include <stdlib.h> // 定义链表节点结构体 typedef struct ListNode { int val; struct ListNode *next; } ListNode; // 定义链表排序函数 void selectionSortList(ListNode* head) { ListNode *p = head, *q = NULL, *minNode = NULL, *minPrev = NULL; while (p != NULL) { minNode = p; minPrev = NULL; q = p->next; while (q != NULL) { if (q->val < minNode->val) { minNode = q; minPrev = p; } p = p->next; q = q->next; } if (minNode != p) { if (minPrev != NULL) { minPrev->next = minNode->next; } else { head = minNode->next; } minNode->next = p; if (p == head) { head = minNode; } else { minPrev->next = minNode; } p = minNode; } else { p = p->next; } } } // 创建链表 ListNode* createList(int* nums, int numsSize) { ListNode *head = NULL, *tail = NULL; for (int i = 0; i < numsSize; i++) { ListNode *node = (ListNode*)malloc(sizeof(ListNode)); node->val = nums[i]; node->next = NULL; if (head == NULL) { head = node; tail = node; } else { tail->next = node; tail = node; } } return head; } // 打印链表 void printList(ListNode* head) { while (head != NULL) { printf("%d ", head->val); head = head->next; } printf("\n"); } int main() { int nums[] = {3, 1, 4, 2, 5}; int numsSize = sizeof(nums) / sizeof(int); ListNode *head = createList(nums, numsSize); printf("Before sorting: "); printList(head); selectionSortList(head); printf("After sorting: "); printList(head); return 0; } ```
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值