java中实现归并排序与链表的使用

目录

在Java中实现归并排序可以按照以下步骤进行:

创建一个用于归并排序的方法,接收一个整数数组作为参数:

创建一个递归方法用于合并两个有序数组:

创建一个示例来测试排序方法:

在Java中,链表可以使用一个节点类来实现。节点类中包含一个数据成员和一个指向下一个节点的指针。

以下是链表的基本操作的一个示例:

下面是Java中使用链表实现归并排序的示例代码:



归并排序

归并排序是建立在归并操作上的一种有效,稳定的排序算法,该算法是采用分治法(Divide and Conquer)的一个非常典型的应用。将已有序的子序列合并,得到完全有序的序列;即先使每个子序列有序,再使子序列段间有序。若将两个有序表合并成一个有序表,称为二路归并

在Java中实现归并排序可以按照以下步骤进行:

  1. 创建一个用于归并排序的方法,接收一个整数数组作为参数:

    public class MergeSort {
        public void mergeSort(int[] array) {
            if (array.length < 2) {
                return;
            }
            int middle = array.length / 2;
            int[] left = new int[middle];
            int[] right = new int[array.length - middle];
            for (int i = 0; i < middle; i++) {
                left[i] = array[i];
            }
            for (int i = middle; i < array.length; i++) {
                right[i - middle] = array[i];
            }
            mergeSort(left);
            mergeSort(right);
            merge(left, right, array);
        }
    }
     
    

  2. 创建一个递归方法用于合并两个有序数组:

    private void merge(int[] left, int[] right, int[] result) {
        int leftLength = left.length;
        int rightLength = right.length;
        int i = 0, j = 0, k = 0;
        while (i < leftLength && j < rightLength) {
            if (left[i] <= right[j]) {
                result[k++] = left[i++];
            } else {
                result[k++] = right[j++];
            }
        }
        while (i < leftLength) {
            result[k++] = left[i++];
        }
        while (j < rightLength) {
            result[k++] = right[j++];
        }
    }
     
    

  3. 创建一个示例来测试排序方法:

    import java.util.Arrays;
    
    public class MergeSortExample {
        public static void main(String[] args) {
            int[] array = {5, 3, 8, 4, 2, 1, 9, 7, 6};
            mergeSort(array);
            System.out.println("Sorted array: " + Arrays.toString(array));
        }
    
        public static void mergeSort(int[] array) {
            if (array.length < 2) {
                return;
            }
            int mid = array.length / 2;
            int[] left = new int[mid];
            int[] right = new int[array.length - mid];
            for (int i = 0; i < mid; i++) {
                left[i] = array[i];
            }
            for (int i = mid; i < array.length; i++) {
                right[i - mid] = array[i];
            }
    
            mergeSort(left);
            mergeSort(right);
            merge(left, right, array);
        }
    
        public static void merge(int[] left, int[] right, int[] result) {
            int i = 0, j = 0, k = 0;
            while (i < left.length && j < right.length) {
                if (left[i] <= right[j]) {
                    result[k++] = left[i++];
                } else {
                    result[k++] = right[j++];
                }
            }
            while (i < left.length) {
                result[k++] = left[i++];
            }
            while (j < right.length) {
                result[k++] = right[j++];
            }
        }
    }
     
    

    在Java中,链表可以使用一个节点类来实现。节点类中包含一个数据成员和一个指向下一个节点的指针。

    以下是链表的基本操作的一个示例:

    class Node {
        int data;
        Node next;
    
        public Node(int data) {
            this.data = data;
            this.next = null;
        }
    }
    
    class LinkedList {
        Node head;
    
        public void append(int data) {
            Node newNode = new Node(data);
    
            if (head == null) {
                head = newNode;
            } else {
                Node current = head;
                while (current.next != null) {
                    current = current.next;
                }
                current.next = newNode;
            }
        }
    
        public void prepend(int data) {
            Node newNode = new Node(data);
    
            newNode.next = head;
            head = newNode;
        }
    
        public void delete(int data) {
            if (head == null) {
                return;
            } else if (head.data == data) {
                head = head.next;
            } else {
                Node current = head;
                while (current.next != null && current.next.data != data) {
                    current = current.next;
                }
                if (current.next != null) {
                    current.next = current.next.next;
                }
            }
        }
    
        public boolean isEmpty() {
            return head == null;
        }
    
        public void printList() {
            Node current = head;
            while (current != null) {
                System.out.print(current.data + " ");
                current = current.next;
            }
            System.out.println();
        }
    }
    
    public class Main {
        public static void main(String[] args) {
            LinkedList linkedList = new LinkedList();
    
            linkedList.append(1);
            linkedList.append(2);
            linkedList.append(3);
            linkedList.append(4);
    
            linkedList.printList(); // 输出: 1 2 3 4
    
            linkedList.prepend(0);
    
            linkedList.printList(); // 输出: 0 1 2 3 4
    
            linkedList.delete(2);
            linkedList.delete(4);
    
            linkedList.printList(); // 输出: 0 1 3
        }
    }
     
    

  4. 下面是Java中使用链表实现归并排序的示例代码:

  5. class ListNode {
        int val;
        ListNode next;
    
        ListNode(int val) {
            this.val = val;
        }
    }
    
    public class MergeSortLinkedList {
        public static ListNode mergeSort(ListNode head) {
            if (head == null || head.next == null) {
                return head;
            }
    
            ListNode middle = getMiddle(head);
            ListNode nextOfMiddle = middle.next;
            middle.next = null;
    
            ListNode left = mergeSort(head);
            ListNode right = mergeSort(nextOfMiddle);
    
            return merge(left, right);
        }
    
        private static ListNode getMiddle(ListNode head) {
            if (head == null) {
                return head;
            }
    
            ListNode slow = head;
            ListNode fast = head;
    
            while (fast.next != null && fast.next.next != null) {
                slow = slow.next;
                fast = fast.next.next;
            }
    
            return slow;
        }
    
        private static ListNode merge(ListNode left, ListNode right) {
            if (left == null) {
                return right;
            }
    
            if (right == null) {
                return left;
            }
    
            ListNode result = null;
    
            if (left.val <= right.val) {
                result = left;
                result.next = merge(left.next, right);
            } else {
                result = right;
                result.next = merge(left, right.next);
            }
    
            return result;
        }
    
        public static void main(String[] args) {
            ListNode head = new ListNode(3);
            head.next = new ListNode(4);
            head.next.next = new ListNode(1);
            head.next.next.next = new ListNode(2);
    
            ListNode sortedList = mergeSort(head);
    
            while (sortedList != null) {
                System.out.print(sortedList.val + " ");
                sortedList = sortedList.next;
            }
        }
    }
     
    

  • 3
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值